Log4j日志等级
Log4j根据日志信息的重要程度,分OFF、FATAL、ERROR、WARN、INFO、DEBUG、ALL
当然再细分的话 还有 FATAL(严重错误),
但是Log4j官方建议实际实用的话,Log4j建议只使用四个级别,优先级从高到低分别是 ERROR、WARN、INFO、DEBUG
假如rootLogger定义的是DEBUG 那所有的报错信息都会输出;
#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
#File
log4j.appender.File = org.apache.log4j.FileAppender
log4j.appender.File.File = f://logs//log.log
log4j.appender.File.layout = org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern =%d [%t] %-5p [%c] - %m%n
Log4j官方的appender给出了一下几种实现
org.apache.log4j.ConsoleAppender(控制台),
org.apache.log4j.FileAppender(文件),
org.apache.log4j.DailyRollingFileAppender(每天产生一个日志文件),
org.apache.log4j.RollingFileAppender(文件大小到达指定尺寸的时候产生一个新的文件),
org.apache.log4j.WriterAppender(将日志信息以流格式发送到任意指定的地方)
实际开发我们使用第1,第3和第4种实现;
假如日志数据量不是很大,我们可以用DailyRollingFileAppender 每天产生一个日志,方便查看;
假如日志数据量很大,我们一般用RollingFileAppender,固定尺寸的日志,假如超过了 就产生一个新的文件;
----------------------------------java-----------------------------------------------------------------------
public class Test {
private static Logger logger = Logger.getLogger(Test.class); // 获取logger实例
public static void main(String[] args) {
logger.info("普通Info信息");
logger.debug("调试debug信息");
logger.error("报错error信息");
logger.warn("警告warn信息");
logger.fatal("严重错误fatal信息");
logger.error("报错信息", new IllegalArgumentException("非法参数"));
int i = 0;
while (i < 1000) {
logger.debug(" RollingFile 调试debug信息"+i);
logger.debug(" RollingFile 调试debug信息"+i);
logger.debug(" RollingFile 调试debug信息"+i);
logger.debug(" RollingFile 调试debug信息"+i);
logger.debug(" RollingFile 调试debug信息"+i);
i++;
}
}
}
----------------------------------案例------------------------------------------------------------------------
log4j.rootLogger=DEBUG, Console ,File ,DailyRollingFile ,RollingFile,zhangsanLog
#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
#File
log4j.appender.File = org.apache.log4j.FileAppender
log4j.appender.File.File = F://logs//log2.log
log4j.appender.File.layout = org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern =%d [%t] %-5p [%c] - %m%n
#DailyRollingFile
log4j.appender.DailyRollingFile = org.apache.log4j.DailyRollingFileAppender
log4j.appender.DailyRollingFile.File =F://logs//log3.log
log4j.appender.DailyRollingFile.layout = org.apache.log4j.PatternLayout
log4j.appender.DailyRollingFile.layout.ConversionPattern =%d [%t] %-5p [%c] - %m%n
#RollingFile
log4j.appender.RollingFile = org.apache.log4j.RollingFileAppender
log4j.appender.RollingFile.File = F://logs//log4.log
log4j.appender.RollingFile.MaxFileSize=1KB
log4j.appender.RollingFile.MaxBackupIndex=3
log4j.appender.RollingFile.layout = org.apache.log4j.PatternLayout
log4j.appender.RollingFile.layout.ConversionPattern =%d [%t] %-5p [%c] - %m%n
#zhangsanLog
log4j.appender.zhangsanLog = org.apache.log4j.RollingFileAppender
log4j.appender.zhangsanLog.File =F\:\\logs\\debug.log
#log4j.appender.zhangsanLog.File =/var/alldata/zhenduan/debug.log
log4j.appender.zhangsanLog.Append = true
## \u8F93\u51FADEBUG\u7EA7\u522B\u4EE5\u4E0A\u7684\u65E5\u5FD7
log4j.appender.zhangsanLog.Threshold = DEBUG
#'.'yyyy-MM-dd: \u6BCF\u5929\u4EA7\u751F\u4E00\u4E2A\u65B0\u7684\u6587\u4EF6
log4j.appender.zhangsanLog.MaxFileSize = 2KB
log4j.appender.zhangsanLog.MaxBackupIndex = 5
log4j.appender.zhangsanLog.layout = org.apache.log4j.PatternLayout
log4j.appender.zhangsanLog.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [%t:%r] - [%p] [%c{1}:%L] [%M] %m%n
log4j.additivity.zhangsanLog = false