Logging

In Polypheny, we value transparency and traceability in our code execution, which we achieve through rigorous logging. This guideline outlines the best practices to follow when incorporating logging into your code within Polypheny.

Log Levels

We use SLF4J as our logging facade, which provides us with six levels of logging severity:

  1. Trace (the least serious)
  2. Debug
  3. Info
  4. Warn
  5. Error
  6. Fatal (the most serious)

Use these levels appropriately to categorize the importance and severity of your log messages.

Logger Annotation

For efficient logger field creation, we utilize Project Lombok. It’s only required to add the @Slf4j Annotation to the class. This is demonstrated in the following example:

@Slf4j
public class LogExample {
  
    public void foo() {
        log.error( "A sophisticated log message" );
    }
  
}

Logging Exceptions

When logging exceptions, always log the entire exception object and not just the message. This practice ensures that the stacktrace is included in the log output, facilitating better debugging and issue resolution. Here’s an example:

log.error( "An exception occurred!", new Exception( "Custom exception" ) );

Avoid String Concatenation

In logger calls, avoid concatenating strings directly. Instead, use parameterized logger calls as shown below:

log.info( "{} started and is listening on port {}.", name, getPort() );

This approach avoids unnecessary string concatenation and improves the readability of your code.

Guards

To optimize logging performance, it’s essential to put “guards” in front of the lower log levels. For example:

if ( log.isTraceEnabled() ) {
    Info info = collectInformation();
    log.trace( "... {}", info.getSomething() );
}

These guards are particularly important if additional method calls are required to prepare the log message. They help avoid unnecessary concatenations and expression evaluations, enhancing the performance of your application.

© Polypheny GmbH. All Rights Reserved.