Spring
Spring

Reputation: 11835

Spring boot multiple log files

In my Spring boot project with "@Slf4j" annotated classes, for certain classes I want to log to a different file. But couldn't figure out how to do that. I have one logback-spring.xml file, which is referenced from my properties file like this:

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

Do I have to create another logback-spring.xml file now? or I can configure it in current file, and if then how can I choose which logger to use when.

Upvotes: 17

Views: 37984

Answers (2)

Sofiane
Sofiane

Reputation: 693

if you want to have two differents files you need to add under tag <appender> the following code :

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

Upvotes: 0

Evgeni Dimitrov
Evgeni Dimitrov

Reputation: 22506

Just add another logger and appender. For example I used the following 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>

In the code you can access the logger with:

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

This will get the audit-log logger and use FILE-AUDIT appender.

The "standart" appender is used with any class that is in the specified package:

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

This will use the <logger name="com.myCompany.myProject" level="info" additivity="false"> and obviosly the FILE appender.

Upvotes: 27

Related Questions