一. JBoss的日志
jboss采用的是Log4J的引擎来记录日志,我们可以通过配置 conf 目录下的 log4j.xml来配置日志。不过很遗憾的是Jboss 没有那种每天一个文件的日志功能,而Tomcat是有的。
分析后发现,Tomcat才用的是扩展于Log4j的一个日志记录器,而JBoss则没有。
<Logger className="org.apache.catalina.logger.FileLogger" debug="0" directory="logs" prefix="localhost_examples_log." suffix=".txt" timestamp="true" verbosity="1"/>
二 实现每天日志功能
借鉴于这一思想,我扩展于JBoss的日志器实现了和Tomcat一样的日志记录器,能够每天形成一个日志文件。
/**
* Filename:DatedFileAppender.java
* Created by: 毛翔
* Created on: 2004-12-30 10:11:45
* Last modified by:$Author$
* Last modified on: $Date$
* Revision: $Revision$
*/
package common.logging;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.log4j.spi.LoggingEvent;
import org.jboss.logging.appender.FileAppender;
public class DatedFileAppender extends
org.apache.log4j.DailyRollingFileAppender {
private static String lastDate;
private static String originalFile;
public void setFile(final String filename) {
//save the original file
originalFile = filename;
// add timestamp
final String newFilename = addTimeStamp(filename);
FileAppender.Helper.makePath(newFilename);
//set lastDate
lastDate = getTimeStamp();
super.setFile(newFilename);
}
/**
* @see org.apache.log4j.WriterAppender#subAppend(org.apache.log4j.spi.LoggingEvent)
*/
protected void subAppend(LoggingEvent event) {
String current = getTimeStamp();
if (!current.equals(lastDate)) {
//if date has changed,need to log to new file
setFile(originalFile);
super.activateOptions();
}
super.subAppend(event);
}
protected String addTimeStamp(String filename) {
String ret = filename;
String dot = ".";
int i = filename.lastIndexOf(dot);
if (i > -1) {
String prefix = filename.substring(0, i);
String suffix = filename.substring(i);
ret = prefix + getTimeStamp() + suffix;
} else {
ret = ret + getTimeStamp();
}
return ret;
}
/**
* get the date
* @return yyyyMMdd example: 20041230
*/
private String getTimeStamp() {
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
return df.format(new Date());
}
/**
* simple test
* @param args
*/
public static void main(String[] args) {
DatedFileAppender test = new DatedFileAppender();
System.out.println(test.addTimeStamp("server.log"));
System.out.println(test.addTimeStamp("server"));
System.out.println(test.addTimeStamp("server.test.log"));
}
}
三。使用实例
在Jboss中conf 下 修改log4j.xml.
<!-- log for my application -->
<appender name="mylog" class="common.logging.DatedFileAppender">
<errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
<param name="File" value="${jboss.server.home.dir}/log/mylog/education.log"/>
<param name="Append" value="true"/>
<!-- Rollover at midnight each day -->
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<!-- Rollover at the top of each hour
<param name="DatePattern" value="'.'yyyy-MM-dd-HH"/>
-->
<layout class="org.apache.log4j.PatternLayout">
<!-- The default pattern: Date Priority [Category] Message\n -->
<param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>
<!-- The full pattern: Date MS Priority [Category] (Thread:NDC) Message\n
<param name="ConversionPattern" value="%d %-5r %-5p [%c] (%t:%x) %m%n"/>
-->
</layout>
</appender>
四。效果