Parameter name: header
Type: String
Description: the optional header string to include at the top of each log file
With the following log4j2.xml configuration:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <?xml version="1.0" encoding="UTF-8"?> <Configuration status="WARN" monitorInterval="20"> <Appenders> <RollingRandomAccessFile name="file" fileName="logs/app.log" filePattern="logs/app-%d_%i.log.gz"> <PatternLayout> <header> Version: ${env:APP_RELEASE_VERSION} </header> <pattern>%message%n</pattern> </PatternLayout> <Policies> <OnStartupTriggeringPolicy/> </Policies> </RollingRandomAccessFile> </Appenders> <Loggers> <Root level="debug"> <AppenderRef ref="file"/> </Root> </Loggers> </Configuration> |
The output will be as follows:
Version: 1.2.0log_message_1
log_message_2
log_message_3
The problem is that the end of the header and the beginning of the log messages are not separated by a new line. But it can be solved by using "line.separator" - a Java system property:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <?xml version="1.0" encoding="UTF-8"?> <Configuration status="WARN" monitorInterval="20"> <Appenders> <RollingRandomAccessFile name="file" fileName="logs/app.log" filePattern="logs/app-%d_%i.log.gz"> <PatternLayout> <header> ${env:APP_RELEASE_VERSION}${sys:line.separator} </header> <pattern>%message%n</pattern> </PatternLayout> <Policies> <OnStartupTriggeringPolicy/> </Policies> </RollingRandomAccessFile> </Appenders> <Loggers> <Root level="debug"> <AppenderRef ref="file"/> </Root> </Loggers> </Configuration> |
The corresponding output:
Version: 1.2.0
log_message_1
log_message_2
log_message_3
To learn more about property substitution go here.
No comments:
Post a Comment