在JavaSE中使用log4j来个性化控制日志,需求为:
1.将Warning以上级别的log记录到error log中。
2.将正常的Info信息记录到info log中。
3.将Debug信息记录到trace log中。
4.log信息尽可能详细,并且格式简明易读,关键内容包括:包名,类名,方法名,调用参数,异常信息,出错行数等等。
每种log最多保留10个日志文件,超过10个文件时最老的log被删除,实现滚动记录日志,日志文件命名格式为:“*_0.log”,"*_1.log"..."*_9.log"。单个log文件最大容量10MB。
log4j配置文件log4j.xml如下:
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false" threshold="null"> <!--This is final xml4j that confirms xml4j.dtd--> <appender class="com.log4j.test.TestLoggingRollingFileAppender" name="error"> <param name="threshold" value="WARN"/> <param name="File" value="/var/opt/test/log/test_error0_0.log"/> <param name="Append" value="true"/> <param name="maxFileSize" value="10MB"/> <param name="maxBackupIndex" value="10"/> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{yyyy-MM-dd-'T'HH:mm:ss.SSSZ} | ${host} | %x | %-15t | %-5p | %c | %m%n"/> </layout> <filter class="org.apache.log4j.varia.LevelRangeFilter"> <param name="LevelMin" value="WARN" /> <param name="LevelMax" value="FATAL" /> </filter> </appender> <appender class="com.log4j.test.TestLoggingRollingFileAppender" name="info"> <param name="threshold" value="INFO"/> <param name="File" value="/var/opt/test/log/test_info0_0.log"/> <param name="Append" value="true"/> <param name="maxFileSize" value="10MB"/> <param name="maxBackupIndex" value="10"/> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{yyyy-MM-dd-'T'HH:mm:ss.SSSZ} | ${host} | %x | %-15t | %-5p | %c | %m%n"/> </layout> <filter class="org.apache.log4j.varia.LevelRangeFilter"> <param name="LevelMin" value="INFO" /> <param name="LevelMax" value="INFO" /> </filter> </appender> <appender class="com.log4j.test.TestLoggingRollingFileAppender" name="trace"> <param name="threshold" value="DEBUG"/> <param name="File" value="/var/opt/test/log/test_trace0_0.log"/> <param name="Append" value="true"/> <param name="maxFileSize" value="10MB"/> <param name="maxBackupIndex" value="10"/> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{yyyy-MM-dd-'T'HH:mm:ss.SSSZ} | ${host} | %x | %-15t | %-5p | %c | %m%n"/> </layout> </appender> <logger additivity="true" name="com.log4j.test"> <level value="INFO"/> <appender-ref ref="error"/> <appender-ref ref="info"/> <appender-ref ref="trace"/> </logger> <root> <level value="INFO"/> <appender-ref ref="error"/> <appender-ref ref="info"/> <appender-ref ref="trace"/> </root> </log4j:configuration>
由于log4j默认的RollingFileAppender滚动记录日志功能文件名格式是:”*.log1“,”*.log2“等不满足需求中“*_0.log”,"*_1.log"..."*_9.log"格式,因此自定滚动日志记录类,实现自定义的log序号格式如下:
log4j.xml配置文件中的如下配置指定了log文件的存放目录和名称格式:
<param name="File" value="/var/opt/test/log/test_error0_0.log"/> <param name="File" value="/var/opt/test/log/test_info0_0.log"/> <param name="File" value="/var/opt/test/log/test_trace0_0.log"/>使用下面自定义的log文件滚动追加类,可以实现日志文件命名格式为:“*_0.log”,"*_1.log"..."*_9.log",并且log4j在记录日志时,当前记录的文件序号总是0_0:
class TestLoggingRollingFileAppender extends RollingFileAppender { private static final String FILE_NAME_POSTFIX = "_0.log"; public TestLoggingRollingFileAppender() { super(); } public TestLoggingRollingFileAppender(Layout layout, String filename, boolean append) throws IOException { super(layout, filename, append); } public TestLoggingRollingFileAppender(Layout layout, String filename) throws IOException { super(layout, filename); } //重写log4j RollingFileAppender的滚动记录log方法,log文件写满时,自动写新文件,log4j默认文件名规则为:*.log0, *.log1... //将log文件名规则修改为:*_0.log, *_1.log...该方法在每个日志文件写满时由log4j自动调用 public void rollOver() { File target; File file; if (qw != null) { long size = ((CountingQuietWriter) qw).getCount(); LogLog.debug("rolling over count=" + size); } LogLog.debug("maxBackupIndex=" + maxBackupIndex); boolean renameSucceeded = true; //配置的log文件数目大于0 if (maxBackupIndex > 0) { //获取最老的log文件名,序号最大的是最老的log文件 file = new File(fileName.substring(0, fileName.length() - FILE_NAME_POSTFIX.length()) + '_' + maxBackupIndex + ".log"); //删除最老的log文件,不一定存在最老文件,只有当log文件个数等于所配置的最大日志文件数时才删除最老日志文件 if (file.exists()) { renameSucceeded = file.delete(); } //将log文件名的序号加1,如*_2.log变为*_3.log,即最新的log会记录在序号最小的log文件中 for (int i = maxBackupIndex - 1; i >= 1 && renameSucceeded; i--) { //获取log文件名 file = new File(fileName.substring(0, fileName.length() - FILE_NAME_POSTFIX.length()) + '_' + i + ".log"); if (file.exists()) { //将log文件名的序号加1,最小的序号是2 target = new File(fileName.substring(0, fileName.length() - FILE_NAME_POSTFIX.length()) + '_' + (i + 1) + ".log"); LogLog.debug("Renaming file " + file + " to " + target); renameSucceeded = file.renameTo(target); } } //将最新写满的log文件命名为序号1 if (renameSucceeded) { //命名*_1.log文件 target = new File(fileName.substring(0, fileName.length() - FILE_NAME_POSTFIX.length()) + '_' + 1 + ".log"); this.closeFile(); file = new File(fileName); LogLog.debug("Renaming file " + file + " to " + target); renameSucceeded = file.renameTo(target); //log文件重命名失败,重新打开log文件继续向里面追加log if (!renameSucceeded) { try { this.setFile(fileName, true, bufferedIO, bufferSize); } catch (IOException e) { if (e instanceof InterruptedIOException) { Thread.currentThread().interrupt(); } LogLog.error("setFile(" + fileName + ", true) call failed.", e); } } } } //所有的log文件都改名成功 if (renameSucceeded) { try { //关闭所有滚动的log文件 this.setFile(fileName, false, bufferedIO, bufferSize); } catch (IOException e) { if (e instanceof InterruptedIOException) { Thread.currentThread().interrupt(); } LogLog.error("setFile(" + fileName + ", false) call failed.", e); } } } }上述日志文件滚动改名方法中,由于当前记录的日志文件序号总是从0开始,因此要依次顺序地日志文件序号加1,以实现序号滚动。
public class TestLog{ private static final String LOGGER_NAME = "com.log4j.test"; private static final String LOG_PATH = "/etc/opt/log4j_test/conf/" private static final String LOG_CONF_NAME = "log4j.xml"; private static final String ERROR_LOG_ENTRY_SEPARATOR = ", "; private static final String EMPTY_STRING = new String(); private static Logger log = null; //Initialize and load log configuration static{ try { //先从服务器上指定位置加载log4j配置文件 File file = new File(LOG_PATH + LOG_CONF_NAME); if(file.exists()){ DOMConfigurator.configure(Constants.RAC3GP_CONF_DIR + LOG_CONF_NAME); } //如果服务器上指定位置log4j配置文件不存在,加载jar包中的log4j配置文件 else{ InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(LOG_CONF_NAME); new DOMConfigurator().doConfigure(in, LogManager.getLoggerRepository());c } } catch (Exception e) { System.err.println("Loading log4j configuration failed, errorMsg:" + e.getMessage()); } log = Logger.getLogger(LOGGER_NAME); log.trace("Log initializing ..."); } public static void logAudit(String message) { log.info(message); } public static void logTrace(String message) { log.debug(message); } public static void logError(Object location, String method, String errorMessage) { logError(location, method, null, errorMessage); } public static void logError(Object location, String method, Exception exp, String errorMessage) { String className = location.getClass().getName(); if( errorMessage == null ) { errorMessage = ""; } String errorMsg = className + " " + method + ": " + errorMessage; StringBuffer msg = new StringBuffer(errorMsg); if (exp != null) { msg.append(Constants.NL).append(Constants.NL).append( getStackTrace(exp)); } else{ msg.append(getStackTrace(new BulkCmException())); } log.error(msg); } public static void logError(Object location, String method, Exception exp, String sessionId, String errorMessage) { String msg = sessionId + ERROR_LOG_ENTRY_SEPARATOR + errorMessage; logError(location, method, exp, msg); } private static String getStackTrace(Throwable t) { if (t == null) { return EMPTY_STRING; } try { StringWriter localSw = new StringWriter(); PrintWriter localPw = new PrintWriter(localSw); t.printStackTrace(localPw); return localSw.toString(); } catch (Throwable u) { return EMPTY_STRING; } } public static void logInfo(Object location, String message) { String className = location.getClass().getName(); String msg = className + ": " + message; log.info( msg ); } public static void logInfo(String message) { log.info(message); } }在应用程序中,直接调用该log类的日志记录方法即可实现自定义log记录需求。