使用log4j实现多个日志位置的最佳方法是什么?

时间:2014-02-26 05:18:29

标签: java java-ee

我必须使用log4j在包含多个模块的out项目中实现多个日志位置,对于每个模块,我必须在单独的位置登录

2 个答案:

答案 0 :(得分:0)

这篇文章中的log4j.properties可能会帮助您寻找所需内容。 http://ta.cnci.org/more-about-java/35-java-blogs/238-dailyfileappender可能你可以使用我之前写过的DailyFileAppender.java。 这个想法很简单,分为4个部分:

  1. 设置log4j.rootCategory以包含“FILE DEF”
  2. 定义您的“文件DEF”,例如log4j.appender.SOADAILY
  3. 告诉记录器将其过滤到“FILE DEF”:log4j.logger.com.incresearch.soa.db = DEBUG,TDLDAILY
  4. 告诉记录器不要从其他地方删除:log4j.additivity.org.cnci.tdl = false
  5. 但这是一个例子:

    
        #------------------------------------------------------------------------------
        #  Default log4j.properties file.  This should be in the LocalFiles folder
        #
        #    Possible Log Levels:
        #      FATAL, ERROR, WARN, INFO, DEBUG
        #
        log4j.rootCategory=DEBUG, SOADAILY, TDLDAILY
        log4j.logger.org.apache=ERROR
    
        #------------------------------------------------------------------------------
        #  The following properties configure the Daily Rolling File appender.
        #  For SOA components, mostly from org.cnci.soa.* packages
        #------------------------------------------------------------------------------
        log4j.appender.SOADAILY = org.cnci.util.DailyFileAppender
        log4j.appender.SOADAILY.File = logs/SOAServices.log
        log4j.appender.SOADAILY.MaxLogs = 30
        log4j.appender.SOADAILY.Append = true
        log4j.appender.SOADAILY.DatePattern = '.'yyy-MM-dd
        log4j.appender.SOADAILY.layout = org.apache.log4j.PatternLayout
        log4j.appender.SOADAILY.layout.ConversionPattern = %d %5p %c{1}:%L - %m%n
    
        #------------------------------------------------------------------------------
        #  The following properties configure the Daily Rolling File appender for TDL.
        #  For TDL applications, mostly from org.cnci.tdl.* packages and jboss seams, richfaces...
        #------------------------------------------------------------------------------
        log4j.appender.TDLDAILY = org.apache.log4j.FileAppender
        log4j.appender.TDLDAILY.File = logs/TDLServices.log
        log4j.appender.TDLDAILY.MaxLogs = 30
        log4j.appender.TDLDAILY.Append = true
        log4j.appender.TDLDAILY.DatePattern = '.'yyy-MM-dd
        log4j.appender.TDLDAILY.layout = org.apache.log4j.PatternLayout
        log4j.appender.TDLDAILY.layout.ConversionPattern = %d %5p %c{1}:%L - %m%n
    
    
        # Extracting all Adapter logging into it own file
        log4j.logger.org.cnci.soa=DEBUG, SOADAILY
        log4j.additivity.org.cnci.soa=false
        log4j.logger.com.hazelcast=ERROR, SOADAILY
        log4j.additivity.com.hazelcast=false
        log4j.logger.org.opensaml=ERROR, SOADAILY
        log4j.additivity.org.opensaml=false
    
        # Extracting all DB (TDL) logging into it own file 
        log4j.logger.com.incresearch.soa.db=DEBUG, TDLDAILY
        log4j.additivity.org.cnci.tdl=false
        log4j.logger.org.jboss.seam=ERROR, DAILY
        log4j.additivity.org.jboss.seam=false
        log4j.logger.org.richfaces=ERROR, DAILY
        log4j.additivity.org.richfaces=false
        log4j.logger.org.ajax4jsf=ERROR, DAILY
        log4j.additivity.org.ajax4jsf=false
    
    

答案 1 :(得分:0)

在这里查看我的答案:Log4j to write json array to disk

它显示了如何将com.foo.bar日志发送到console,com.example到文件appender;你可以混合搭配 - 基本上是log4j.com之后的包名。 part将为根植于那里的类的子树创建一个新的记录器;如果在后代包中你需要更改位置(记住,默认情况下appenders是添加的)你必须将'inherit'设置为'false'。