how to config spring logback set maxHistory for 1hour? - spring

spring logback config: 1
here got maxHistory save the log for 1 day. but how to make it save 1 hour?
cause there have too many logs if saved 1 day and the disk is full just half day.
<appender name="APPLICATION" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_FILE}</file>
<encoder>
<pattern>${FILE_LOG_PATTERN}</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxHistory>1</maxHistory>
<maxFileSize>512MB</maxFileSize>
<totalSizeCap>5GB</totalSizeCap>
</rollingPolicy>
</appender>

Use
<fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd-HH}.%i.log.zip</fileNamePattern>
it will roll log file every hour into a zip file.
This is NOT about <maxHistory>, but rolling log file every hour.

Related

How to configure appender to roll every 5 minutes in logback

I want to implement appender which rolls every 5 minutes. How can I achieve it?
Here is the code:
Currently, it rolls every 1 minute
<appender name="FILE-AUDIT" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>myfile.log</file>
<encoder>
<pattern>${MY_PATTERN_HERE}</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>myfile.log.%d{dd-MM-yyyy_HH-mm}.%i.gz</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>300MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>1000</maxHistory>
</rollingPolicy>
</appender>

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.

Spring Boot log

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.

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