Create new Spring log file every day - spring

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>

Related

How to clear log file before executing spring boot app?

I have been using the default spring logging configuration where I only specify filename in the application.properties file.
logging.file.name=app.log
But this by default appends logs when I start the application from cmd line "java -jar abc.jar"
I tried to search for the property which clears the file before starting application every time but couldn't find it. How should I clear the log file before starting the app?
Spring Boot uses Logback framework as as a default Logger.
You can use environnement variable via application.properties file to set some logging properties but logback xml configuration provides more powerfull features.
When a file in the classpath has one of the following names, Spring Boot will automatically load it over the default configuration:
logback-spring.xml
logback.xml
logback-spring.groovy
logback.groovy
So You can just put the code snippet below in src/main/resources/logback-spring.xml file
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="Console"
class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%black(%d{ISO8601}) %highlight(%-5level) [%blue(%t)] %yellow(%C{1.}): %msg%n%throwable
</Pattern>
</layout>
</appender>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>app.log</file>
<append>false</append>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<!-- LOG everything at INFO level -->
<root level="info">
<appender-ref ref="FILE" />
<appender-ref ref="Console" />
</root>
</configuration>
The line <append>false</append> does the job.
if you wanna log more information than those avaible with the encoder pattern <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern> please check logback documention https://logback.qos.ch/manual/layouts.html#logback-access

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.

How to print logs for 2 classes in 2 different files in spring boot

I have set logging.file=C:/usr/local/tomcat/logs/hib.log in application.properties and setting like below in class
private static final Logger logger = LogManager.getLogger(ChargeMasterController.class);
logger.info("Total time taken for is ---------------------------" + time + " ms");
Logs are getting printed fine in the path mentioned as logging.file.
Now I want to print my logs in 2 different files for 2 different classes(In same package), how can I set 2 logging.file in application.properties
You can't do that with the configuration Spring provides. But since spring-boot-starter-logging uses Logback by default, you can use a Logback configuration in stead and define multiple appenders, for example:
<appender name="FILE1" class="ch.qos.logback.core.FileAppender">
<file>log1.log</file>
<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="FILE2" class="ch.qos.logback.core.FileAppender">
<file>log2.log</file>
<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="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>
After that, you could use the FILE1 appender for your first class, FILE2 appender for your second class, and STDOUT for all other classes. This can be done by defining the loggers:
<logger name="com.example.pkg.ClassName1" additivity="false" level="info">
<appender-ref ref="FILE1" />
</logger>
<logger name="com.example.pkg.ClassName2" additivity="false" level="info">
<appender-ref ref="FILE2" />
</logger>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
As you can see, there are two loggers next to the root logger, called com.example.pkg.ClassName1 and com.example.pkg.ClassName2. These should match the names of the classes that should log to separate files.
The additivity="false" is important to make sure that logs that go to log1.log or log2.log aren't also sent to the console since they would also match the root logger.
This configuration can be placed within a logback.xml file on your classpath (eg. src/main/resources). Alternatively, you can also configure the location by using the logging.config property within application.properties. For example:
logging.config=file:/path/to/logback.conf
Or if you want to use an arbitrary location on your classpath:
logging.config=classpath:my/logback.conf

Logging pattern in spring boot application

How do I set the logging file pattern to something like server.log.2017-12-22.gz?
As of now in my application.properties file, I have set the logging pattern to:
logging.pattern.file= "%d{yyyy-MM-dd } [%thread] %-5level %logger{36} - %msg%n"
logging.file=/var/opt/VS_Logs/server.log
But I need to store the files in the following format: server.log.2017-12-22.gz
As soon as you want custom rolling and triggering policies, you can no longer rely on Spring boot's logging configuration, and you have to use the vendor specific logging configuration. Here is an example using Logback and a TimeBasedRollingPolicy:
<configuration>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>server.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>server.log.%d{yyyy-MM-dd}.gz</fileNamePattern>
<maxHistory>30</maxHistory>
<totalSizeCap>3GB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd } [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="FILE" />
</root>
</configuration>
Logback will automatically gzip it when you use the .gz extension. If you save this file as logback.xml and put it on your classpath, Spring boot will automatically detect it, otherwise, you can use the logging.config property:
logging.config=classpath:logback.xml

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.

Resources