My FNZ Studio Version

System Maintenance

Latest Update: 16.11.2023 |
Recipe
2022.2
2022.1
11
10
9
8
7
6

Problem

How do I add the user ID to every line in a log4j log file?

Solution

Here's the short answer:

  1. Create a file [DataHome]/conf/log4j-additional.properties

  2. Add the following lines to this file:

    log4j.appender.stdlog.layout.ConversionPattern = %d | %-20X{userId} | %-5p %c - %m%n
    log4j.appender.dbglog.layout.ConversionPattern = %d | %-20X{userId} | %-5p %c - %m%n
    log4j.appender.errlog.layout.ConversionPattern = %d | %-20X{userId} | %-5p %c - %m%n
    
  3. Restart Appway

2 Explanation

Appway uses log4j as logging framework. By default, every entry in the log file contains the following information:

  • date and time
  • log level: DEBUG, INFO, WARN, ERROR or FATAL
  • logger name (component which has generated the log message)
  • log message

3 log4j Configuration

What information is written to the log may be configured using "layout patterns". The default pattern generating log entries as described above looks like this:

%d %-5p %c - %m%n
  • %d is a placeholder for a time stamp including the current date and time.
  • %-5p will be replaced with the log level, padded on the right to have a length of at least 5 characters.
  • %c stands for the logger name
  • %m stands for the actual log message provided to the logger
  • %n adds a new-line character at the end

More information on layout patterns and supported placeholders can be found here:
http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html

A special feature of log4j is the "mapped diagnostic context" (MDC). This is a map of key/value pairs which may be used by applications to inject custom data into log entries. By default, Appway adds the following information to the MDC:

  • userId — ID of the current user
  • clientIP — IP address of the client making an HTTP request
  • httpSessionId — current HTTP session ID
  • requestId — ID of the current HTTP request (incremented by one for every request)

To reference an MDC value in a log entry, the placeholder "%X{key}" may be used. The following pattern includes the user id immediatley after the time stamp and adds white-spaces to the end of it to have a fixed-size column with:

%d | %-20X{userId} | %-5p %c - %m%n

In order to override the default layout pattern for a log4j appender, the configuration file [DataHome]/conf/log4j-additional.properties may be used. If this file exists, it is loaded at start-up and used to override properties defined in the default log4j configuration.

The default log4j configuration of Appway defines three appenders named "stdlog", "dbglog" and "errlog". The first appender writes its messages to the console, the other two write their messages into the log files "debug.log" and "error.log". Every appender has its own layout pattern. To override the pattern for the "dbglog" appender, the following configuration may be used:

log4j.appender.dbglog.layout.ConversionPattern = %d | %-20X{userId} | %-5p %c - %m%n

IMPORTANT! Appway must be restarted in order for this configuration to have an effect.
© 2026 Appway AG. All Rights Reserved