How to close springboot default file-appender.xml? - spring-boot

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

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

Spring Boot logback-spring.xml creates a log file under path /var/log even if custom log path is defined in the xml and application.properties files

I prepared a custom RollingFileAppender configuration in the logback-spring.xml file and application.properties files. The log file soduncu.log created under path /var/app/sefa/logs. This is expected behavior for the configuration but there is an unexpected situation that soduncu.log created under default Linux log path /var/log/. I tried to prevent this unexpected log file creation but I couldn't. What is the situation here and what I did wrong?
logback-spring.xml:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<springProperty name="LOG_DIR" source="logging.path" defaultValue="/var/app/sefa/logs">
</springProperty>
<appender name="ROLLING"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_DIR}/soduncu.log</file>
<rollingPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>${LOG_DIR}/soduncu-%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<maxFileSize>10MB</maxFileSize>
<maxHistory>60</maxHistory>
<totalSizeCap>20GB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%date %-4relative [%thread] %-5level %logger{35} - %msg %n</pattern>
</encoder>
</appender>
<root level="WARN">
<appender-ref ref="ROLLING" />
</root>
</configuration>
application.properties:
logging.path=/var/app/sefa/logs
logging.file=/var/app/sefa/logs/soduncu.log
logging.level.root=INFO
logging.level.org.springframework=ERROR
logging.level.com.nuvia=DEBUG
Here is the one Linux process id link to a log file under two different path:
/proc/14368/fd/1 -> /var/log/soduncu.log
/proc/14368/fd/2 -> /var/log/soduncu.log
/proc/14368/fd/6 -> /var/app/sefa/logs/soduncu.log
I am unable to replicate this behavior with your code.
Try to change
name="LOG_DIR" source="logging.path"
to
name="LOG_DIR_PATH" source="log.dir.path"
and see what happens
make sure to delete soduncu.log from var/log first.
I hope it helps.

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

How to configure rolling file appender within Spring Boot's application.yml

Is is possible to configure a daily file appender within the application.yml of a Spring Boot application?
i.e. filenamePattern: myfile.%d{yyyy-MM-dd-HH-mm-ss}.log
I have configuration such as the following in my application.yml file.
logging:
file: /mypath/myfile.log
level:
mypackage: INFO
Thanks
The default file appender is size based (10MB).
In your logback.xml just configure a TimeBasedRollingPolicy as described here
I.e. something like:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<appender name="ROLLIN" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_FILE}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}.log</fileNamePattern>
</rollingPolicy>
</appender>
<root level="INFO">
<appender-ref ref="ROLLIN" />
</root>
<logger name="org.springframework.web" level="INFO"/>
</configuration>
To override the default file appender and change it to daily rollover, you could use a logback-spring.xml looking like this:
<?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/console-appender.xml"/>
<appender name="ROLLING-FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder>
<pattern>${FILE_LOG_PATTERN}</pattern>
</encoder>
<file>${LOG_FILE}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}.log</fileNamePattern>
</rollingPolicy>
</appender>
<root level="INFO">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="ROLLING-FILE"/>
</root>
</configuration>
logging.file.name=MyApp.log
logging.pattern.rolling-file-name=MyApp-%d{yyyy-MM-dd-HH-mm-ss}.%i.log
Working with Spring Boot 2.3.4 and 2.2.10
Not working with Spring Boot 2.1.17
application.yml
logging:
logback:
rollingpolicy:
file-name-pattern: ${LOG_FILE}.%d{yyyy-MM-dd}.%i.gz # by date
max-file-size: 10MB # by size
Works on springboot v.2.5.10
You can also configure rolling policy based on file size in your
logback-spring.xml. In the below, we are specifying max file size as 10MB for SizeBasedTriggeringPolicy:
<?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/console-appender.xml" />
<appender name="ACTUAL_LOG_FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder>
<pattern>${FILE_LOG_PATTERN}</pattern>
</encoder>
<file>${LOG_FILE}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<!-- gz extension to enable file deletion by logrotator -->
<fileNamePattern>${LOG_FILE}.%i.gz</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>10</maxIndex>
</rollingPolicy>
<triggeringPolicy
class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>10MB</MaxFileSize>
</triggeringPolicy>
</appender>
<root level="INFO">
<appender-ref ref="ACTUAL_LOG_FILE" />
</root>
</configuration>
A little late to the party... but I was able to get my log file to roll (by size) with the following configuration in application.yaml and no logback.xml configuration whatsoever:
logging:
file: /var/log/webapps/app/app.log
# Roll the log file when it reaches max size
file.max-size: 1024KB
# Limit the number of log files retained
file.max-history: 50
pattern:
console: "%d %-5level %logger : %msg%n"
file: "%d %-5level [%thread] %logger : %msg%n"
level:
root: info
my.package.of.app: debug
org.springframework: error
# etc. etc.
In the application.properties file add these lines of code, use any these according to your requirement
logging.file.name=myinfo.log
#daily rolling logs
logging.pattern.rolling-file-name=myinfo-%d{yyyy-MM-dd}.%i.log
#per hour rolling logs
logging.pattern.rolling-file-name=myinfo-%d{yyyy-MM-dd-HH}.%i.log
#per minute rolling logs
logging.pattern.rolling-file-name=myinfo-%d{yyyy-MM-dd-HH-mm}.%i.log
#per secs rolling logs
logging.pattern.rolling-file-name=myinfo-%d{yyyy-MM-dd-HH-mm-ss}.%i.log
From this link :-
logging:
file: logs/application-debug.log
pattern:
console: "%d %-5level %logger : %msg%n"
file: "%d %-5level [%thread] %logger : %msg%n"
level:
org.springframework.web: ERROR
com.howtodoinjava: INFO
org.hibernate: ERROR

Resources