1. 程式人生 > >基於SSM框架的日誌管理

基於SSM框架的日誌管理

本部落格介紹log4j的日誌管理配置

log4j.peroperties:

在D盤建立一個logs的資料夾來存放日誌

### set log levels ### 
log4j.rootLogger = info , Console , D
#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=INFO
log4j.logger.java.sql.Statement=INFO
log4j.logger.java.sql.PreparedStatement=INFO
#output2file
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender 
log4j.appender.D.File = D\:/logs/log.log 
log4j.appender.D.Append = true 
log4j.appender.D.Threshold = INFO \#\# \u8F93\u51FAinfo\u7EA7\u522B\u4EE5\u4E0A\u7684\u65E5\u5FD7 
log4j.appender.D.layout = org.apache.log4j.PatternLayout 
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH\:mm\:ss} [ %t\:%r ] - [ %p ] %m%n 
 

寫個日誌管理類:
package com.appweb.core.logger;


public class Logger {

	private org.apache.log4j.Logger logger;

	
	/**
	 * 構造方法,初始化Log4j的日誌物件
	 */
	private Logger(org.apache.log4j.Logger log4jLogger) {
		logger = log4jLogger;
	}

	/**
	 * 獲取構造器,根據類初始化Logger物件
	 * 
	 * @param Class
	 *            Class物件
	 * @return Logger物件
	 */
	public static Logger getLogger(Class classObject) {
		return new Logger(org.apache.log4j.Logger.getLogger(classObject));
	}

	/**
	 * 獲取構造器,根據類名初始化Logger物件
	 * 
	 * @param String
	 *            類名字串
	 * @return Logger物件
	 */
	public static Logger getLogger(String loggerName) {
		return new Logger(org.apache.log4j.Logger.getLogger(loggerName));
	}

	public void debug(Object object) {
		logger.debug(object);
	}

	public void debug(Object object, Throwable e) {
		logger.debug(object, e);
	}

	public void info(Object object) {
		logger.info(object);
	}

	public void info(Object object, Throwable e) {
		logger.info(object, e);
	}

	public void warn(Object object) {
		logger.warn(object);
	}

	public void warn(Object object, Throwable e) {
		logger.warn(object, e);
	}

	public void error(Object object) {
		logger.error(object);
	}

	public void error(Object object, Throwable e) {
		logger.error(object, e);
	}

	public void fatal(Object object) {
		logger.fatal(object);
	}

	public String getName() {
		return logger.getName();
	}

	public org.apache.log4j.Logger getLog4jLogger() {
		return logger;
	}

	public boolean equals(Logger newLogger) {
		return logger.equals(newLogger.getLog4jLogger());
	}
}

可以寫個BaseController的控制類

然後將log的管理類加進來,實現日誌管理的重複使用

package com.appweb.core.base;


import javax.servlet.http.HttpServletRequest;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.appweb.core.entity.Page;
import com.appweb.core.entity.PageData;
import com.appweb.core.logger.Logger;
import com.appweb.core.utils.UuidUtil;

public class BaseController {
	
	protected Logger logger = Logger.getLogger(this.getClass());

	private static final long serialVersionUID = 6357869213649815390L;
	

	
	/**
	 * 得到分頁列表的資訊 
	 */
	
	
	public static void logBefore(Logger logger, String interfaceName){
		logger.info("");
		logger.info("start");
		logger.info(interfaceName);
	}
	
	public static void logAfter(Logger logger){
		logger.info("end");
		logger.info("");
	}
	
}


然後每個控制類都extends這個BaseController,使用日誌的時候叫呼叫logger日誌管理類