标签:
1 public class Demo{ 2 // 构造记录器,形参是记录器所在的类,表示要在该类做日志 3 private static Logger logger = Logger.getLogger(Demo.class); 4 5 public static void main(String[] args) { 6 getMessage(); 7 } 8 9 private static void getMessage() { 10 // 记录下各种级别的信息,这些信息放在哪儿,以哪种方式存放,在log4j.properties文件中配置. 11 //一下是记录日志的等级,向下兼容 12 logger.debug("This is debug message."); 13 logger.info("This is a info message."); 14 logger.warn("This is a warn message."); 15 logger.error("This is a error message............."); 16 } 17 18 }
1 ### direct log messages to stdout ### 2 log4j.appender.stdout=org.apache.log4j.ConsoleAppender 3 log4j.appender.stdout.Target=System.out 4 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 5 log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n 6 7 ### direct messages to file mylog.log ### 8 ### set log levels - for more verbose logging change ‘info‘ to ‘debug‘ ### 9 log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender 10 log4j.appender.FILE.Append=true 11 log4j.appender.FILE.DatePattern=‘.‘yyyy-MM-dd 12 #log4j.appender.FILE.DatePattern=‘.‘yyyy-MM-dd-HH-mm/yyyy-MM-dd-HH: 13 log4j.appender.FILE.File=./logs/parseInfo.log 14 log4j.appender.FILE.Threshold=INFO 15 log4j.appender.FILE.Encoding=UTF-8 16 #log4j.appender.FILE.MaxFileSize=20MB 17 log4j.appender.FILE.layout=org.apache.log4j.PatternLayout 18 log4j.appender.FILE.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} - [%p]-[%l] %m%n 19 log4j.rootLogger=info,FILE,stdout
# 每天产生一个日志文件(dailyFile)
log4j.appender.dailyFile=org.apache.log4j.DailyRollingFileAppender
#当天的日志文件全路径
log4j.appender.dailyFile.File=d:/logs/sbrd
#服务器启动日志是追加,false:服务器启动后会生成日志文件把老的覆盖掉
log4j.appender.dailyFile.Append=true
#日志文件格式
log4j.appender.dailyFile.layout=org.apache.log4j.PatternLayout
log4j.appender.dailyFile.layout.ConversionPattern=%-d{yyyy-MM-dd HH\:mm\:ss} [%c]-[%p] [%t] (%F\:%L) ->%m %n
log4j.appender.dailyFile.Threshold=INFO
#设置每天生成一个文件名后添加的名称,备份名称:年月日.log
log4j.appender.dailyFile.DatePattern=‘.‘yyyy-MM-dd‘.log‘
标签:
原文地址:http://www.cnblogs.com/potato80s/p/5538204.html