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
Related
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
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
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>
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.
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