17

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.

Slava Semushin
  • 14,904
  • 7
  • 53
  • 69
Spring
  • 11,333
  • 29
  • 116
  • 185
  • It seems like you are using Lombok for declaring a logger for your class. You can choose a different logger by using the `topic` parameter in the annotation: `@XSlf4j(topic="nameOfLogger").` From the docs: https://projectlombok.org/features/log. Combine this with the answers below on how to create another logger by declaring another `appender` in your configuration. – thomas77 Jun 07 '19 at 07:50

2 Answers2

26

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.

Evgeni Dimitrov
  • 21,976
  • 33
  • 120
  • 145
  • thanks, but I tried your code but cant see a new audit-log file created anywhere. Maybe I need to define something like this in properties ? logging.config=path – Spring Aug 24 '16 at 09:35
  • @Spring Take a look at `` The file should be in `/var/applications/myProject/applogs/myProject_audit.log` or to whatever path you set `LOGS_HOME`. Also check if the application have rights to write in the directory. – Evgeni Dimitrov Aug 24 '16 at 09:57
  • thx but still dont work, I checked the rights(ls -la) its looks same as my other default log file – Spring Aug 25 '16 at 12:16
  • Do you log something using `private static Logger audit = LoggerFactory.getLogger("audit-log");` ? Also try to run logback in debug mode: http://stackoverflow.com/questions/3802054/run-logback-in-debug – Evgeni Dimitrov Aug 25 '16 at 15:12
0

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>
Sofiane
  • 473
  • 1
  • 6
  • 13