How To Configure Log4j Inwards Coffee Programme Without Xml Or Properties File

Sometime configuring Log4j using XML or properties file looks annoying, particularly if your programme non able to detect them because of simply about classpath issues, wouldn't it live on overnice if you lot tin configure as well as utilisation Log4j without using whatever configuration file e.g. XML or properties. Well, Log4j people has idea well-nigh it as well as they render 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 speedily incorporating Log4j inwards your Java program. One of the argue programmer prefer to utilisation System.out.println() over Log4j, of-course for testing purpose, because it doesn't ask whatever configuration, you lot tin simply utilisation it, without bothering well-nigh XML or properties file configuration, but most programmer volition handgrip that, they would prefer to utilisation Log4j over println statements, fifty-fifty for assay out programs if it's piece of cake to laid upward them up.

By the way, for production utilisation prefer SLF4J over Log4J.  In this Java tutorial, nosotros volition run into a overnice trivial tip to utilisation Log4j correct away yesteryear configuring inwards span of lines. In fact you lot tin configure Log4J inwards simply 1 line, if you lot intend to utilisation at that topographic point basic configuration which set's the log marking every bit DEBUG.



Log4J Configuration without XML file

properties files, which are essentially text files. This code leverage configuration options defined inwards 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 inwards 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 organization 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 organization impact

Couple of things to regime notation well-nigh 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 laid upward to utilisation DEBUG log level, which agency it volition display all messages. If you lot expect at our example, nosotros accept reset the log marking to WARN, which agency entirely ERROR and WARN level message volition live on displayed into console, as well as INFO and DEBUG level messages volition live on suppressed. This is 1 of the important logging tips inwards 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 marking on production as well as DEBUG during development

3) You tin reset configuration yesteryear calling BasiConfigurator.reset() method.

Isn't it groovy to utilisation Log4j simply similar this, I actually liked it. Yes, I even as well as thence prefer Sytem.out.println() disceptation for simplest business but similar to utilisation Log4j to a greater extent than frequently now. Though, it even as well as thence brand feel to utilisation XML based logger for production use, Log4j alongside basic configuration is simply fine for temporary or tutorial purpose. 

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


Sumber https://javarevisited.blogspot.com/

0 Response to "How To Configure Log4j Inwards Coffee Programme Without Xml Or Properties File"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel