Logging Setup

Last updated on 18th April 2024

The Simudyne SDK uses SLF4J (Simple Logging Facade for Java) for logging. SLF4J is an abstraction over common logging frameworks which means it can be used with most common Java logging frameworks (e.g. java.util.logging, logback, log4j), so that if you are already using one of these frameworks you can continue using this framework for your simulation so that your logging is consistent. To make use of this so that logging is not just done in the console, you will need to choose one of these frameworks to work with. For this example, we will be using Log4J.

Logging framework

If you want a specific framework, you will have to get the Slf4j binding for that framework and add it to your project. A list of bindings for different loggers can be found here.

However, log4j is already present as a dependency if you choose to use it.

Log4J CVE-2021-44228 Vulnerability

CVE-2021-44228 is a high severity vulnerability. It allows an attacker to execute arbitrary code by injecting attacker-controlled data into a logged message. Simudyne provide Log4J as a default framework, but is using version 1.x which is not affected by this vulnerabilty due to missing capabilities.

Please ensure that when you are adding a framework of Log4J 2.x that you are using the latest version 2.15.0+

You will then need to follow the specific instructions for configuring the logger you have chosen, which can be found on the websites of the specific frameworks. We will be showing you an example configuration using log4j. More detailed instructions on configuring log4j can be found here.

There are many possible ways of configuring log4j. We will be using a properties file. Create a file called log4j.properties, in the resources folder of your project. (Resources folders are created in the path projectName/src/main/resources).

log4j.properties

log4j.rootCategory=WARN, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.target=System.err
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{ISO8601} [%4p] [%t] %c - %m%n

log4j.logger.org.apache.hadoop.yarn=WARN
log4j.logger.org.apache.spark=WARN
log4j.logger.org.apache.spark.jetty=WARN

The log4j properties file is used to set the log level (so you can choose if you want to see all debug, info, warn or error messages), and other details about where to log to and with what format. We are only logging Error and Warn messages here and logging everything to the console.

We could log to a specific file with the following properties.

appender.file.type = File
appender.file.name = LOGFILE
appender.file.fileName=${filename}/propertieslogs.log
appender.file.layout.type=PatternLayout