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

Sometime configuring Log4j using XML or properties file looks annoying, peculiarly if your programme non able to notice them because of but about classpath issues, wouldn't it endure overnice if y'all tin flame configure too work Log4j without using whatever configuration file e.g. XML or properties. Well, Log4j people has idea well-nigh it too they render a BasicConfigurator class to configure log4j programmatically, idea this is non equally rich equally at that spot XML too properties file version is, but it's actually handy for rapidly incorporating Log4j inward your Java program. One of the argue programmer prefer to work System.out.println() over Log4j, of-course for testing purpose, because it doesn't quest whatever configuration, y'all tin flame but work it, without bothering well-nigh XML or properties file configuration, but most programmer volition handgrip that, they would prefer to work Log4j over println statements, fifty-fifty for seek programs if it's piece of cake to fix them up.

By the way, for production work prefer SLF4J over Log4J.  In this Java tutorial, nosotros volition encounter a overnice picayune tip to work Log4j correct away yesteryear configuring inward duet of lines. In fact y'all tin flame configure Log4J inward but i line, if y'all think to work at that spot basic configuration which set's the log marker equally 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, y'all tin flame configure Log4j inward i 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 arrangement 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 arrangement impact

Couple of things to regime notation well-nigh basic Log4j Configuration :

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

2) Root logger is fix to work DEBUG log level, which way it volition display all messages. If y'all await at our example, nosotros convey reset the log marker to WARN, which way exclusively ERROR and WARN level message volition endure displayed into console, too INFO and DEBUG level messages volition endure suppressed. This is i of the important logging tips inward Java to remember, because total of  logging is inversely proportional to performance, to a greater extent than y'all log, slower your programme volition be. Use ERROR or WARN marker on production too DEBUG during development

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

Isn't it smashing to work Log4j but similar this, I actually liked it. Yes, I even too then prefer Sytem.out.println() contestation for simplest chore but similar to work Log4j to a greater extent than oftentimes now. Though, it even too then brand feel to work XML based logger for production use, Log4j amongst basic configuration is but fine for temporary or tutorial purpose. 

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

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