log4j日志输出配置
1.log4j的文件配置:
log4j.rootLogger = debug,stdout,I,E #### 控制台打印 stdout:标准输出### #log4j.appender.stdout = org.apache.log4j.ConsoleAppender #log4j.appender.stdout.Target = System.out #log4j.appender.stdout.layout = org.apache.log4j.PatternLayout #log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n #### 输出到文件file info ### log4j.logger.info=I #log4j.appender.I = org.apache.log4j.DailyRollingFileAppender log4j.appender.I =com.shopping.test.LogAppender log4j.appender.I.append = true log4j.appender.I.Threshold = INFO log4j.appender.I.datePattern = '.'yyyy-MM-dd'.log' log4j.appender.I.MaxFileSize=100000KB log4j.appender.I.layout = org.apache.log4j.PatternLayout log4j.appender.I.File = ${catalina.home}/logs_dk/info.log log4j.appender.I.layout.ConversionPattern = [%-5p]_%d{yyyy-MM-dd HH:mm:ss} %c - %m%n log4j.appender.I.encoding=UTF-8 ### 记录error 级别的日志### log4j.logger.error=E log4j.appender.E = org.apache.log4j.DailyRollingFileAppender log4j.appender.E.datePattern = '.'yyyy-MM-dd'.log' log4j.appender.E.Append = true log4j.appender.E.Threshold = ERROR log4j.appender.E.layout = org.apache.log4j.PatternLayout #log4j.appender.E.File =/home/logs/error.log #log4j.appender.E.File =F:\\logs\\dk\\error.log log4j.appender.E.File = ${catalina.home}/logs_dk/error.log log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
2. 重写一个方法类制定自己的输出
package com.shopping.test; import org.apache.log4j.DailyRollingFileAppender; import org.apache.log4j.Priority; public class LogAppender extends DailyRollingFileAppender { @Override public boolean isAsSevereAsThreshold(Priority priority) { // 只判断是否相等,不判断优先级 return this.getThreshold().equals(priority); } }
我重写这个方法是在info 文件输出时候,过滤输出非Info级别的日志
3.在项目中:
private static Logger logger = Logger.getLogger(FcodeViewAction.class); //logger.info("今日管理"); //logger.error("今日管理出错");
这样就可以在自己定义的输出位置找要输出的日志文件了