日志可以方便追踪和调试问题,以前用log4net写日志,换Java了改用log4j写日志,用法和log4net差不多。
到apache包下载下载log4j的包,解压后把下图两个jar包引入工程
先到网站根下加一个log4j2.xml的配置文件来配置日志的格式和参数
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="ERROR"><!--Configuration 配置项status:log4j本身的日志级别, “trace”, “debug”, “info”, “warn”, “error” and “fatal”monitorInterval:每隔多少秒从新读取配置文件,可以在不重启的情况下修改配置--><Appenders><!--Appenders定义日志输出,有Console、File、RollingRandomAccessFile、MongoDB、Flume 等有Console:输出源到控制台File:将日志输出到文件,通过fileName指定存储到的文件(目录不存在会自动创建)RollingRandomAccessFile:也是写入文件,但可以定义规则按照文件大小或时间等重新创建一个新的日志文件存储;如果是按时间分割需要配合filePattern使用--><Console name="Console" target="SYSTEM_OUT"><PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %F %logger{36} - %msg%n"/><!--PatternLayout指定输出日志的格式 --></Console><File name="ExceptionRollingLogFileAppender" fileName="./Logs/异常日志.txt" append="true"><PatternLayout pattern="%date [%thread] %-5level %15logger %appdomain %location -- %message %exception %newline"/></File><File name="DebugRollingLogFileAppender" fileName="./Logs/调试日志.txt" append="true"><PatternLayout pattern="%date [%thread] %-5level %15logger %appdomain %location -- %message %exception %newline"/></File><File name="OperationRollingLogFileAppender" fileName="./Logs/方法日志.txt" append="true"><PatternLayout pattern="%date [%thread] %-5level %15logger %appdomain %location -- %message %exception %newline"/></File><File name="SqlRollingLogFileAppender" fileName="./Logs/Sql语句日志.txt" append="true"><PatternLayout pattern="%date [%thread] %-5level %15logger %appdomain %location -- %message %exception %newline"/></File><File name="CapabilityRollingLogFileAppender" fileName="./Logs/性能.txt" append="true"><PatternLayout pattern="%date [%thread] %-5level %15logger %appdomain %location -- %message %exception %newline"/></File><File name="SecurityRollingLogFileAppender" fileName="./Logs/安全.txt" append="true"><PatternLayout pattern="%date [%thread] %-5level %15logger %appdomain %location -- %message %exception %newline"/></File><RollingFile name="customscript" fileName="${LOG_HOME}${FILE_NAME}" filePattern="${LOG_HOME}${FILE_NAME}.%d{yyyy-MM-dd}.log"><PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %M %L - %msg%xEx%n"/><Policies><TimeBasedTriggeringPolicy /></Policies></RollingFile></Appenders><Loggers><!--日志器,通过LogManager.getLogger(日志器name)的日志器名字决定使用具体哪个日志器分为根Root日志器和自定义日志器--><Root level="ERROR"><!--当根据名字找不到对应的日志器时,使用Root的日志器leve:日志输出等级(默认ERROR);TRACE > DEBUG > INFO > WARN > ERROR, ALL or OFF--><AppenderRef ref="Console"/><!--AppenderRef:关联Appenders中定义的输出规则,可以有多个,日志可以同时输出到多个地方--><AppenderRef ref="debuglog"/></Root><Logger name="Exception" level="ALL" additivity="false"><!--Logger自定义日志器:name为日志器的名字,通过LogManager.getLogger(日志器name)获得该日志器连接additivity:相加性。默认为true,若为true会将当前日志内容也打印到它上面的日志器内,这里上面日志器是Root--><AppenderRef ref="ExceptionRollingLogFileAppender"/></Logger><Logger name="Operation" level="ALL" additivity="false"><!--Logger自定义日志器:name为日志器的名字,通过LogManager.getLogger(日志器name)获得该日志器连接additivity:相加性。默认为true,若为true会将当前日志内容也打印到它上面的日志器内,这里上面日志器是Root--><AppenderRef ref="OperationRollingLogFileAppender"/></Logger><Logger name="Debug" level="ALL" additivity="false"><!--Logger自定义日志器:name为日志器的名字,通过LogManager.getLogger(日志器name)获得该日志器连接additivity:相加性。默认为true,若为true会将当前日志内容也打印到它上面的日志器内,这里上面日志器是Root--><AppenderRef ref="DebugRollingLogFileAppender"/></Logger><Logger name="SqlLog" level="ALL" additivity="false"><!--Logger自定义日志器:name为日志器的名字,通过LogManager.getLogger(日志器name)获得该日志器连接additivity:相加性。默认为true,若为true会将当前日志内容也打印到它上面的日志器内,这里上面日志器是Root--><AppenderRef ref="SqlRollingLogFileAppender"/></Logger><Logger name="Capability" level="ALL" additivity="false"><!--Logger自定义日志器:name为日志器的名字,通过LogManager.getLogger(日志器name)获得该日志器连接additivity:相加性。默认为true,若为true会将当前日志内容也打印到它上面的日志器内,这里上面日志器是Root--><AppenderRef ref="CapabilityRollingLogFileAppender"/></Logger><Logger name="Security" level="ALL" additivity="false"><!--Logger自定义日志器:name为日志器的名字,通过LogManager.getLogger(日志器name)获得该日志器连接additivity:相加性。默认为true,若为true会将当前日志内容也打印到它上面的日志器内,这里上面日志器是Root--><AppenderRef ref="SecurityRollingLogFileAppender"/></Logger></Loggers>
</Configuration>
然后实现日志工具类
package LIS.Core.Util;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;public class LogUtils {//得到日志操作,默认写在Tomcat的Bin下//异常日志static final Logger loggerException = LogManager.getLogger("Exception");//操作日志static final Logger loggerOperation = LogManager.getLogger("Operation");//调试日志static final Logger loggerDebug = LogManager.getLogger("Debug");//sql日志static final Logger loggerSqlLog = LogManager.getLogger("SqlLog");//性能日志static final Logger loggerCapability = LogManager.getLogger("Capability");//安全日志static final Logger loggerSecurity = LogManager.getLogger("Security");/// <summary>/// 书写调试日志/// </summary>/// <param name="message">日志内容</param>public static void WriteDebugLog(String message){loggerDebug.debug(message);}/// <summary>/// 书写异常日志/// </summary>/// /// <param name="message">日志内容</param>/// <param name="exception">异常对象</param>public static void WriteExceptionLog(String message, Exception exception){loggerException.error(message, exception);}/// <summary>/// 书写性能日志/// </summary>/// <param name="message">日志内容</param>public static void WriteCapabilityLog(String message){loggerCapability.info(message);}/// <summary>/// 书写安全日志/// </summary>/// <param name="message">日志内容</param>public static void WriteSecurityLog(String message){loggerSecurity.info(message);}/// <summary>/// 书写操作日志/// </summary>/// <param name="message">日志内容</param>public static void WriteOperationLog(String message){loggerOperation.info(message);}/// <summary>/// 书写调试日志/// </summary>/// <param name="message">日志内容</param>public static void WriteSqlLog(String message){loggerSqlLog.debug(message);}
}
调用写日志测试
在Tomcat的Bin下的Logs生成日志
后面就把System.out.println的打印日志换为LIS.Core.Util.LogUtils写日志了