How To Configure Log4j Inwards Coffee Plan Without Xml Or Properties File
Sometime configuring Log4j using XML or properties file looks annoying, specially if your programme non able to detect them because of only about classpath issues, wouldn't it endure prissy if you lot tin configure in addition to work Log4j without using whatever configuration file e.g. XML or properties. Well, Log4j people has idea close it in addition to they furnish a BasicConfigurator class to configure log4j programmatically, idea this is non every bit rich every bit at that topographic point XML in addition to properties file version is, but it's actually handy for speedily 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 ask whatever configuration, you lot tin only work it, without bothering close XML or properties file configuration, but most programmer volition concord that, they would prefer to work Log4j over println statements, fifty-fifty for bear witness programs if it's like shooting fish in a barrel to gear upwards them up.
By the way, for production work prefer SLF4J over Log4J. In this Java tutorial, nosotros volition meet a prissy piddling tip to work Log4j correct away past times configuring inward brace of lines. In fact you lot tin configure Log4J inward only 1 line, if you lot intend to work at that topographic point basic configuration which set's the log marker every bit DEBUG.
properties files, which are essentially text files. This code leverage configuration options defined inward BasicConfigurator class.
Couple of things to depository fiscal establishment notation close basic Log4j Configuration :
1) BasicConfigurator display log messages into console by using PatternLayout with pattern "%-4r [%t] %-5p %c %x - %m%n" to display log messages.
2) Root logger is gear upwards to work DEBUG log level, which agency it volition display all messages. If you lot hold back at our example, nosotros convey reset the log marker to WARN, which agency solely ERROR and WARN level message volition endure displayed into console, in addition to INFO and DEBUG level messages volition endure 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 marker on production in addition to DEBUG during development
3) You tin reset configuration past times calling BasiConfigurator.reset() method.
Isn't it non bad to work Log4j only similar this, I actually liked it. Yes, I yet prefer Sytem.out.println() tilt for simplest business but similar to work Log4j to a greater extent than oftentimes now. Though, it yet brand feel to work 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 work prefer SLF4J over Log4J. In this Java tutorial, nosotros volition meet a prissy piddling tip to work Log4j correct away past times configuring inward brace of lines. In fact you lot tin configure Log4J inward only 1 line, if you lot intend to work at that topographic point basic configuration which set's the log marker every bit DEBUG.
Log4J Configuration without XML file
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 atomic number 82 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 atomic number 82 to organisation impact
Couple of things to depository fiscal establishment notation close basic Log4j Configuration :
1) BasicConfigurator display log messages into console by using PatternLayout with pattern "%-4r [%t] %-5p %c %x - %m%n" to display log messages.
2) Root logger is gear upwards to work DEBUG log level, which agency it volition display all messages. If you lot hold back at our example, nosotros convey reset the log marker to WARN, which agency solely ERROR and WARN level message volition endure displayed into console, in addition to INFO and DEBUG level messages volition endure 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 marker on production in addition to DEBUG during development
3) You tin reset configuration past times calling BasiConfigurator.reset() method.
Isn't it non bad to work Log4j only similar this, I actually liked it. Yes, I yet prefer Sytem.out.println() tilt for simplest business but similar to work Log4j to a greater extent than oftentimes now. Though, it yet brand feel to work 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!
Komentar
Posting Komentar