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.DailyRollingFileAppenderlog4j.appender.I =com.shopping.test.LogAppenderlog4j.appender.I.append = truelog4j.appender.I.Threshold = INFOlog4j.appender.I.datePattern = '.'yyyy-MM-dd'.log'log4j.appender.I.MaxFileSize=100000KBlog4j.appender.I.layout = org.apache.log4j.PatternLayoutlog4j.appender.I.File = ${catalina.home}/logs_dk/info.loglog4j.appender.I.layout.ConversionPattern = [%-5p]_%d{yyyy-MM-dd HH:mm:ss} %c - %m%nlog4j.appender.I.encoding=UTF-8### 记录error 级别的日志###log4j.logger.error=Elog4j.appender.E = org.apache.log4j.DailyRollingFileAppenderlog4j.appender.E.datePattern = '.'yyyy-MM-dd'.log'log4j.appender.E.Append = truelog4j.appender.E.Threshold = ERRORlog4j.appender.E.layout = org.apache.log4j.PatternLayout#log4j.appender.E.File =/home/logs/error.log#log4j.appender.E.File =F:\\logs\\dk\\error.loglog4j.appender.E.File = ${catalina.home}/logs_dk/error.loglog4j.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("今日管理出错");
这样就可以在自己定义的输出位置找要输出的日志文件了