Log4j2 provides a new parameter for PatternLayout:
Parameter name: header
Description: the optional header string to include at the top of each log file
With the following log4j2.xml configuration:
The output will be as follows:
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:
The corresponding output:
To learn more about property substitution go here.