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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With