Spring Boot log - spring

I have below code for SpringBoot log. It suppose to generate the file everyday no matter how large the file size is. But sometimes the log file not generated and sometimes the file include the other days log.
How can I solve this ? Is it because the file size not enough so log cannot be generated ?
Please help !
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="DEV_HOME" value="./logs" />
<appender name="SYSLOG" class="ch.qos.logback.classic.net.SyslogAppender">
<syslogHost>10.0.3.20</syslogHost>
<facility>SYSLOG</facility>
<suffixPattern>abc [%thread] %logger %msg</suffixPattern>
</appender>
<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-AUDIT" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${DEV_HOME}/abc.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${DEV_HOME}/abc.%d{yyyy-MM-dd}.log</fileNamePattern>
</rollingPolicy>
<encoder>
<pattern>%relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<logger name="com.rh.abc" level="debug">
<appender-ref ref="STDOUT" />
<appender-ref ref="SYSLOG" />
<appender-ref ref="FILE-AUDIT" />
</logger>
</configuration>

Hope this helps you
For various technical reasons, rollovers are not clock-driven but depend on the arrival of logging events. For example, on 8th of March 2002, assuming the fileNamePattern is set to yyyy-MM-dd (daily rollover), the arrival of the first event after midnight will trigger a rollover. If there are no logging events during, say 23 minutes and 47 seconds after midnight, then rollover will actually occur at 00:23'47 AM on March 9th and not at 0:00 AM. Thus, depending on the arrival rate of events, rollovers might be triggered with some latency. However, regardless of the delay, the rollover algorithm is known to be correct, in the sense that all logging events generated during a certain period will be output in the correct file delimiting that period.
Check out the docs for more details.

Related

How to close springboot default file-appender.xml?

My logback.xml content is as follow:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="LOG_TEMP" value="${user.home}/logs/cloudcanal/console"/>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<appender name="PROJECT" class="ch.qos.logback.classic.sift.SiftingAppender">
<discriminator>
<Key>module</Key>
<DefaultValue>console</DefaultValue>
</discriminator>
<sift>
<appender name="FILE-${module}" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${user.home}/logs/cloudcanal/console/${module}.log</File>
<rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>${user.home}/logs/cloudcanal/console/%d{yyyy-MM-dd}/${module}-%d{yyyy-MM-dd}-%i.log.gz
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!-- or whenever the file size reaches 100MB -->
<maxFileSize>512MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>60</maxHistory>
</rollingPolicy>
<encoder>
<pattern>
%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{56} - %msg%n
</pattern>
</encoder>
</appender>
</sift>
</appender>
<root level="info">
<appender-ref ref="PROJECT"/>
</root>
</configuration>
When I start springboot I find there exist two log file called console.log and spring.log whose content is the same. I check some document and know that spring.log is generated by springboot's file appender(file-appender.xml). Can I only use my own PROJECT appender?
New edited at 2020/11/30
What I really want to achieve is to rename the default spring.log
Can you try adding -
logging.config=logback.xml
on the property file (application.properties)
and see
I find the answer myself. Just using the following config in spring config properties or yaml file.
logging.file.name=console.log

Create new Spring log file every day

I want to configure Spring to write the log messages into file:
logging.file=/my-logs/app.log
logging.path=/my-logs/spring.log
Is it possible to rotate the file every day? I want to create a new file every day.
From the doc:
Log files rotate when they reach 10 MB and, as with console output, ERROR-level, WARN-level, and INFO-level messages are logged by default. Size limits can be changed using the logging.file.max-size property. Previously rotated files are archived indefinitely unless the logging.file.max-history property has been set.
Also, if you just want to log to "/my-logs/app.log", delete logging.path and change logging.file to:
logging.file=/my-logs/app.log
Edit: about rotating the log every day, Spring default logger does not support it, you can use Logback. Create a file called logback-spring.xml in src/main/resources with the following content:
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{0}[%M:%L] - %msg%n</pattern>
</encoder>
</appender>
<appender name="ROTATE_FILE_DAILY" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/my-logs/app.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>app-%d{yyyy-MM-dd}.log</fileNamePattern>
</rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{0}[%M:%L] - %msg%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT"/>
<appender-ref ref="ROTATE_FILE_DAILY"/>
</root></configuration>

why is my logback Daily RollingFileAppender not rolling and still appending to one file?

I use Logback through Lombok and the #Slf4j annotation, within a Spring application, so my logging configuration is in a logback-spring.xml file. The general logging is working, but the problem is I'm trying to create a daily log file, and it is instead continuing to append to the single log file. I used the config from this SO post but it is still not working.
Here is the appender section of my spring-logback.xml file:
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logs/myApp.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d{yyyy-MM-dd_HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>logs/myApp.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- keep 30 days' worth of history -->
<maxHistory>30</maxHistory>
</rollingPolicy>
<triggeringPolicy
class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>2MB</MaxFileSize>
</triggeringPolicy>
</appender>
I then found another SO post here, and tried that configuration as well, but it's still not working. Here's that configuration:
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logs/myApp.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>myApp-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxFileSize>10MB</maxFileSize>
<maxHistory>30</maxHistory>
<totalSizeCap>1GB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%msg%n</pattern>
</encoder>
</appender>
Is it possible that it's due to Spring, and I need to configure it another way?
Did you forget this in your configuration:
<root level="INFO">
<appender-ref ref="FILE"/>
</root>
I guest then won't roll until your log file append new line.

Custom log file for specific packages in Spring boot

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 ?
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).
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>logs/${SPECIAL_FILE_NAME}.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>logs/${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.

How to create rolling log file

I can't seem to get a rolling log file.
My logback.xml config has the following:
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date %level %thread %X{jobid} %logger{20}.%method\(%line\) %msg%n</pattern>
</encoder>
</appender>
<appender name="SIFT" class="ch.qos.logback.classic.sift.SiftingAppender">
<discriminator>
<key>logFileName</key>
<defaultValue>unknown</defaultValue>
</discriminator>
<sift>
<appender name="FILE-${logFileName}" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${catalina.base}/logs/${logFileName}.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${catalina.base}/logs/${logFileName}_%d{yyyy-MM-dd_HH-mm}.%i.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%date %level %thread %X{jobid} %logger{20}.%method\(%line\) %msg%n</pattern>
</encoder>
</appender>
</sift>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
<appender-ref ref="SIFT" />
</root>
Once Tomcat starts and loads the app I can see the log file "unknown.log". That's because I haven't set the logFileName in the code yet.
However, I do not see the rolling files. Even after waiting several minutes.
According to logback documentation the pattern "%d{yyyy-MM-dd_HH-mm}" should create a rolling time policy of every minute.
I am not seeing a new log file created.
Update:
I added the STDOUT appender. Now I see in catalina.out the logs.
Interesting. The entries in the "unknown.log" file from the SIFT stop at the stop of the minute. After that nothing is written to the SIFT file but I keep getting entries in catalina.out:
2015-12-09 16:31:22,269 INFO Thread1 RlQti5b0QYybZvRuHi/m c.l.l.w.Worker.consumeMessage(51) Rcvd: job.complete.
2015-12-09 16:31:49,654 INFO Thread1 mY0E7sV7QZyTRZAw9UwV c.l.l.w.Worker.consumeMessage(51) Rcvd: job.complete.
2015-12-09 16:31:51,812 INFO Thread1 WWjuyzH+RmCa35JcxRJy c.l.l.w.Worker.consumeMessage(51) Rcvd: job.complete.
2015-12-09 16:31:58,977 INFO Thread1 jUdqB1DaQ8KxajDFk6jN c.l.l.w.Worker.consumeMessage(51) Rcvd: job.complete.
Just a guess but - probably because you are not logging anything. They are "lazy" like that.
Removing the %i from the fileNamePattern seems to fix the problem. I guess the index (%i) is only allowed for FixedWindowRollingPolicy.
try this
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<statusListener class="ch.qos.logback.core.status.NopStatusListener" />
<property name="USER_HOME" value="\logs" />
<appender name="FILE-THREAD" class="ch.qos.logback.classic.sift.SiftingAppender">
<!-- This is MDC value -->
<!-- We will assign a value to 'logFileName' via Java code -->
<discriminator>
<key>logFileName</key>
<defaultValue>head0</defaultValue>
</discriminator>
<sift>
<!-- A standard RollingFileAppender, the log file is based on 'logFileName'
at runtime -->
<appender name="FILE-${logFileName}"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${USER_HOME}/${logFileName}.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} %mdc [%thread] %level %logger{35}
- %msg%n
</Pattern>
</encoder>
<rollingPolicy
class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<FileNamePattern>${USER_HOME}/${logFileName}.%i.log.zip
</FileNamePattern>
<MinIndex>1</MinIndex>
<MaxIndex>10</MaxIndex>
</rollingPolicy>
<triggeringPolicy
class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>10MB</MaxFileSize>
</triggeringPolicy>
</appender>
</sift>
</appender>

Resources