0

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-x.x.x.tar.gz files, instead there are apache-log4j-x.x.x-bin.tar.gz and apache-log4j-x.x.x-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!

Jacobian
  • 10,122
  • 29
  • 128
  • 221
  • Whats about this: [log4j](http://www.mkyong.com/tutorials/log4j-tutorial/) – Patrick Nov 13 '15 at 09:48
  • This tutorial refers to http://logging.apache.org/log4j/1.2/ page which dates back to 2012-05-13. Not sure, whether it is fresh. – Jacobian Nov 13 '15 at 09:54

1 Answers1

0

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");
}`

}

Community
  • 1
  • 1
Slimu
  • 2,301
  • 1
  • 22
  • 28
  • Should log4j.properties file be in the same directory as those two log4j jars (api and core jar)? Or should it be in the same directory as the running program? – Jacobian Nov 13 '15 at 09:56
  • It should work if you put it in the same directory where you have the class holding the main method. If you are using an IDE like Intellij you could create a folder where to store all kind of property files and mark it as a resource folder (for testing purposes). – Slimu Nov 13 '15 at 10:00
  • See also this http://stackoverflow.com/questions/1485987/log4j-properties-file-where-to-put-it – Slimu Nov 13 '15 at 10:02