You are here: Foswiki>Applications Web>AppHowToMain>AppHowToLogging (12 Jan 2023, AndreasSchaller)Edit Attach
The official page with information regarding Diagnostic Logging resided in the IN Web:

https://www-acc.gsi.de/wiki/IN/DiagnosticLogging

Usage

However, the APP group moved to log4j 2 and does not use the log4j 1.2 Socket Appender. Instead we use the logstash-gelf appender that outputs JSON directly in a Graylog compatible format.

Enabling log4j 2

To enable log4j 2 to load the appender you have to add the following maven dependency (please check for the latest version in artifacts.acc.gsi.de ):
<dependency>
  <groupId>de.gsi.aco</groupId>
  <artifactId>aco-logging</artifactId>
  <version>18.0.0-SNAPSHOT</version>
</dependency>

After that you should already be able to use the logging system since a default configuration is also provided inside aco-logging.

Logger Configuration (Level and Format)

By default the log4j2.xml is configured to log very detailed (DEBUG level) information to the console and fewer (INFO level) to the log server (https://graylog.acc.gsi.de/).

If you want to use you own configuration you can specify it during program start:

-Dlog4j.configurationFile=path/to/log4j2.xml

If the application uses our regular parent and the dependency aco-logging the relative path from the resources directory can be used. A list of the standard configurations can be seen here: https://git.acc.gsi.de/fcc-commons/aco-logging/src/branch/master/src/main/resources

-Dlog4j.configurationFile=log4j2-dev.xml

When creating your own configuration and if you do not want to start from scratch and instead modify the XML provided by APP, it can be found here

https://git.acc.gsi.de/fcc-commons/aco-logging/src/branch/master/src/main/resources/log4j2.template.xml

Example

Here is an example how to use the different APIs available (we strongly suggest to use the Slf4j API):
class LoggingTest {

    // Getting an Slf4J logger
    private static final org.slf4j.Logger LOGGER_A = org.slf4j.LoggerFactory.getLogger(LoggingTest.class);

    // Getting an Apache commons-logging logger 
    private static final org.apache.commons.logging.Log LOGGER_B = org.apache.commons.logging.LogFactory
            .getLog(LoggingTest.class);
    // Getting an Log4j 2 logger
    private static final org.apache.logging.log4j.Logger LOGGER_C = org.apache.logging.log4j.LogManager
            .getLogger("HelloWorld");

    {
        LOGGER_A.error("This is a logging test 1");
        LOGGER_A.warn("This is a logging test 2");
        LOGGER_A.info("This is a logging test 3");
        LOGGER_A.debug("This is a logging test 4");

        LOGGER_B.error("This is a logging test 1");
        LOGGER_B.warn("This is a logging test 2");
        LOGGER_B.info("This is a logging test 3");
        LOGGER_B.debug("This is a logging test 4");

        LOGGER_C.error("This is a logging test 1");
        LOGGER_C.warn("This is a logging test 2");
        LOGGER_C.info("This is a logging test 3");
        LOGGER_C.debug("This is a logging test 4");
    }

    public static void main(String[] args) {
        LoggingTest test = new LoggingTest();
    }
}

Regardless which API you use the message will be forwarded to log4j 2 and the formatting from the log4j.xml will be used.
This is what the output looks like:
ERROR [07 Jan 2015 10:34:13,052] (LsaTester.java) - This is a logging test 1
 WARN [07 Jan 2015 10:34:13,056] (LsaTester.java) - This is a logging test 2
 INFO [07 Jan 2015 10:34:13,056] (LsaTester.java) - This is a logging test 3
DEBUG [07 Jan 2015 10:34:13,056] (LsaTester.java) - This is a logging test 4
ERROR [07 Jan 2015 10:34:13,057] (LsaTester.java) - This is a logging test 1
 WARN [07 Jan 2015 10:34:13,057] (LsaTester.java) - This is a logging test 2
 INFO [07 Jan 2015 10:34:13,057] (LsaTester.java) - This is a logging test 3
DEBUG [07 Jan 2015 10:34:13,057] (LsaTester.java) - This is a logging test 4
ERROR [07 Jan 2015 10:34:13,057] (LsaTester.java) - This is a logging test 1
 WARN [07 Jan 2015 10:34:13,058] (LsaTester.java) - This is a logging test 2
 INFO [07 Jan 2015 10:34:13,058] (LsaTester.java) - This is a logging test 3
DEBUG [07 Jan 2015 10:34:13,058] (LsaTester.java) - This is a logging test 4

Placeholders

You can add placeholder like {} to the message easily. Using this method the resulting string is only build on execution and might save a few resources. As always, Object.toString is called if you pass objects, nulls are fine.

LOGGER.debug("I want to tell you about {} and {}", 3.14, Runtime.getRuntime().availableProcessors());

Output:

DEBUG 20-07-31 09:43:05,451 main JavaShell.main  I want to tell you about 3.14 and 20

Maven

Maven uses a separate log4j2 configuration showing only warnings and errors. If you want to see more detailed log output in your maven build process, you can add the command line option

-Dlog4j.configurationFile=path/to/log4j2.xml

-- RaphaelMueller - 07 Jan 2015
Topic revision: r15 - 12 Jan 2023, AndreasSchaller
This site is powered by FoswikiCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Foswiki? Send feedback