Sometime configuring Log4j using XML or properties file looks annoying, specially if your plan non able to uncovering them because of unopen to classpath issues, wouldn't it survive prissy if you lot tin configure together with exercise Log4j without using whatsoever configuration file e.g. XML or properties. Well, Log4j people has idea near it together with they render a BasicConfigurator class to configure log4j programmatically, idea this is non equally rich equally at that spot XML together with properties file version is, but it's actually handy for rapidly incorporating Log4j inwards 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 whatsoever configuration, you lot tin only exercise it, without bothering near XML or properties file configuration, but most programmer volition concur that, they would prefer to exercise Log4j over println statements, fifty-fifty for attempt out programs if it's piece of cake to prepare them up.
By the way, for production exercise prefer SLF4J over Log4J. In this Java tutorial, nosotros volition encounter a prissy niggling tip to exercise Log4j correct away yesteryear configuring inwards pair of lines. In fact you lot tin configure Log4J inwards only ane line, if you lot intend to exercise at that spot basic configuration which set's the log score equally DEBUG.
properties files, which are essentially text files. This code leverage configuration options defined inwards BasicConfigurator class.
Couple of things to banker's complaint near 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 way it volition display all messages. If you lot expect at our example, nosotros bring reset the log score to WARN, which way alone ERROR and WARN level message volition survive displayed into console, together with INFO and DEBUG level messages volition survive suppressed. This is ane 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 plan volition be. Use ERROR or WARN score on production together with DEBUG during development
3) You tin reset configuration yesteryear calling BasiConfigurator.reset() method.
Isn't it dandy to exercise Log4j only similar this, I actually liked it. Yes, I notwithstanding prefer Sytem.out.println() disputation for simplest business but similar to exercise Log4j to a greater extent than oftentimes now. Though, it notwithstanding brand feel to exercise XML based logger for production use, Log4j amongst 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!
By the way, for production exercise prefer SLF4J over Log4J. In this Java tutorial, nosotros volition encounter a prissy niggling tip to exercise Log4j correct away yesteryear configuring inwards pair of lines. In fact you lot tin configure Log4J inwards only ane line, if you lot intend to exercise at that spot basic configuration which set's the log score equally DEBUG.
Log4J Configuration without XML file
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
/**
* Java plan to configure log4j without using XML or properties file.
* By using BasicConfigurator class, you lot tin configure Log4j inwards ane 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 banker's complaint near 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 way it volition display all messages. If you lot expect at our example, nosotros bring reset the log score to WARN, which way alone ERROR and WARN level message volition survive displayed into console, together with INFO and DEBUG level messages volition survive suppressed. This is ane 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 plan volition be. Use ERROR or WARN score on production together with DEBUG during development
3) You tin reset configuration yesteryear calling BasiConfigurator.reset() method.
Isn't it dandy to exercise Log4j only similar this, I actually liked it. Yes, I notwithstanding prefer Sytem.out.println() disputation for simplest business but similar to exercise Log4j to a greater extent than oftentimes now. Though, it notwithstanding brand feel to exercise XML based logger for production use, Log4j amongst 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!