If it is not explicitly told in following recipe, setting up services are described for Ubuntu 20.04 server, but applications are described for Xubuntu 20.04 workstation. If you use different Ubuntu version or Linux distribution, settings as well as content, names and places of configuration files may be different!
Got it.

Extra 10. Log4J

Main steps

  1. 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

  2. Recompile project and regenerate settings for eclipse:

    mvn clean compile eclipse:eclipse
  3. 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
    ...
  4. 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
    ...
  5. Review package jtm.extra10
    1. Implement class Log with methods trace, debug, info, warn, error, fatal which will write log messages with appropriate level,
    2. Implement method getLogger() which returns reference to used logger,
    3. 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.
  6. Check that unit tests are passing.

  

Created by Valdis Vītoliņš on 2017-07-21 08:30
Last modified by Valdis Vītoliņš on 2024-08-27 17:32
 
Xwiki Powered
Creative Commons Attribution 3.0 Unported License