I have three microservices in Spring Boot, all of them have the same rolling policies.
The first time in the day that I use them it creates a new log file but this happens three times, each time depending on the microservice that needs to log.
For example when I log in into the application it correctly creates a new log file with the informations in it and rename the one from the previous day and change its location.
When i log out, that is an operation from another microservice, it creates another log file with only that information and the previous data from the login has been sent to the log file in the archived directory.
This happens only the first time that I use the microservices, then everything works fine.
These are my configuration files, thank you in advance:
logback-spring.xml (1):
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="LOGS" value="./logs" />
<appender name="Console"
class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%white(%d{ISO8601}) %highlight(%-5level) [%blue(%t)] %yellow(%C{1.}): %msg%n%throwable
</Pattern>
</layout>
</appender>
<appender name="HistoricLog"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOGS}/historic.log</file>
<encoder
class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
</encoder>
<rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily and when the file reaches 10 MegaBytes -->
<fileNamePattern>${LOGS}/historicArchived/spring-boot-logger-%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<appender name="ApplicativeLog"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOGS}/applicative.log</file>
<encoder
class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
</encoder>
<rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily and when the file reaches 10 MegaBytes -->
<fileNamePattern>${LOGS}/applicativeArchived/spring-boot-logger-%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<root level="info">
<appender-ref ref="Console" />
</root>
<logger name="loggerHistoric" level="info" additivity="false">
<appender-ref ref="HistoricLog" />
</logger>
<logger name="loggerApplicative" level="error" additivity="false">
<appender-ref ref="ApplicativeLog" />
</logger>
</configuration>
logback-spring.xml (2):
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="LOGS" value="./logs" />
<appender name="Console"
class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%white(%d{ISO8601}) %highlight(%-5level) [%blue(%t)] %yellow(%C{1.}): %msg%n%throwable
</Pattern>
</layout>
</appender>
<appender name="HistoricLog"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOGS}/historic.log</file>
<encoder
class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
</encoder>
<rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily and when the file reaches 10 MegaBytes -->
<fileNamePattern>${LOGS}/historicArchived/spring-boot-logger-%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<appender name="ApplicativeLog"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOGS}/applicative.log</file>
<encoder
class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
</encoder>
<rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily and when the file reaches 10 MegaBytes -->
<fileNamePattern>${LOGS}/applicativeArchived/spring-boot-logger-%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<root level="info">
<appender-ref ref="Console" />
</root>
<logger name="loggerHistoricJSON" level="info" additivity="false">
<appender-ref ref="HistoricLog" />
</logger>
<logger name="loggerApplicativeJSON" level="error" additivity="false">
<appender-ref ref="ApplicativeLog" />
</logger>
<logger name="loggerHistoricSingle" level="info" additivity="false">
<appender-ref ref="HistoricLog" />
</logger>
<logger name="loggerApplicativeSingle" level="error" additivity="false">
<appender-ref ref="ApplicativeLog" />
</logger>
</configuration>
logback-spring.xml (3):
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="LOGS" value="./logs" />
<appender name="Console"
class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%white(%d{ISO8601}) %highlight(%-5level) [%blue(%t)] %yellow(%C{1.}): %msg%n%throwable
</Pattern>
</layout>
</appender>
<appender name="HistoricLog"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOGS}/historic.log</file>
<encoder
class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
</encoder>
<rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily and when the file reaches 10 MegaBytes -->
<fileNamePattern>${LOGS}/historicArchived/spring-boot-logger-%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<root level="info">
<appender-ref ref="Console" />
</root>
<logger name="loggerHistoric" level="info" additivity="false">
<appender-ref ref="HistoricLog" />
</logger>
<logger name="loggerApplicative" level="info" additivity="false">
<appender-ref ref="HistoricLog" />
</logger>
<logger name="loggerLogout" level="info" additivity="false">
<appender-ref ref="HistoricLog" />
</logger>
</configuration>
Related
How can I convert logback.xml file to properties?
For example, there is such a configuration file or the code I have attached below. I need to write similar properties in application.properties
Are there examples for such configurations?
is it correct to use the example from this question?
<configuration>
<timestamp key="today" datePattern="yyyy-MM-dd"/>
<property name="LOG_HOME" value="log"/>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<Pattern>
%black(%d{ISO8601}) %highlight(%-5level) [%blue(%t)] %yellow(%C{1.}): %msg%n%throwable
</Pattern>
</encoder>
</appender>
<appender name="FILE-AUDIT" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_HOME}/board_of_ads-${today}.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>${LOG_HOME}/archive/board_of_ads-log_%d{yyyy-MM-dd}.%i.txt</fileNamePattern>
<!-- each file should be at most 20MB, keep 30 days worth of history, but at most 1GB -->
<maxFileSize>20MB</maxFileSize>
<maxHistory>30</maxHistory>
<totalSizeCap>1GB</totalSizeCap>
</rollingPolicy>
<encoder>
<Pattern>
%d{DEFAULT} %-5level [%t] %c: %msg%n
</Pattern>
</encoder>
</appender>
<appender name="AuthController-Audit"
class="ch.qos.logback.core.FileAppender">
<file>${SIMPLE_CONTROLLER_HOME}/auth-controller/AuthController-log_${today}.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d{DEFAULT} %level - %msg%n
</Pattern>
</encoder>
</appender>
<logger name="com.board_of_ads" level="INFO" additivity="false">
<appender-ref ref="FILE-AUDIT"/>
<appender-ref ref="STDOUT"/>
</logger>
<!-- Controllers loggers setup-->
<logger name="com.board_of_ads.controllers.simple.AuthController" level="INFO" additivity="true">
<appender-ref ref="AuthController-Audit"/>
</logger>
<logger name="com.board_of_ads.controllers.rest.NotificationRestController" level="DEBUG" additivity="true">
<appender-ref ref="NotificationRestController-Audit"/>
<appender-ref ref="STDOUT"/>
</logger>
</configuration>
I am trying to save logs of my spring-boot application for each date in a different directory with the current date, month and year.
I have tried using timestamps it stores the logs in the separate directory only if we restart the tomcat server every day.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<logger name="org.hibernate" level="ERROR" />
<logger name="org.mongodb" level="ERROR" />
<property name="DIRECTORY" value="/home/Properties/logs" />
<timestamp key="year" datePattern="yyyy" />
<timestamp key="month" datePattern="MM" />
<timestamp key="date" datePattern="dd" />
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>[%d{yyyy-MM-dd HH:mm:ss.sss}][%-5p][%C][%t][%L]%m%n
</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>${DIRECTORY}/${year}/${month}/${date}/logfile.log</file>
<encoder>
<pattern>[%d{yyyy-MM-dd HH:mm:ss.sss}][%-5p][%C][%t][%L]%m%n
</pattern>
</encoder>
</appender>
<root level="info">
<appender-ref ref="FILE" />
<appender-ref ref="STDOUT" />
</root>
</configuration>
I want my logs for each date to be stored in a directory created every day without the need of restarting the server in the manner similar as [year]\[month]\[date]\logfile.log
Found solution to my own question, if any of you find a better solution please let me know.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="DIRECTORY" value="/home/Properties/logs" />
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>[%d{yyyy-MM-dd HH:mm:ss.sss}][%-5p][%C][%t][%L]%m%n
</pattern>
</encoder>
</appender>
<appender name="FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${DIRECTORY}/logfile.log</file>
<rollingPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>${DIRECTORY}/%d{yyyy/MM/dd, aux}/logfile-%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<maxFileSize>50MB</maxFileSize>
</rollingPolicy>
<encoder>
<pattern>[%d{yyyy-MM-dd HH:mm:ss.sss}][%-5p][%C][%t][%L]%m%n
</pattern>
</encoder>
</appender>
<root level="info">
<appender-ref ref="FILE" />
<appender-ref ref="STDOUT" />
</root>
</configuration>
Using RollingFileAppender in conjunction with TimeBasedRollingPolicy, And set fileNamePattern to %d{yyyy/MM/dd}/logFile.log. See this link for more details
<configuration>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>%d{yyyy/MM/dd}/logFile.log</fileNamePattern>
<!-- keep 30 days worth of history capped at 3GB total size -->
<maxHistory>30</maxHistory>
<totalSizeCap>3GB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="FILE" />
</root>
</configuration>
I creating multiple log file in Springboot , log-back.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="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>${HOME}/abc.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${HOME}/abc.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
// another log file
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${HOME}mon.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${HOME}/mon.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<logger name="com.rh.project" level="debug">
<appender-ref ref="STDOUT" />
<appender-ref ref="SYSLOG" />
<appender-ref ref="FILE-AUDIT" />
</logger>
<logger name="mail-log" level="debug">
<appender-ref ref="STDOUT" />
<appender-ref ref="SYSLOG" />
<appender-ref ref="FILE" />
</logger>
</configuration>
When I check inside log directory, I only saw abc.log , but no mon.log
Did you notice the slash missing inside in second logger??
<file>${HOME}/mon.log</file>
I believe that's the problem. However, nothing to do with spring boot :)
I am trying to create logger policy in a spring based project.
In below logger I wish to created a rolling policy which creates a new file when rollingfile.log exceds more than 10 Mb. So I can have backup of logs without having one single huge logger file. Is there any way to do that?
Given below is my logback.xml.
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true" scan="true" scanPeriod="30 seconds">
<property name="LOG_PATH" value="D:/coinLogs" />
<property name="LOG_ARCHIVE" value="${LOG_PATH}/archive" />
<appender name="Console-Appender" class="ch.qos.logback.core.ConsoleAppender">
<layout>
<pattern>[%d{yyyy-MM-dd HH:mm:ss}] - [%X{requestId}] - %p %c -- %m%n
</pattern>
</layout>
</appender>
<appender name="File-Appender" class="ch.qos.logback.core.FileAppender">
<file>${LOG_PATH}/logfile.log</file>
<encoder>
<pattern>[%d{yyyy-MM-dd HH:mm:ss}] - [%X{requestId}] - %p %c -- %m%n
</pattern>
<outputPatternAsHeader>true</outputPatternAsHeader>
</encoder>
</appender>
<appender name="RollingFile-Appender"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/rollingfile.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_ARCHIVE}/rollingfile.log%d{yyyy-MM-dd}.log
</fileNamePattern>
<maxHistory>30</maxHistory>
<totalSizeCap>10MB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%msg%n</pattern>
</encoder>
</appender>
<appender name="Async-Appender" class="ch.qos.logback.classic.AsyncAppender">
<appender-ref ref="RollingFile-Appender" />
</appender>
<logger name="coinPay.logbackxml" level="info" additivity="false">
<appender-ref ref="Console-Appender" />
<appender-ref ref="File-Appender" />
<appender-ref ref="Async-Appender" />
</logger>
<!-- To remove extra hibernate logs -->
<logger name="org.hibernate">
<level value="info" />
</logger>
<root>
<appender-ref ref="Console-Appender" />
<appender-ref ref="File-Appender" />
<appender-ref ref="Async-Appender" />
</root>
</configuration>
The total size cap is not doing what you want.
<totalSizeCap>10MB</totalSizeCap>
It caps the total size of all your archived log files. You could have a look at TimeBasedArchiveRemover::capTotalSize to understand what it is doing.
What you need to do is change TimeBasedRollingPolicy to SizeAndTimeBasedRollingPolicy and set <maxFileSize>10MB</maxFileSize>
So your configuration should include:
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${LOG_ARCHIVE}/rollingfile.log%d{yyyy-MM-dd}.log
</fileNamePattern>
<maxFileSize>10MB<maxFileSize>
<maxHistory>30</maxHistory>
</rollingPolicy>
I have an app with the following logback config
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level{5} %logger - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${HOME}/app.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${HOME}/old/app-%d{yyyy-MM-dd}.%i.gz</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>14</maxHistory>
</rollingPolicy>
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n</pattern>
</encoder>
</appender>
<appender name="EXCEPTIONS" class="com.software.nagibator.emulator.logging.ExceptionsAppender">
<connectionSource class="ch.qos.logback.core.db.JNDIConnectionSource">
<jndiLocation>jdbc/AppExceptions</jndiLocation>
</connectionSource>
</appender>
<logger name="FileLogger" level="DEBUG" additivity="false">
<appender-ref ref="FILE" />
</logger>
<logger name="ExceptionsLogger" level="DEBUG" additivity="false">
<appender-ref ref="EXCEPTIONS" />
</logger>
<root level="DEBUG">
<appender-ref ref="STDOUT" />
</root>
</configuration>
I use this config on two ubuntu servers. On the first server the config works as expected (it writes the log to file). However, on the second server logback does not write the log to file.
Is there something wrong with my config or is the error elsewhere in the project?
</rollingPolicy>
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
...
It's look like you miss < !-- before comment in xml.