Spring启动多个日志文件

时间:2016-08-23 14:18:34

标签: spring logging spring-boot logback

在我的带有“@ Slf4j”注释类的Spring启动项目中,对于某些类,我想要记录到不同的文件。但无法弄清楚如何做到这一点。我有一个logback-spring.xml文件,它从我的属性文件中引用,如下所示:

logging.config= path/to/logback-spring.xml
logging.file=myCurrentLogFile.log

我现在必须创建另一个logback-spring.xml文件吗?或者我可以在当前文件中配置它,如果那样,我该如何选择使用哪个记录器。

2 个答案:

答案 0 :(得分:12)

添加另一个记录器和追加器。 例如,我使用了以下logback.xml

<property name="LOGS_HOME" value="/var/applications/myProject/applogs/" />

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <layout class="ch.qos.logback.classic.PatternLayout">
        <Pattern>
            %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
        </Pattern>
    </layout>
</appender>

<appender name="FILE"
    class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${LOGS_HOME}myProject_log.log</file>
    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
        <Pattern>
            %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
        </Pattern>
    </encoder>

    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <!-- rollover daily -->
        <fileNamePattern>${LOGS_HOME}myProject_log.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
        <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
            <maxFileSize>100MB</maxFileSize>
        </timeBasedFileNamingAndTriggeringPolicy>
    </rollingPolicy>
</appender>

<appender name="FILE-AUDIT" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>${LOGS_HOME}myProject_audit.log</file>
    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
        <Pattern>
            %d{yyyy-MM-dd HH:mm:ss};%msg%n
        </Pattern>
    </encoder>

    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <!-- rollover daily -->
        <fileNamePattern>${LOGS_HOME}myProject_audit.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
        <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
            <maxFileSize>100MB</maxFileSize>
        </timeBasedFileNamingAndTriggeringPolicy>
    </rollingPolicy>
</appender>

<logger name="com.myCompany.myProject" level="info" additivity="false">
    <appender-ref ref="FILE" />
    <appender-ref ref="STDOUT" />
</logger>

<logger name="audit-log" level="info" additivity="false">
    <appender-ref ref="FILE-AUDIT" />
    <appender-ref ref="STDOUT" />
</logger>

<root level="error">
    <appender-ref ref="FILE" />
    <appender-ref ref="STDOUT" />
</root>

在代码中,您可以使用以下命令访问记录器:

private static Logger audit = LoggerFactory.getLogger("audit-log");

这将获得audit-log记录器并使用FILE-AUDIT appender。

“standart”appender与指定包中的任何类一起使用:

private static Logger logger = LoggerFactory.getLogger(MyApplication.class);

这将使用<logger name="com.myCompany.myProject" level="info" additivity="false">和obviosly FILE appender。

答案 1 :(得分:0)

如果要具有两个不同的文件,则需要在标签<appender>下添加以下代码:

<File name="FILE-AUDIT-SUIVI" fileName="${LOGS_HOME}myProject_audit.log">
        <PatternLayout>
            <pattern>
                ${LOGS_HOME}
            </pattern>
        </PatternLayout>
    </File>

<RollingFile name="FileAppender" fileName="${LOGS_HOME}archived/myProject_audit.log"
        filePattern="${LOGS_HOME}archived/myProject_audit.%d{yyyy-MM-dd}.%i.log">
        <PatternLayout>
            <Pattern>${LOGS_HOME}</Pattern>
        </PatternLayout>
        <Policies>
            <SizeBasedTriggeringPolicy size="10MB" />
        </Policies>
        <DefaultRolloverStrategy max="1" />
    </RollingFile>