Warning

If it is not explicitly told in following recipe, setting up services are described for Ubuntu 24.04 server, but applications are described for Xubuntu 24.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.

Log4J

Prev
UML and forward-reverse engineering
Next
JPA

Main steps

  1. If necessary, review presentation: Log4J
  2. Add dependency on log4j in pom.xml file:

        <dependencies>
        ...
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-core</artifactId>
                <version>2.23.1</version>
            </dependency>
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-api</artifactId>
                <version>2.23.1</version>
            </dependency>
        ...
        </dependencies>

    where ... are some other existing settings

  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/log4j2.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.:

    ...
    # Direct log messages to file
    logger.jtm.name = jtm.extra05.Log
    logger.jtm.level = info
    logger.jtm.additivity = false
    logger.jtm.appenderRefs = file, stdout
    logger.jtm.appenderRef.file.ref = file
    logger.jtm.appenderRef.stdout.ref = stdout
    ...
  5. Set category filter settings in log4j2.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.:

    ...
    logger.jtmExt.name = jtm.extra05.Log.ExtLog
    logger.jtmExt.level = trace
    logger.jtmExt.additivity = false
    logger.jtmExt.appenderRefs = file, stdout
    logger.jtmExt.appenderRef.file.ref = file
    logger.jtmExt.appenderRef.stdout.ref = stdout
    ...
  6. Review package jtm.extra05
  7. Declare and initialize class logger in Log class:

    private static final Logger log = LogManager.getLogger(Log.class);
  8. Implement static methods to log trace, debug, info, warn, error, fatal which will write log messages with appropriate level, using the class logger.
  9. Implement method getLogger() which returns reference to used logger
  10. In nested class ExtLog declare and initialize logger:

    static final Logger log = LogManager.getLogger(ExtLog.class);
  11. In internal class ExtLog implement static methods trace, debug, info, warn, error, fatal which will write log messages with appropriate level.
  12. Check that unit tests are passing

Prev
UML and forward-reverse engineering
Next
JPA

 
 

Created by Valdis Vītoliņš on 2026-03-19 12:07
Last modified by Valdis Vītoliņš on 2026-03-19 13:54
XWiki Powered
Creative Commons Attribution 3.0 Unported License