Using Log4J 1.*, how can I write two packages to two separate files?

前端 未结 4 1533
终归单人心
终归单人心 2020-12-31 11:11

I have the following two packages:

com.mycorp.project.first
com.mycorp.project.second

I\'d like to configure Log4J (SLF4J) to write the logs from one pac

相关标签:
4条回答
  • 2020-12-31 11:13

    put dynamiclly file path .in log4j.properties

    log4j.appender.FILE.File=${file.name}
    

    And in java set file path System.setProperty("file.name", FILEPATH);

    0 讨论(0)
  • 2020-12-31 11:21

    Create two appenders LOG1 and LOG2, one for each log file. After that define two categories(one for each package) with reference to these appenders int the log4j config file.

    <category name="com.mycorp.project.first" additivity="true">
        <priority value="TRACE"/>
        <appender-ref ref="LOG1"/>
    </category>
    

    Appender2

    <category name="com.mycorp.project.second" additivity="true">
        <priority value="TRACE"/>
        <appender-ref ref="LOG2"/>
    </category>
    


    0 讨论(0)
  • 2020-12-31 11:29

    you just specify 2 appenders, but give same output log file name for both.

    #same appender for both packages
    log4j.logger.package1=INFO,MyCommonLogger    
    log4j.logger.package2=INFO,MyCommonLogger
    
    log4j.appender.MyCommonLogger=org.apache.log4j.DailyRollingFileAppender
    log4j.appender.MyCommonLogger.layout=org.apache.log4j.PatternLayout
    log4j.appender.MyCommonLogger.layout.ConversionPattern=%t %d{MM/dd/yy HH:mm:ss} %x %-5p %c %M - %m%n
    
    #single log file for logs from both packages
    log4j.appender.MyCommonLogger.File=/somewhereonserver/log/mysinglelogfile.log
    
    0 讨论(0)
  • 2020-12-31 11:38

    Try this is the example of properties file:

    log4j.rootLogger=DEBUG, CONSOLE
    # Each package has different appender name     
    log4j.logger.com.mycorp.project.first=DEBUG, FIRST
    log4j.logger.com.mycorp.project.second=DEBUG, SECOND
    
    log4j.appender.FIRST=org.apache.log4j.RollingFileAppender
    log4j.appender.FIRST.File=./first.log
    log4j.appender.FIRST.layout=org.apache.log4j.PatternLayout
    
    log4j.appender.SECOND=org.apache.log4j.RollingFileAppender
    log4j.appender.SECOND.File=./second.log
    log4j.appender.SECOND.layout=org.apache.log4j.PatternLayout
    

    or this for XML (only highlight important part):

    <!-- FIRST appender definition -->
    <appender name="FIRST" class="org.apache.log4j.FileAppender">
        ...
    </appender> 
    
    <!-- SECOND appender definition -->
    <appender name="SECOND" class="org.apache.log4j.FileAppender">
        ...
    </appender>
    
    <!-- logger package com.mycorp.project.first -->
    <logger name="com.mycorp.project.first">
        <level value="DEBUG" />
        <appender-ref ref="FIRST"/>
    </logger>
    
    <!-- logger for package com.mycorp.project.second -->
    <logger name="com.mycorp.project.second">
        <level value="DEBUG" />
        <appender-ref ref="SECOND"/>
    </logger>
    
    0 讨论(0)
提交回复
热议问题