标签:日志 log4j
①使用到的jar包:
大概是这几个,具体我也不知道是哪一个,下次使用的时候自己测试下。
commons-logging-1.2.jar
commons-discovery-0.5.jar
log4j-1.2.17.jar
②log4j.properties
log4j.rootLogger=DEBUG,CONSOLE,A log4j.addivity.org.apache=false log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender log4j.appender.CONSOLE.Threshold=DEBUG log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss} -%-4r [%t] %-5p %x - %m%n log4j.appender.CONSOLE.Target=System.out log4j.appender.CONSOLE.Encoding=gbk log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout log4j.appender.A=org.apache.log4j.DailyRollingFileAppender log4j.appender.A.File=${catalina.home}/logs/kentra_log/PurePro_ log4j.appender.A.DatePattern=yyyy-MM-dd‘.log‘ log4j.appender.A.layout=org.apache.log4j.PatternLayout log4j.appender.A.layout.ConversionPattern=[FH_sys] %d{yyyy-MM-dd HH\:mm\:ss} %5p %c{1}\:%L \: %m%n log4j.logger.java.sql.ResultSet=INFO log4j.logger.org.apache=INFO log4j.logger.java.sql.Connection=DEBUG log4j.logger.java.sql.Statement=DEBUG log4j.logger.java.sql.PreparedStatement=DEBUG
③log4j.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd"> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> <!-- Appenders --> <appender name="console" class="org.apache.log4j.ConsoleAppender"> <param name="Target" value="System.out" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{yyyy HH:mm:ss} %-5p %c - %m%n" /> </layout> </appender> <!-- Application Loggers --> <logger name="com.kentra"> <level value="info" /> </logger> <!-- 3rdparty Loggers --> <logger name="org.springframework.core"> <level value="info" /> </logger> <logger name="org.springframework.beans"> <level value="info" /> </logger> <logger name="org.springframework.context"> <level value="info" /> </logger> <logger name="org.springframework.web"> <level value="info" /> </logger> <logger name="org.springframework.jdbc"> <level value="info" /> </logger> <logger name="org.mybatis.spring"> <level value="info" /> </logger> <logger name="java.sql"> <level value="info" /> </logger> <!-- Root Logger --> <root> <priority value="info" /> <appender-ref ref="console" /> </root> </log4j:configuration>
④Logger.java工具类
package com.kentra.util; /** * 说明:日志处理 * 创建人:KENTRASOFT * 修改时间:2014年9月20日 * @version */ 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()); } }
⑤使用
/**保存 * @param * @throws Exception */ @RequestMapping(value="/save") @ResponseBody public ResultUtil save(Supervision supervision,String fileGrids) throws Exception{ String busId = this.get32UUID(); if(!Jurisdiction.buttonJurisdiction(this.getPageData().getString("key"))){return null;} //校验权限 supervision.setId(busId); supervision.setCreatTime(DateUtil.getTime()); supervisionService.save(supervision); logger.info("-----------日志打印------------"); logBefore(logger, busId, Jurisdiction.getUsername(), "督办单-新增"); return ResultUtil.success("添加成功!", "supervision", "", "closeCurrent"); }
本文出自 “JianBo” 博客,请务必保留此出处http://jianboli.blog.51cto.com/12075002/1960178
标签:日志 log4j
原文地址:http://jianboli.blog.51cto.com/12075002/1960178