简体   繁体   中英

Log4j installation and configuration

I tried to follow this tutorial in order to install and use log4j in my application, but it seems like this tutorial is outdated.

First of all, contrary to this , on the official page there are no apache-log4j-xxxtar.gz files, instead there are apache-log4j-xxx-bin.tar.gz and apache-log4j-xxx-src.tar.gz.

So, I downloaded both archives, but their contents has nothing to do with

apache-log4j-1.2.15/tests/input/
apache-log4j-1.2.15/tests/input/xml/
apache-log4j-1.2.15/tests/src/
apache-log4j-1.2.15/tests/src/java/
apache-log4j-1.2.15/tests/src/java/org/
....

And finally, the tutorial is addressing log4j.properties file, however, nothing is said about where it is stored or should it be created manually and so on. I hope, someone can provide a fresher tutorial. Thanks!

When I first started learning about log4j , I read first about it's architecture from here Log4j introduction . There you will also find a download link to the log4j jars required for using this API.

Regarding the log4j.properties file, you will have to create it yourself. I'll add bellow a sample of it

#Define the root logger with the appender FILE

log4j.rootLogger = INFO, FILE

Set the appender named FILE to be a File Appender

log4j.appender.FILE=org.apache.log4j.RollingFileAppender log4j.appender.FILE.File=logs/log.out

log4j.appender.FILE.threshold=DEBUG

log4j.appender.FILE.MaxFileSize=10MB log4j.appender.FILE.MaxBackupIndex=10 log4j.appender.FILE.ImmediateFlush=true log4j.appender.FILE.Append=true

Define the layout for FILE appender

log4j.appender.FILE.layout=org.apache.log4j.PatternLayout log4j.appender.FILE.layout.conversionPattern=%m%n

Define the CONSOLE Appender to be a Console Appender

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender

Define the layout for the CONSOLE Appender

log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout.conversionPattern=%m%n

Here a simple test class`public class TestLog4j {

private static final Logger logger = Logger.getLogger("newLogger");

public static void main(String[] args) {
    logger.info("Hello World");
}`

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM