Log4j配置,

作者:杏彩彩票app下载

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

##########################u914du7f6erootLogger--u8f93u51fau6d88u606fu7ea7u522buff1aINFO, WARN, ERRORu548c FATAL ###############
#log4j.rootLogger = ALL, STUDIO, INFO_FILE, WARN_FILE, ERROR_FILE, FATAL_FILE
#####################################u63a7u5236u53f0u8f93u51fa##################################################
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_FILEu8f93u51fa################################################
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_FILEu8f93u51fa#################################################
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_FILEu8f93u51fa#################################################
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_FILEu8f93u51fa################################################
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、生成的日志文件:

  图片 1

1、导入Log4j的jar包; 2、配置Log4j.properties: #log4j.rootCategory=error,stdout##log4j.appender.stdout=org.apache.log4j.ConsoleAppender#log4j.appender.stdou...

1:首先导入需要的jar包

SSH使用Log4j,sshlog4j

  1. 将Jar文件log4j-1.2.14.jar导入项目。

  2. 在src文件夹下新建log4j.properties文件:

    log4j.rootLogger = debug,stdout,D,E

    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

    log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
    log4j.appender.D.File = D://logs/log.log
    log4j.appender.D.Append = true
    log4j.appender.D.Threshold = DEBUG
    log4j.appender.D.layout = org.apache.log4j.PatternLayout
    log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n

    log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
    log4j.appender.E.File =D://logs/error.log
    log4j.appender.E.Append = true
    log4j.appender.E.Threshold = ERROR
    log4j.appender.E.layout = org.apache.log4j.PatternLayout
    log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n

该文件设置了日志的级别、目录等信息。

  1. 修改web.xml文件,增加下面的语句:

     <context-param>    
         <param-name>webAppRootKey</param-name>    
         <param-value>webapp.root</param-value>    
     </context-param> 
    
     <context-param>  
         <param-name>log4jConfigLocation</param-name>  
         <param-value>classpath:log4j.properties</param-value>  
     </context-param> 
    
     <context-param>    
         <param-name>log4jRefreshInterval</param-name>    
         <param-value>3000</param-value>    
     </context-param>  
    
     <listener>  
         <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
     </listener>
    

applicationConetext.xml文件不需要修改。

  1. 在需要输出日志的类里面增加语句:

    private static Logger logger = Logger.getLogger(XXX.class);

该语句获得了一个Logger。然后在需要输出日志的地方:

Logger.debug ( Object message ) ;  
Logger.info ( Object message ) ;  
Logger.warn ( Object message ) ;  
Logger.error ( Object message ) ;

四条语句对应了4个不同的级别,debug的级别最低,error级别最高,Threshold如果设置成DEBUG,则4个级别都会输出,如果设置成ERROR,则只会输出error级别。

1. 将Jar文件log4j-1.2.14.jar导入项目。 2. 在src文件夹下新建log4j.properties文件: log4j.rootLogger = debug,stdout,D,E log4j.appender.std...

2:创建log4j.properties文件

log4j.rootLogger=info,stdout

#log4j.rootLogger=error,daily_file

log4j.appender.stdout=org.apache.log4j.ConsoleAppender

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

本文由杏彩发布,转载请注明来源

关键词: