1. 程式人生 > >javaEE開發之使用log4j記錄日誌

javaEE開發之使用log4j記錄日誌

web開發中,當專案開發完畢後,可能會有些bug存在,程式出現bug後,如何快速定位並修改,就需要對這些程式的執行過程做一個記錄,並儲存起來,以便維護。

常用的是使用log4j框架,通過自定義日誌級別,然後在程式碼中打下日誌。

首先,寫一個工具類LogUtil:

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.net.SyslogAppender;

/**
 * 自定義級別日誌定義log方法
 */
public class LogUtil {

	// 自定義等級
	private static final Level CUSTOM_LEVEL = new CustomLevel(25000, "CUSTOM",
			SyslogAppender.LOG_LOCAL0);

	// 單例模式
	private static LogUtil logUtil = new LogUtil();

	private LogUtil() {
	}

	private static Logger loggerCustom = null;

	public static LogUtil getLogUtil(Object message) {
		loggerCustom = Logger.getLogger(message.toString().substring(6));
		return logUtil;
	}

	public void customLog(Object message) {
		loggerCustom.log(CUSTOM_LEVEL, message);
	}
}
然後,自定義一個日誌級別過濾器CustomLogFilter:
import org.apache.log4j.spi.Filter;
import org.apache.log4j.spi.LoggingEvent;

/**
 * 自定義日誌級別過濾器
 * 
 */
public class CustomLogFilter extends Filter {
	boolean acceptOnMatch;
	int levelMin;
	int levelMax;

	public int getLevelMin() {
		return levelMin;
	}

	public void setLevelMin(int levelMin) {
		this.levelMin = levelMin;
	}

	public int getLevelMax() {
		return levelMax;
	}

	public void setLevelMax(int levelMax) {
		this.levelMax = levelMax;
	}

	public boolean isAcceptOnMatch() {
		return acceptOnMatch;
	}

	public void setAcceptOnMatch(boolean acceptOnMatch) {
		this.acceptOnMatch = acceptOnMatch;
	}

	@Override
	public int decide(LoggingEvent lgEvent) {
		int inputLevel = lgEvent.getLevel().toInt();
		if (inputLevel >= levelMin && inputLevel <= levelMax) {
			return 0;
		}
		return -1;
	}

}
之後,自定義一個日誌級別類,CustomLevel:
import org.apache.log4j.Level;

/**
 * 
 * 自定義Custom級別
 * 
 */
public class CustomLevel extends Level {
	private static final long serialVersionUID = 1L;

	public CustomLevel(int level, String levelStr, int syslogEquivalent) {
		super(level, levelStr, syslogEquivalent);
	}
}
最後,在log4j.xml中配置如下資訊:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//LOGGER" "log4j.dtd">

<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
	<!-- 輸出到控制檯 -->
	<appender name="myConsole" class="org.apache.log4j.ConsoleAppender">
		<!-- 輸出格式 -->
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="[%-5p] [%d{yyyy年MM月dd日  HH:mm:ss,SSS\}] %c - %m%n" />
		</layout>
		<!--過濾器設定輸出的級別 -->
		<filter class="com.telek.utils.CustomLogFilter">
			<param name="levelMin" value="25000" />
			<param name="levelMax" value="25000" />
			<param name="AcceptOnMatch" value="true" />
		</filter>
	</appender>
	<!-- 輸出到指定檔案 -->
	<appender name="Info" class="org.apache.log4j.RollingFileAppender">
		<param name="File"
			value="E:/WorkspaceForJava/UsedMallMinaServer/WebRoot/log/info.log" /><!-- 
			設定日誌輸出檔名 -->
		<!-- 設定是否在重新啟動服務時,在原有日誌的基礎新增新日誌 -->
		<param name="Append" value="true" />
		<param name="MaxBackupIndex" value="10" />
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="[%-5p] [%d{HH:mm:ss}] %c - %m%n" />
		</layout>
		<!-- 過濾級別為只有info資訊 -->
		<filter class="org.apache.log4j.varia.LevelRangeFilter">
			<param name="levelMin" value="INFO" />
			<param name="levelMax" value="INFO" />
			<param name="AcceptOnMatch" value="true" />
		</filter>
	</appender>

	<appender name="Debug" class="org.apache.log4j.RollingFileAppender">
		<param name="File"
			value="E:/WorkspaceForJava/UsedMallMinaServer/WebRoot/log/debug.log" /><!-- 
			設定日誌輸出檔名 -->
		<!-- 設定是否在重新啟動服務時,在原有日誌的基礎新增新日誌 -->
		<param name="Append" value="true" />
		<param name="MaxBackupIndex" value="10" />
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="[%-5p] [%d{yyyy年MM月dd日  HH:mm:ss}] %c - %m%n" />
		</layout>
		<filter class="org.apache.log4j.varia.LevelRangeFilter">
			<param name="levelMin" value="DEBUG" />
			<param name="levelMax" value="DEBUG" />
			<param name="AcceptOnMatch" value="true" />
		</filter>
	</appender>

	<appender name="Warn" class="org.apache.log4j.RollingFileAppender">
		<param name="File"
			value="E:/WorkspaceForJava/UsedMallMinaServer/WebRoot/log/warn.log" /><!-- 
			設定日誌輸出檔名 -->
		<!-- 設定是否在重新啟動服務時,在原有日誌的基礎新增新日誌 -->
		<param name="Append" value="true" />
		<param name="MaxBackupIndex" value="10" />
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="[%-5p] [%d{yyyy年MM月dd日  HH:mm:ss}] %c - %m%n" />
		</layout>
		<filter class="org.apache.log4j.varia.LevelRangeFilter">
			<param name="levelMin" value="WARN" />
			<param name="levelMax" value="WARN" />
			<param name="AcceptOnMatch" value="true" />
		</filter>
	</appender>

	<appender name="Error" class="org.apache.log4j.RollingFileAppender">
		<param name="File"
			value="E:/WorkspaceForJava/UsedMallMinaServer/WebRoot/log/error.log" /><!-- 
			設定日誌輸出檔名 -->
		<!-- 設定是否在重新啟動服務時,在原有日誌的基礎新增新日誌 -->
		<param name="Append" value="true" />
		<param name="MaxBackupIndex" value="10" />
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="[%-5p] [%d{yyyy年MM月dd日  HH:mm:ss}] %c - %m%n" />
		</layout>
		<filter class="org.apache.log4j.varia.LevelRangeFilter">
			<param name="levelMin" value="ERROR" />
			<param name="levelMax" value="ERROR" />
			<param name="AcceptOnMatch" value="true" />
		</filter>
	</appender>
	<appender name="MinaCustom" class="org.apache.log4j.RollingFileAppender">
		<param name="File"
			value="E:/WorkspaceForJava/UsedMallMinaServer/WebRoot/log/custom.log" />
		<!--設定日誌輸出檔名 -->
		<!-- 設定是否在重新啟動服務時,在原有日誌的基礎新增新日誌 -->
		<param name="Append" value="true" />
		<param name="MaxBackupIndex" value="10" />
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="[%-5p] [%d{yyyy年MM月dd日  HH:mm:ss,SSS\}] - %m%n" />
		</layout>
		<filter class="com.telek.utils.CustomLogFilter">
			<param name="levelMin" value="25000" />
			<param name="levelMax" value="25000" />
			<param name="AcceptOnMatch" value="true" />
		</filter>
	</appender>
	<root>
		<level value="ALL" />
		<appender-ref ref="myConsole" />
		<appender-ref ref="Info" />
		<appender-ref ref="Debug" />
		<appender-ref ref="Warn" />
		<appender-ref ref="Error" />
		<appender-ref ref="MinaCustom" />
	</root>
</log4j:configuration>