12

我有以下两个包:

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>

4

4 回答 4

24

试试这是属性文件的示例:

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>
于 2013-05-15T11:57:49.380 回答
2

创建两个 appenderLOG1LOG2,每个日志文件一个。之后参考这些附加程序在 log4j 配置文件中定义两个类别(每个包一个)。

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

附加器2

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


于 2013-05-15T11:53:15.720 回答
1

把动态文件路径.in log4j.properties

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

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

于 2013-05-15T11:50:05.800 回答
0

您只需指定 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
于 2014-09-29T22:52:31.007 回答