How do I add the user ID to every line in a Log4j log file?
Here's the short answer:
-
Create a file called [DataHome]/conf/log4j2-additional.properties
-
Add the following lines to this file:
appender.stdlog.layout.pattern = %d{yyyy-MM-dd'T'HH:mm:ss.SSSZ} | %-20X{userId} | %-5p [%t] %c - %m%n%throwable
appender.applog.layout.pattern = %d{yyyy-MM-dd'T'HH:mm:ss.SSSZ} | %-20X{userId} | %-5p [%t] %c - %m%n%throwable
-
Restart FNZ Studio
FNZ Studio uses Log4j 2.x as a logging framework (see Migrating Log4j Configuration guide). By default, every entry in the log file contains the following information:
- date and time
- log level: DEBUG, INFO, WARN, ERROR or FATAL
- thread name
- logger name (component which has generated the log message)
- log message
The type of information written to the log may be configured using "layout patterns". The default pattern generating the log entries described above looks as follows:
%d{yyyy-MM-dd'T'HH:mm:ss.SSSZ} %-5p [%t] %c - %m%n%throwable
where:
%d{yyyy-MM-dd'T'HH:mm:ss.SSSZ} is a placeholder for a timestamp, including the current date and time.%-5p will be replaced by the log level, padded on the right to have a length of at least 5 characters.%t will include the name of the current Java thread.%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.%throwable will include the Java exception message and stack trace, if an exception has been logged.
More information on layout patterns and supported placeholders can be found at the following
link.
A special feature of Log4j is the "thread context". This is a map of key/value pairs which may be used by applications to inject custom data into log entries. By default, FNZ Studio adds the following information to the thread context:
userId — ID of the current userclientIP — IP address of the client making an HTTP requesthttpSessionId — Current HTTP session IDrequestId — ID of the current HTTP request (incremented by one for every request)locale — Current translation languageversionFilter — Current version filter
To reference a thread context value in a log entry, the placeholder %X{key} may be used. The following pattern includes the user Id immediately after the timestamp and adds white-spaces to the end of it to have a fixed-size column width:
%d{yyyy-MM-dd'T'HH:mm:ss.SSSZ} | %-20X{userId} | %-5p [%t] %c - %m%n%throwable
In order to override the default layout pattern for a Log4j appender, the configuration file [DataHome]/conf/log4j2-additional.properties may be used. If this file exists, it is loaded at startup and used to override properties defined in the default Log4j configuration.
The default Log4j configuration of FNZ Studio defines two main appenders:
stdlog writes its messages to the consoleapplog writes its messages into the log file application.log
Each appender has its own layout pattern. To override the pattern for the applog appender, the following configuration may be used:
appender.applog.layout.pattern = %d{yyyy-MM-dd'T'HH:mm:ss.SSSZ} | %-20X{userId} | %-5p [%t] %c - %m%n%throwable
Important note: FNZ Studio must be restarted in order for this configuration to have effect.