如何制作一个可以从不同包记录到不同文件的记录器

时间:2016-07-21 07:13:56

标签: java logging log4j2

所以我使用Log4j2将我的应用程序中的信息记录到某些文件中,但我知道我希望按包分开文件。我有一个记录器,我不知道如何让他按包/ class /

过滤日志
<Logger name="hextodec.MainClass" additivity="FALSE">
        <AppenderRef ref="info"  level="INFO"/>
        <AppenderRef ref="error"  level="ERROR"/>
        <AppenderRef ref="stdout" level ="INFO" />
</Logger>

2 个答案:

答案 0 :(得分:1)

嘿,这只是为了您的信息,对您的测试用例有一个想法和实现:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <property name="HIBERNATE_LOG_LOCATION" value="../logs/dev/hibernate" />
    <property name="HIBERNATE_ARCHIVE_LOG_LOCATION" value="../logs/dev/hibernate/archive" />

    <property name="SPRING_LOG_LOCATION" value="../logs/dev/spring" />
    <property name="SPRING_ARCHIVE_LOG_LOCATION" value="../logs/dev/spring/archive" />

<appender name="HIBERNATE_INFO"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${BASE_LOCATION}${HIBERNATE_LOG_LOCATION}/RERUN_UTIL_HIB_INFO.log</file>

        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- hourly rollover -->
            <fileNamePattern>${BASE_LOCATION}${HIBERNATE_ARCHIVE_LOG_LOCATION}/RERUN_UTIL_HIB_INFO.%d{yyyy-MM-dd_HH}.%i.log.gz
            </fileNamePattern>

            <timeBasedFileNamingAndTriggeringPolicy
                class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <!-- or whenever the file size reaches 100MB -->
                <maxFileSize>5MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>

            <!-- keep 30 days' worth of history -->
            <maxHistory>30</maxHistory>
        </rollingPolicy>

        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>INFO</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>

        <encoder>
            <!-- <pattern>%date %level [%thread] %logger{10} [%file : %line] %msg%n</pattern> -->
            <pattern>%date [%thread] %-5level %class [%M:%L] - %msg%n</pattern>
        </encoder>
    </appender>
    <!-- similar appenders for all others -->
<appender name="SP_INFO"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${BASE_LOCATION}${SPRING_LOG_LOCATION}/SPRING_INFO.log</file>

        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- hourly rollover -->
            <fileNamePattern>${BASE_LOCATION}${SPRING_ARCHIVE_LOG_LOCATION}/SPRING_INFO.%d{yyyy-MM-dd_HH}.%i.log.gz
            </fileNamePattern>

            <timeBasedFileNamingAndTriggeringPolicy
                class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">

                <maxFileSize>5MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>


            <maxHistory>30</maxHistory>
        </rollingPolicy>

        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>INFO</level>
            <onMatch>ACCEPT</onMatch>
            <onMismatch>DENY</onMismatch>
        </filter>

        <encoder>

            <pattern>%date [%thread] %-5level %class [%M:%L] - %msg%n</pattern>
        </encoder>
    </appender>
    <!-- similar appenders for all others-->

<logger name="org.hibernate" additivity="false">
        <appender-ref ref="HIBERNATE_DEBUG" />
        <appender-ref ref="HIBERNATE_INFO" /> 
        <appender-ref ref="HIBERNATE_ERROR" />      
    </logger>

    <logger name="org.springframework" additivity="false">
        <appender-ref ref="SP_ERROR" />
        <appender-ref ref="SP_DEBUG" />     
        <appender-ref ref="SP_INFO" />
    </logger>

    <root level="ALL">
        <appender-ref ref="INFO" />
        <appender-ref ref="DEBUG" />
        <appender-ref ref="ERROR" />
    </root>

</configuration>

希望这能解决你的问答。

答案 1 :(得分:0)

您可以通过创建多个文件追加器来完成此操作。

示例:https://logging.apache.org/log4j/2.x/manual/configuration.html

带有

的Appender示例
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
  <Appenders>
    <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </Console>
    <File name="File" fileName="/var/log/file.log">
       <PatternLayout>
           <pattern>%d %p %C{1.} [%t] %m%n</pattern>
       </PatternLayout>
    </File>
  </Appenders>
  <Loggers>
    <Logger name="com.foo.Bar" level="trace">
      <AppenderRef ref="Console"/>
    </Logger>
    <Logger name="com.foo.acme" level="trace">
      <AppenderRef ref="File"/>
    </Logger>
    <Root level="error">
      <AppenderRef ref="Console"/>
    </Root>
  </Loggers>
</Configuration>