HeaderPatternLayout
See attachments for current snapshot versions. Still in early beta. Requires Debug String Utils and optionally Container Detector. Custom Layout for Log4j 1.2 and LogBack. Drop-in replacement for PatternLayout. Mostly inspired by http://juliusdavies.ca/logging.html (search for "A Log Rotation Best Practice"). The general idea is to write a line on top (and optionally end) of every log file with basic information about the current environment, like memory, Java runtime and VM info, operating system, etc.Properties
All properties except ConversionPattern / Pattern are optional. FirstLine, SecondLine - custom text provided by user; can contain system properties as usual ( ${propName} syntax}. ConversionPattern (Log4j), Pattern (LogBack) - same as in PatternLayout. HeaderPattern - additional pattern to use only for header/footer lines. Follows the same syntax as ConversionPattern / Pattern. WriteFooter - whether or not to write a footer. See known issues.Sample usage
Compact output
datestamp=yyyy-MM-dd/HH:mm:ss.SSS/zzz
pattern=%d{${datestamp}} %p [%t] %c (%F:%L) - %m%n
log4j.rootLogger=DEBUG, SysOut, TestFile
log4j.appender.SysOut=org.apache.log4j.ConsoleAppender
log4j.appender.SysOut.layout=org.apache.log4j.PatternLayout
log4j.appender.SysOut.layout.ConversionPattern=${pattern}
log4j.appender.TestFile=org.apache.log4j.FileAppender
log4j.appender.TestFile.File=target/test-log4j-compact.txt
log4j.appender.TestFile.Append=false
log4j.appender.TestFile.layout=net.lilianne_blaze.logging.log4j.HeaderPatternLayout
log4j.appender.TestFile.layout.ConversionPattern=${pattern}
log4j.appender.TestFile.layout.MultiLine=false
log4j.appender.TestFile.layout.WriteFooter=false
log4j.appender.TestFile.layout.Fractions=false
log4j.appender.TestFile.layout.FirstLine=app="my app"
log4j.appender.TestFile.layout.SecondLine=userHome=${user.home}2008-07-01/06:22:00.150/CEST INFO [main] na (?:?) - app="my app" userHome=C:\Users\LBlaze UPTIME=[0:0.219] RUNTIME=[Java(TM) SE Runtime Environment 1.6.0_10-beta-b25] VM=[Java HotSpot(TM) Client VM 11.0-b12] SYSTEM=[Windows XP 5.1 x86] MemHeap=[621KiB(12%/1%)/5MiB(8%)/64MiB] MemNonHeap=[13MiB(40%/11%)/33MiB(28%)/116MiB] ServletContainer=[none] 2008-07-01/06:22:00.150/CEST INFO [main] net.lilianne_blaze.logging.log4j.HeaderPatternLayoutTest (HeaderPatternLayoutTest.java:46) - Testing compact log4j output.
Known issues
File footer is not guaranteed to get written. It's not specific to HeaderPatternLayout, just a typical limitation of logging frameworks.Legalese
It is available under Apache Licence 2.0 and WTFPL, with the following exception: if you choose to use any non-final version (i.e. one that is marked as "snapshot", "rc" or "beta") please (i.e. it is a request, not a legal requirement) repack it to another package using JarJar or similar software. See JarJar homepage, JarJar: solves class loading conflicts
Version 1.1 last modified by XWikiGuest on 08/09/2008 at 23:21

Comments: 10