I have introduced logback to a system that uses muliple JVM's. The system is in fact two seperate projects, and previously they shared an external configuration file that looked like this:
<?xml version="1.0" encoding="UTF-8" ?>
<configuration scan="true">
<!-- general console appender -->
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d{HH:mm:ss.SSS,CET} %-5level [%file:%line] - %msg%n</pattern>
</layout>
</encoder>
<target>System.out</target>
</appender>
<!-- Project1 appenders -->
<appender name="project1ApplicationAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<prudent>true</prudent>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>/data/logs/project1/application/%d{yyyy-MM-dd}/application.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!-- or whenever the file size reaches 100MB -->
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<!-- keep 90 days' worth of history -->
<maxHistory>90</maxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>[%-5p] [%d{dd.MM.yyyy - HH.mm.ss.SSS,CET}] [%C{1} - %L] %X{ClientTraceId} %X{CallSequenceTrace} %m%n</Pattern>
</layout>
</encoder>
</appender>
<appender name="project1ConnectorAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<prudent>true</prudent>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>/data/logs/project1/connector/%d{yyyy-MM-dd}/connector.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!-- or whenever the file size reaches 100MB -->
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<!-- keep 90 days' worth of history -->
<maxHistory>90</maxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>[%d{dd.MM.yyyy - HH.mm.ss.SSS,CET}] %m%n</Pattern>
</layout>
</encoder>
</appender>
<!-- project1 loggers -->
<logger name="logger1project1" additivity="false">
<level value="ALL"/>
<appender-ref ref="project1ConnectorAppender"/>
</logger>
<logger name="logger2project1">
<level value="INFO"/>
<appender-ref ref="project1ApplicationAppender"/>
</logger>
<logger name="logger3project1">
<level value="INFO"/>
<appender-ref ref="project1ApplicationAppender"/>
</logger>
<!-- project2 appenders -->
<appender name="project2ApplicationAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<prudent>true</prudent>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>/data/logs/project2/application/%d{yyyy-MM-dd}/application.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!-- or whenever the file size reaches 100MB -->
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<!-- keep 90 days' worth of history -->
<maxHistory>90</maxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>[%-5p] [%d{dd.MM.yyyy - HH.mm.ss.SSS,CET}] [%C{1} - %L] %X{ClientTraceId} %X{CallSequenceTrace} %m%n</Pattern>
</layout>
</encoder>
</appender>
<appender name="project2ConnectorAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<prudent>true</prudent>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>/data/logs/project2/connector/%d{yyyy-MM-dd}/connector.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!-- or whenever the file size reaches 100MB -->
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<!-- keep 30 days' worth of history -->
<maxHistory>90</maxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>[%d{dd.MM.yyyy - HH.mm.ss.SSS,CET}] %m%n</Pattern>
</layout>
</encoder>
</appender>
<!-- project2 loggers -->
<logger name="logger1project2" additivity="false">
<level value="OFF"/>
<appender-ref ref="project2ConnectorAppender"/>
</logger>
<logger name="logger2project2">
<level value="INFO"/>
<appender-ref ref="project2ApplicationAppender"/>
</logger>
<root>
<level value="INFO"/>
<appender-ref ref="console"/>
</root>
</configuration>
This worked like it should.
Then we introduced a new logger that had the same name in both projects, but we still wanted separate files for each project, so we had to split the config file into two.
When we did that, one of the projects stopped logging, and i cannot understand why. This is the configuration file for the project that stopped:
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true">
<!-- general console appender -->
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d{HH:mm:ss.SSS,CET} %-5level [%file:%line] - %msg%n</pattern>
</layout>
</encoder>
<target>System.out</target>
</appender>
<!-- project2 appenders -->
<appender name="project2ApplicationAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<prudent>true</prudent>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>/data/logs/project2/application/%d{yyyy-MM-dd}/application.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!-- or whenever the file size reaches 100MB -->
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<!-- keep 90 days' worth of history -->
<maxHistory>90</maxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>[%-5p] [%d{dd.MM.yyyy - HH.mm.ss.SSS,CET}] [%C{1} - %L] %X{ClientTraceId} %X{CallSequenceTrace} %m%n</Pattern>
</layout>
</encoder>
</appender>
<appender name="project2ConnectorAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<prudent>true</prudent>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>/data/logs/project2/connector/%d{yyyy-MM-dd}/connector.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!-- or whenever the file size reaches 100MB -->
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<!-- keep 90 days' worth of history -->
<maxHistory>90</maxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>[CBM - %-5p] [%d{"yyyy-MM-dd'T'HH:mm:ss.SSSXXX"}] %m%n</pattern>
</layout>
</encoder>
</appender>
<appender name="project2PayloadAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<prudent>true</prudent>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>/data/logs/project2/payload/%d{yyyy-MM-dd}/payload.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!-- or whenever the file size reaches 100MB -->
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<!-- keep 90 days' worth of history -->
<maxHistory>90</maxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>[CBM - %-5p] [%d{"yyyy-MM-dd'T'HH:mm:ss.SSSXXX"}] %m%n</pattern>
</layout>
</encoder>
</appender>
<!-- project2 loggers -->
<logger name="logger1project2" additivity="false">
<level value="INFO" />
<appender-ref ref="project2ConnectorAppender" />
</logger>
<logger name="newlogger" level="TRACE" additivity="false">
<appender-ref ref="project2PayloadAppender" />
</logger>
<logger name="logger2project2" additivity="false">
<level value="DEBUG" />
<appender-ref ref="project2ApplicationAppender" />
</logger>
<root>
<level value="INFO" />
<appender-ref ref="project2ApplicationAppender" />
</root>
</configuration>
I have added the specific paths in the java options in each JVM like this:
-Dlogback.configurationFile={config path}
They were previously the same for each project, but are now separate.
One other piece of information is that the system is deployed on two servers, each with a copy of the config files. And it works perfectly on one of the servers, even tho the config files and deployment process are identical.
I have looked for solutions for an entire workday without any luck.
This is in a live production environment, so i cannot simply do a new deploy of the system, or read status messages from console.
We have test systems, but the issue is not present there, even tho the config files are similar.
Thanks for any answers in advance!
Related
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>
I am trying to create logger policy in a spring based project.
The issue I am facing is related to rolling policy. the logfile.log is created and is working fine but the rolling file rollingfile.log.%d{yyyy-MM-dd}.log is not created.
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>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>${LOG_ARCHIVE}/rollingfile.log.%d{yyyy-MM-dd}.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<logger name="coinPay.logbackxml" level="info" additivity="false">
<appender-ref ref="Console-Appender" />
<appender-ref ref="File-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" />
</root>
</configuration>
any help will be appreciated. Thank you in advance :D
You need to specify ch.qos.logback.core.rolling.RollingFileAppender as class for your appender. A FileAppender can not roll.
I believe you're missing the %i parameter, which should be replaced by the rolling file index (i.e. 'mylog.2017-03-13.0.txt', 'mylog.2017-03-13.1.txt', etc.)
In addition, like Uwe Allner, I'm using RollingFileAppender instead of FileAppender.
(I also suggest adding a totalSizeCap to the configuration, in order to control to total size)
Here's my entire appender configuration:
<appender name="SQL_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_HOME}/mylog.txt</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level- %msg%n
</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<maxHistory>3</maxHistory>
<totalSizeCap>5GB</totalSizeCap>
<fileNamePattern>${LOG_HOME}/archived/mylog.%d{yyyy-MM-dd}.%i.txt.zip</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>20MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
We are using logback as the logging framework for our java project... The logback configuration is as given below
<configuration debug="true">
<property name="LOG_HOME" value="/etc/report-synchronizer" />
<appender name="SIFT" class="ch.qos.logback.classic.sift.SiftingAppender">
<discriminator>
<key>modulename</key>
<defaultValue>unknown</defaultValue>
</discriminator>
<sift>
<appender name="FILE-${modulename}"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>${LOG_HOME}/${modulename}-%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!-- or whenever the file size reaches 5MB -->
<maxFileSize>5MB</maxFileSize>
<!-- Number of days for which the files will be kept -->
<maxHistory>10</maxHistory>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d [%thread] %level %mdc %logger{35} - %msg%n</pattern>
</layout>
</appender>
</sift>
</appender>
<root level="DEBUG">
<appender-ref ref="SIFT" />
</root>
</configuration>
We are using java cassandra driver in the project. All the logs generated by the cassandra driver are getting mixed up with our application logs. Is there any way to separate the cassandra driver logs to a separate file
Thanks in advance
Declare a logger with name com.datastax.driver, a dedicated appender, and additivity set to false. This way you will confine the driver logs to its appender.
The following example should give you a good start:
<logger name="com.datastax.driver" level="INFO" additivity="false">
<appender-ref ref="DRIVER"/>
</logger>
<appender name="DRIVER" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_HOME}/${modulename}-driver.log</file>
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d [%thread] %level %mdc %logger{35} - %msg%n</pattern>
</layout>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>${LOG_HOME}/${modulename}-driver-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!-- or whenever the file size reaches 5MB -->
<maxFileSize>5MB</maxFileSize>
<!-- Number of days for which the files will be kept -->
<maxHistory>10</maxHistory>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
Using log-back for logging.When building project with Maven its creating log files and writing logs but not same, when running Spring Runner test case or deployed on server.Its just creating empty log file.
Following is my Configuration,
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="1000000 seconds">
<appender name="consoleOutput"
class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d{hh:mm:ss} %-5level %c:%M:%L %m%n- %msg%n</pattern>
</layout>
</appender>
<!-- Appender: for logging all the Logs into the Log file -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ALL</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>ACCEPT</onMismatch>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<customFilSystem>true</customFilSystem>
<!-- rollover daily -->
<fileNamePattern>log/LOGFILE-%d{yyyy-MM-dd}.%i.txt.zip</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!-- or whenever the file size reaches 100MB -->
<maxFileSize>100MB</maxFileSize>
<minIndex>1</minIndex>
<maxIndex>2</maxIndex>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<pattern>%d{hh:mm:ss} %-5level [%thread] %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="ALL" additivity="false">
<appender-ref ref="FILE"/>
</root>
<root level="info">
<appender-ref ref="consoleOutput"/>
</root>
<!-- Specify the package or module and reference of respective Appender -->
<logger name="org.springframework" level="OFF" />
</configuration>
I've been trying to set up a simple logback project to roll my log files by date and by filesize, and so far I have been unable to get my appender to roll over to another file. Instead, it writes to the log specified by the <file/> tag.
Here is my logback.xml configuration file:
<?xml version="1.0"?>
<configuration scan="true" scanPeriod="10 seconds">
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
</layout>
</appender>
<appender name="milliroller" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>log/output.log</file>
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
</layout>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>log/mylog-%d{yyyy-MM-dd}.%i.txt</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>1KB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<root level="DEBUG">
<appender-ref ref="stdout"/>
<appender-ref ref="milliroller"/>
</root>
<logger name="com.tkassembled.logback" level="DEBUG"/>
</configuration>
At first glance, it looks like it should work, right? Is there something I'm doing wrong? My entire, buildable project is available in a zip here: http://www.mediafire.com/file/2bxokkdyz2i/logback.zip
Although this is an old question, I felt that a working answer is appropriate to help anyone who requires this kind of implementation.
I use the following logback configuration to provide an HTML log, rolled over by date and filesize, as well as logging to console for debugging output.
Logfiles are stored in a logs directory with a name of logFile.html while its active, and logFile.2013-mm-dd.i.html when it rolls over, where i is the number of 50MB log files. For instance logFile.2013-01-07.0.html.
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<charset>UTF-8</charset>
<pattern>%d{HH:mm:ss.SSS} [%thread] %highlight(%-5level) %cyan(%logger{35}) - %msg %n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logs\logFile.html</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>logs\logFile.%d{yyyy-MM-dd}.%i.html</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!-- or whenever the file size reaches 50MB -->
<maxFileSize>50MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<!-- keep 30 days' worth of history -->
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<charset>UTF-8</charset>
<layout class="ch.qos.logback.classic.html.HTMLLayout">
<pattern>%d{HH:mm:ss.SSS}%thread%level%logger%line%msg</pattern>
</layout>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</root>
</configuration>
Since logback 1.1.7 (released March 2016) a new policy called SizeAndTimeBasedRollingPolicy is available that dramatically simplifies what you need to do:
<appender name="ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>app.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>app-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxFileSize>10MB</maxFileSize>
<maxHistory>30</maxHistory>
<totalSizeCap>1GB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%msg%n</pattern>
</encoder>
</appender>
See here for further info.