Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
837 views
in Technique[技术] by (71.8m points)

java - What is the best way to implement multiple logs location using log4j?

I have to implement multiple log location using log4j in out project which includes multiple modules, for each module i have to log the in separate location

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Perhaps the log4j.properties on this articles would help you seek what you are looking for. http://ta.cnci.org/more-about-java/35-java-blogs/238-dailyfileappender and may be you could use the DailyFileAppender.java that I wrote a while back. The idea is simple and it is in 4 parts:

  1. Set the log4j.rootCategory to include your "FILE DEF"
  2. Define your "FILE DEF" such as log4j.appender.SOADAILY
  3. Tell the Logger to filter it to your "FILE DEF" as: log4j.logger.com.incresearch.soa.db=DEBUG, TDLDAILY
  4. Tell the Logger not delete from everywhere else as: log4j.additivity.org.cnci.tdl=false

But here is an example:


    #------------------------------------------------------------------------------
    #  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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...