Log4j配置

  1、导入Log4j的jar包;

  2、配置Log4j.properties:

#log4j.rootCategory=error,stdout
#
#log4j.appender.stdout=org.apache.log4j.ConsoleAppender
#log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
#log4j.appender.stdout.layout.ConversionPattern=[HYYT] %p [%t] %C.%M(%L) | %m%n
#
#log4j.logger.com.opensymphony.xwork2.ognl.OgnlValueStack=ERROR

##########################\u914d\u7f6erootLogger--\u8f93\u51fa\u6d88\u606f\u7ea7\u522b\uff1aINFO, WARN, ERROR\u548c FATAL ###############
#log4j.rootLogger = ALL, STUDIO, INFO_FILE, WARN_FILE, ERROR_FILE, FATAL_FILE
#####################################\u63a7\u5236\u53f0\u8f93\u51fa##################################################
log4j.rootLogger = ALL, INFO_FILE, WARN_FILE, ERROR_FILE
###############################################################################################
log4j.appender.STUDIO = org.apache.log4j.ConsoleAppender
log4j.appender.STUDIO.Targer = System.out
log4j.appender.STUDIO.Threshold = INFO
log4j.appender.STUDIO.ImmediateFlush = TRUE
log4j.appender.STUDIO.layout = org.apache.log4j.PatternLayout
log4j.appender.STUDIO.layout.ConversionPattern = [%-5p][%-22d{yyyy/MM/dd HH\:mm\:ssS}][%l]%n%m%n
####################################INFO_FILE\u8f93\u51fa################################################
log4j.appender.INFO_FILE = org.apache.log4j.DailyRollingFileAppender
#log4j.appender.INFO_FILE.Targer = /WebLogs/Info_File
log4j.appender.INFO_FILE.File = /WebLogs/web-mobile-server/Info_File/log.txt
log4j.appender.INFO_FILE.Threshold = INFO
log4j.appender.INFO_FILE.ImmediateFlush = TRUE
log4j.appender.INFO_FILE.Append = TRUE
log4j.appender.INFO_FILE.Encoding = UTF-8
log4j.appender.WARN_FILE.DataPattern = '.'YYYY-MM-dd
log4j.appender.INFO_FILE.layout =  org.apache.log4j.PatternLayout
log4j.appender.INFO_FILE.layout.ConversionPattern = [%-5p][%-22d{yyyy/MM/dd HH:mm:ssS}][%l]%n%m%n
##################################WARN_FILE\u8f93\u51fa#################################################
log4j.appender.WARN_FILE = org.apache.log4j.DailyRollingFileAppender
log4j.appender.WARN_FILE.Targer = /WebLogs/Warn_File
log4j.appender.WARN_FILE.File = /WebLogs/web-mobile-server/Warn_File/log.txt
log4j.appender.WARN_FILE.Threshold = WARN
log4j.appender.WARN_FILE.ImmediateFlush = TRUE
log4j.appender.WARN_FILE.Append = TRUE
log4j.appender.WARN_FILE.Encoding = UTF-8
log4j.appender.WARN_FILE.DataPattern = '.'YYYY-MM-dd
log4j.appender.WARN_FILE.layout = org.apache.log4j.PatternLayout
log4j.appender.WARN_FILE.layout.ConversionPattern = [%-5p][%-22d{yyyy/MM/dd HH:mm:ssS}][%l]%n%m%n
##################################ERROR_FILE\u8f93\u51fa#################################################
log4j.appender.ERROR_FILE = org.apache.log4j.DailyRollingFileAppender
log4j.appender.ERROR_FILE.Targer = /WebLogs/Error_File
log4j.appender.ERROR_FILE.File = /WebLogs/web-mobile-server/Error_File/log.txt
log4j.appender.ERROR_FILE.Threshold = ERROR
log4j.appender.ERROR_FILE.ImmediateFlush = TRUE
log4j.appender.ERROR_FILE.Append = TRUE
log4j.appender.ERROR_FILE.Encoding = UTF-8
log4j.appender.ERROR_FILE.DataPattern = '.'YYYY-MM-dd
log4j.appender.ERROR_FILE.layout = org.apache.log4j.PatternLayout
log4j.appender.ERROR_FILE.layout.ConversionPattern = [%-5p][%-22d{yyyy/MM/dd HH:mm:ssS}][%l]%n%m%n
##################################FATAL_FILE\u8f93\u51fa################################################
log4j.appender.FATAL_FILE = org.apache.log4j.DailyRollingFileAppender
log4j.appender.FATAL_FILE.Targer = /WebLogs/Fatal_File
log4j.appender.FATAL_FILE.File = /WebLogs/web-mobile-server/Fatal_File/log.txt
log4j.appender.FATAL_FILE.Threshold = FATAL
log4j.appender.FATAL_FILE.ImmediateFlush = TRUE
log4j.appender.FATAL_FILE.Append = TRUE
log4j.appender.FATAL_FILE.Encoding = UTF-8
log4j.appender.FATAL_FILE.DataPattern = '.'YYYY-MM-dd
log4j.appender.FATAL_FILE.layout = org.apache.log4j.PatternLayout
log4j.appender.FATAL_FILE.layout.ConversionPattern = [%-5p][%-22d{yyyy/MM/dd HH:mm:ssS}][%l]%n%m%n

  3、编写测试类:

public class LoggerTest {
    
    private static final Logger logger = Logger.getLogger(LoggerTest.class);

@Test public void sysLog(){ System.out.println("-------------------\n" + "sys日志" + "\n-------------------"); logger.debug("-------------------\n" + "Debug日志" + "\n-------------------");//不打印 logger.info("-------------------\n" + "Info日志" + "\n-------------------"); logger.warn("-------------------\n" + "Warn日志" + "\n-------------------"); logger.error("-------------------\n" + "Error日志" + "\n-------------------"); } }

  4、生成的日志文件:

  

posted @ 2017-08-30 11:05  小破孩123  阅读(348)  评论(0编辑  收藏  举报