Using log4j2 (beta9) with java 1.7.
My complete log4j2.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Properties>
<Property name="projectPrefix">Tts</Property>
<Property name="rawPattern">%d %-5p [%t] %C{2} (%F:%L) - %m%n</Property>
<Property name="coloredPattern">%d %highlight{%-5p}{FATAL=bright red, ERROR=red, WARN=yellow, INFO=cyan, DEBUG=green, TRACE=bright blue} %style{[%t] %C{2} (%F:%L) -}{bright,black} %m%n</Property>
<Property name="fileName">Log/${projectPrefix}.log</Property>
<Property name="filePattern">Log/${projectPrefix}-%i.log</Property>
</Properties>
<Appenders>
<Console name="Stdout" target="SYSTEM_OUT">
<PatternLayout pattern="${coloredPattern}"/>
</Console>
<RollingFile name="Logfile" fileName="${fileName}" filePattern="${filePattern}">
<PatternLayout pattern="${rawPattern}"/>
<Policies>
<SizeBasedTriggeringPolicy size="16 MB"/>
</Policies>
<DefaultRolloverStrategy fileIndex="min" max="16"/>
</RollingFile>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Stdout"/>
<AppenderRef ref="Logfile"/>
</Root>
</Loggers>
</Configuration>
I want to add some custom info at the top of each logfile, like the version string of my application, the application uptime and the system uptime. And even writing some »bye, bye / eof« to the bottom of the just closed logfile would also be fine.
Is there something like a hook or callback to get notified when the RollingFileAppander has created a new file, so that I can put my things at first into these new logfile (or any other suggestion)?
New solution available
Some time has passed since this question was asked, and now, when I wanted to do the same I have found out that it is possible to solve without messing with factories, but it was not easy to find. The log4j2 team have made it possible to do this by configuration. I hope my post will be of use and save time for someone else.
They have hidden this feature in the PatternLayout element. Not where I first looked, but why complain when it works?
This is my configuration (pay attention to header and footer, and the properties they use):
As you can see I have included a timestamp, and a property, that includes a system property. Log4j2 can show many different kinds of properties, with this you can do a lot of things of what you asked for.
The log file looks like this:
Do you need more custom information? - try to put that information in properties, system properties, or something else that log4j2 can read.
See Property Substitution in log4j2 for details in what kind of properties you can have.
Comments about the configuration
Ok, there is a working solution for this problem by extending DefaultRolloverStrategy like described here. But
To let log4j2 call our factory method, the root tag of log4j2.xml must be attributed with the package of our class, e.g.:
and within our own RolloverStrategy we have to deal with
@Plugin
and@PluginFactory
as described here.Finally here my complete log4j2.xml (you don't need all that properties - that's just the way how I like to configure my logging):
And here MyRolloverStrategy.java:
Solving this requirement might be easier in future versions of log4j2, if my posted feature request would implemented.
Currently there is no callback hook for rollovers. May I suggest raising this as a feature request in the log4j2 issue tracker?