使用Log4J的1 *,我怎么能写两个包两个单独的文件?(Using Log4J 1.*, how

2019-09-03 08:36发布

我有以下两个包:

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

我想配置Log4j(SLF4J)从一个包写日志来一个文件,并从其它封装到第二个文件。 我不希望他们被一起混合。

需要明确的是,这是一个项目/一个进程运行。

我试过过滤,并用记录仪,但他们似乎被log4j的被忽略。 这两个文件都始终相同。

编辑:谢谢你的回答,到目前为止,这是我得和它不工作。 这两个输出文件是相同的。

<configuration debug="true">
<contextName>dev</contextName>

<appender name="FIRST_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>A:/dev/LogTesting/logs/first.log</file>
    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
        <level>INFO</level>
    </filter>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <fileNamePattern>A:/dev/LogTesting/logs/first.%d{yyyyMMdd}%d{_HHmmss,aux}.log.gz</fileNamePattern>
    </rollingPolicy>
    <encoder>
        <pattern>%d{MMM dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
</appender>

<appender name="SECOND_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>A:/dev/LogTesting/logs/second.log</file>
    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
        <level>INFO</level>
    </filter>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <fileNamePattern>A:/dev/LogTesting/logs/second.%d{yyyyMMdd}%d{_HHmmss,aux}.log.gz</fileNamePattern>
    </rollingPolicy>
    <encoder>
        <Pattern>%d{MMM dd HH:mm:ss.SSS} %property{HOSTNAME} [%thread] %level %logger{36} %msg%n</Pattern>
    </encoder>
</appender>

<!-- =============================================================== -->
<logger name="com.test.apples" additivity="false">
    <level value="DEBUG" />
    <appender-ref ref="FIRST_FILE"  />
</logger>

<logger name="com.test.oranges" additivity="false">
    <level value="DEBUG" />
    <appender-ref ref="SECOND_FILE"  />
</logger>
<!-- =============================================================== -->

<category name="com.test.apples" additivity="false">
    <priority value="DEBUG"/>
    <appender-ref ref="FIRST_FILE"/>
</category>

<category name="com.test.oranges" additivity="false">
    <priority value="DEBUG"/>
    <appender-ref ref="SECOND_FILE"/>
</category>

<!-- =============================================================== -->

<!-- default -->
<root level="WARN">
    <appender-ref ref="FIRST_FILE" />
    <appender-ref ref="SECOND_FILE" />
</root>

Answer 1:

试试这个是属性文件的例子:

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

或本作XML(只突出重要的组成部分):

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


Answer 2:

创建两个附加目的地LOG1LOG2 ,每个日志文件。 后定义两类(每个封装),参照这些附加目的地诠释log4j的配置文件。

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




Answer 3:

放dynamiclly文件。在log4j.properties路径

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

而在java中设置文件路径System.setProperty("file.name", FILEPATH);



Answer 4:

你只需指定2个追加程序,但都给出相同的输出日志文件名。

#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


文章来源: Using Log4J 1.*, how can I write two packages to two separate files?