Extra 10 exercise: Java Logging with Log4J

  1. Review presentation
  2. Add dependency on log4j in pom.xml file:

            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>1.2.17</version>
            </dependency>
  3. Recompile project and regenerate settings for eclipse:

    mvn clean compile eclipse:eclipse
  4. Add file logger named file with file appender and layout format into configuration file src/main/resources/log4j.properties:

    1. Folder of the log file should be logs folder inside project folder
    2. 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
    ...
  5. Set category filter settings in log4j.properties file, that logger will show:

    1. INFO messages for jtm.extra10.Log class
    2. TRACE messages for jtm.extra10.Log$ExtLog class

    e.g.:

    ...
    log4j.logger.jtm.extra10.Log=info
    log4j.logger.jtm.extra10.Log$ExtLog=trace
    ...
  6. Review package jtm.extra10
    1. Implement class Log with static methods trace, debug, info, warn, error, fatal which will write log messages with appropriate level,
    2. 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.
  7. Check that unit tests are passing.

Tagi:
Izveidojis Valdis Vītoliņš 2017-07-21 11:30
Pēdējais mainījis Valdis Vītoliņš 2017-08-31 18:49
 
Xwiki Powered
Creative Commons Attribution 3.0 Unported License