Extra 10. Log4J
Main steps
Add dependency on log4j in pom.xml file:
<dependencies>
...
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
...
</dependencies>where ... are some other existing settings
Recompile project and regenerate settings for eclipse:
mvn clean compile eclipse:eclipseAdd file logger named file with file appender and layout format into configuration file src/main/resources/log4j.properties:
- Folder of the log file should be logs folder inside project folder
- Name of log file should be test.log
e.g.:
log4j.rootLogger=..., file
...
# Direct log messages to file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=logs/test.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{4} %M:%L - %m%n
...Set category filter settings in log4j.properties file, that logger will show:
- INFO messages for jtm.extra10.Log class
- TRACE messages for jtm.extra10.Log$ExtLog class
e.g.:
...
log4j.logger.jtm.extra10.Log=info
log4j.logger.jtm.extra10.Log$ExtLog=trace
...- Review package jtm.extra10
- Implement class Log with methods trace, debug, info, warn, error, fatal which will write log messages with appropriate level,
- Implement method getLogger() which returns reference to used logger,
- In class Log implement internal class ExtLog which also implements static methods trace, debug, info, warn, error, fatal which will write log messages with appropriate level.
- Check that unit tests are passing.