Log Rotation
Last modified by David Avendasora on 2010/11/30 06:46
Log Rotation
Mike Kienenberger
I can't remember who gave me the basic starting point for this, but I think it may have been one of the Jonathans. You'll need to change the filenames to make it work under non-unix deployment systems.
You'll find it easier to copy out the source by going into Edit mode first.
public static void main(String argv[])
{
try
{
String baseOutputPath = null;
//look for a -WOOutputPath argument
for (int i=0; i<argv.length; i++) {
if ( argv[i].equals("-WOOutputPath") &&
!argv[i+1].equals("/dev/null") ) {
String outputPath = argv[i+1];
baseOutputPath = outputPath;
java.io.File outputFile = new java.io.File(outputPath);
if (outputFile.exists()) {
// move old file out of way to new location,
//name based on existing name with an appended timestamp
// Format the current time.
java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");
java.util.GregorianCalendar now = new java.util.GregorianCalendar();
String dateSuffix = ".bck-" + formatter.format(now.getTime());
System.err.println("new name: " + outputPath + dateSuffix);
java.io.File renamedFile = new java.io.File(outputPath + dateSuffix);
outputFile.renameTo(renamedFile);
}
break;
}
}
}
catch (Throwable e) {
//just so any Throwables generated in trying to do this don't
//keep our app from launching.
System.err.println("Ignoring: " + e);
}
WOApplication.main(argv, Application.class);
}
Log4j
Kieran Kelleher provides his configuration as an example that will log for each instance and rotate the file every day. Moreover, any errors get instantly emailed with subject line indicating which woa, host and port.
#########################################################################
# ERXLogger
#########################################################################
# Pattern codes:
# %-5p : Priority DEBUG, INFO, WARN, etc.
# %d(ISO08601 : Date and time
# %t : Thread
# %c : Logger name (usually a class name)
# %m%n : Log message arguments
# Available levels are DEBUG, INFO, WARN, ERROR, FATAL
# log4j appender and formatting
log4j.rootLogger=WARN, A2, myMail
log4j.loggerFactory=er.extensions.ERXLogger$Factory
# A1 is a ConsoleAppender
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=er.extensions.ERXPatternLayout
#log4j.appender.A1.layout.ConversionPattern=%-5p %d{HH:mm:ss} (%-20c:%L) %x -%m%n
log4j.appender.A1.layout.ConversionPattern=%r %-5p [%t] (%C{3}, %M, %L) - %n%m%n%n
# A2 is a DailyRollingFileAppender
log4j.appender.A2=org.apache.log4j.DailyRollingFileAppender
log4j.appender.A2.file=/var/log/webobjects_apps/YourAppName-@<at:var at:name="WOPort" />@.log
log4j.appender.A2.datePattern='.'yyyy-MM-dd
log4j.appender.A2.append=true
log4j.appender.A2.layout=er.extensions.ERXPatternLayout
log4j.appender.A2.layout.ConversionPattern=%-5p %d{ISO8601} [%t] (%c: %L) - %m%n
# myMail is the SMTPAppender
log4j.appender.myMail=org.apache.log4j.net.SMTPAppender
log4j.appender.myMail.Threshold=ERROR
log4j.appender.myMail.BufferSize=10
# log4j.appender.myMail.To=<email1>,<email2>
log4j.appender.myMail.From=<removed>
log4j.appender.myMail.SMTPHost=@@cheetah.smtpserver@@
log4j.appender.myMail.Subject=Log4J Error (@@build.app.bundle.name@@ host @<at:var at:name="host" />@ port @<at:var at:name="WOPort" />@)
log4j.appender.myMail.layout=er.extensions.ERXPatternLayout
# log4j.appender.myMail.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
log4j.appender.myMail.layout.ConversionPattern=%r %-5p %d{ISO8601}%n%n%V{t total/u used/f free}%n%n[%t] %n%n (%C, %M, %F:%L) - %n%n%m%n