Custom log file for specific packages in Spring bo

2019-07-11 02:39发布

I have a java package with specialized operations. Specialized in the sense that they are rarely used and i don't want to have them be mixed with normal logging.

I know that adding logging.file=myapplication.log will redirect the logging to this file but is there a way to specify only logging from specific packages to another file? Like logging.file.my.package=special.log ?

2条回答
仙女界的扛把子
2楼-- · 2019-07-11 02:58

Spring uses Logback as default logger. According to the official doc you can setup logback.xml yourself to add to the default logging mechanism your 'special' behavior:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/defaults.xml" />
    <property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}"/>

    <include resource="org/springframework/boot/logging/logback/file-appender.xml" />
    <include resource="org/springframework/boot/logging/logback/console-appender.xml" />

    <property name="SPECIAL_FILE_NAME" value="special"/>
    <appender name="SPECIAL_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <encoder>
            <pattern>%date{yyyy-MM-dd HH:mm:ss.SSS} [%-10.10thread] %-5level %30.30logger{29}:%-4line %msg%n</pattern>
            <charset>utf8</charset>
        </encoder>
        <file>log/${SPECIAL_FILE_NAME}.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <fileNamePattern>${SPECIAL_FILE_NAME}-%i.log</fileNamePattern>
        </rollingPolicy>
        <triggeringPolicy
                class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>10MB</MaxFileSize>
        </triggeringPolicy>
    </appender>

    <logger name="logging.file.my.package" level="debug" additivity="false">
        <appender-ref ref="SPECIAL_FILE"/>
    </logger>

    <root level="INFO">
        <appender-ref ref="FILE" />
        <appender-ref ref="CONSOLE" />
    </root>
</configuration>

Here we use Spring default FILE and CONSOLE appenders to log app info as usual (except logging.file.my.package), and use SPECIAL_FILE appender to log info from this package to file log/special.log.

查看更多
虎瘦雄心在
3楼-- · 2019-07-11 02:59

It's not possible with the logging configuration Spring Boot provides. However, you can fall back upon the configuration that the logging framework provides. By default, this is Logback, which can be configured to log to multiple logging files.

To do that, you need to add a logback.xml file to your classpath and configure multiple appenders. For example:

<appender name="FILE1" class="ch.qos.logback.core.FileAppender">
    <file>log1.log</file>
    <append>true</append>
    <encoder>
        <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
    </encoder>
</appender>

<appender name="FILE2" class="ch.qos.logback.core.FileAppender">
    <file>log2.log</file>
    <append>true</append>
    <encoder>
        <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
    </encoder>
</appender>

And now you can define a separate logger in case you want to log to a different file. Make sure to add the additivity="false" otherwise, the log message will still be printed in both log files:

<logger name="com.example.apps.special-package" level="INFO" additivity="false">
    <appender-ref ref="FILE2" />
</logger>
<root level="INFO">
    <appender-ref ref="FILE1" />
</root>

In this case, all logs will be written to log1.log (FILE1 appender), while logs from the package com.example.apps.special-package will be written to log2.log (FILE2 appender).

查看更多
登录 后发表回答