How To Configure Log4j Inward Coffee Plan Without Xml Or Properties File

How To Configure Log4j Inward Coffee Plan Without Xml Or Properties File - Hallo sahabat BEST LEARNING JAVA, Pada Artikel yang anda baca kali ini dengan judul How To Configure Log4j Inward Coffee Plan Without Xml Or Properties File, kami telah mempersiapkan artikel ini dengan baik untuk anda baca dan ambil informasi didalamnya. mudah-mudahan isi postingan Artikel core java, Artikel logging, Artikel programming, yang kami tulis ini dapat anda pahami. baiklah, selamat membaca.

Judul : How To Configure Log4j Inward Coffee Plan Without Xml Or Properties File
link : How To Configure Log4j Inward Coffee Plan Without Xml Or Properties File

Baca juga


How To Configure Log4j Inward Coffee Plan Without Xml Or Properties File

Sometime configuring Log4j using XML or properties file looks annoying, specially if your programme non able to honour them because of about classpath issues, wouldn't it hold upward overnice if you lot tin configure as well as exercise Log4j without using whatever configuration file e.g. XML or properties. Well, Log4j people has idea almost it as well as they furnish a BasicConfigurator class to configure log4j programmatically, idea this is non every bit rich every bit at that topographic point XML as well as properties file version is, but it's actually handy for chop-chop incorporating Log4j inward your Java program. One of the argue programmer prefer to exercise System.out.println() over Log4j, of-course for testing purpose, because it doesn't demand whatever configuration, you lot tin only exercise it, without bothering almost XML or properties file configuration, but most programmer volition concur that, they would prefer to exercise Log4j over println statements, fifty-fifty for examine programs if it's like shooting fish in a barrel to prepare them up.

By the way, for production exercise prefer SLF4J over Log4J.  In this Java tutorial, nosotros volition encounter a overnice petty tip to exercise Log4j correct away past times configuring inward couplet of lines. In fact you lot tin configure Log4J inward only 1 line, if you lot intend to exercise at that topographic point basic configuration which set's the log score every bit DEBUG.



Log4J Configuration without XML file

properties files, which are essentially text files. This code leverage configuration options defined inward BasicConfigurator class.


import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;

/**
  * Java programme to configure log4j without using XML or properties file.
  * By using BasicConfigurator class, you lot tin configure Log4j inward 1 line.
  * @author
  */
public class Log4JConfigurator {
    private static final Logger logger = Logger.getLogger(Log4JConfigurator.class);
  
    public static void main(String args[]) {
      
      
        BasicConfigurator.configure(); //enough for configuring log4j
      
        Logger.getRootLogger().setLevel(Level.WARN); //changing log level
      
      
        logger.error("Critical message, almost fatal");
        logger.warn("Warnings, which may Pb to organisation impact");
        logger.info("Information");
        logger.debug("Debugging information ");
           
    }    
  
}

Output:
0 [main] ERROR Log4JConfigurator  - Critical message, almost fatal
0 [main] WARN Log4JConfigurator  - Warnings, which may Pb to organisation impact

Couple of things to banker's complaint almost basic Log4j Configuration :

1) BasicConfigurator display log messages into console by using PatternLayout with blueprint "%-4r [%t] %-5p %c %x - %m%n" to display log messages.

2) Root logger is prepare to exercise DEBUG log level, which agency it volition display all messages. If you lot await at our example, nosotros accept reset the log score to WARN, which agency solely ERROR and WARN level message volition hold upward displayed into console, as well as INFO and DEBUG level messages volition hold upward suppressed. This is 1 of the important logging tips inward Java to remember, because sum of  logging is inversely proportional to performance, to a greater extent than you lot log, slower your programme volition be. Use ERROR or WARN score on production as well as DEBUG during development

3) You tin reset configuration past times calling BasiConfigurator.reset() method.

Isn't it neat to exercise Log4j only similar this, I actually liked it. Yes, I nevertheless prefer Sytem.out.println() disceptation for simplest chore but similar to exercise Log4j to a greater extent than oftentimes now. Though, it nevertheless brand feel to exercise XML based logger for production use, Log4j alongside basic configuration is only fine for temporary or tutorial purpose. 

Further Learning
Complete Java Masterclass
Java Fundamentals: The Java Language
Java In-Depth: Become a Complete Java Engineer!



Demikianlah Artikel How To Configure Log4j Inward Coffee Plan Without Xml Or Properties File

Sekianlah artikel How To Configure Log4j Inward Coffee Plan Without Xml Or Properties File kali ini, mudah-mudahan bisa memberi manfaat untuk anda semua. baiklah, sampai jumpa di postingan artikel lainnya.

Anda sekarang membaca artikel How To Configure Log4j Inward Coffee Plan Without Xml Or Properties File dengan alamat link https://bestlearningjava.blogspot.com/2019/09/how-to-configure-log4j-inward-coffee.html

Belum ada Komentar untuk "How To Configure Log4j Inward Coffee Plan Without Xml Or Properties File"

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel