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.
Related
In my spring boot project,I am using spring logback.xml. I have a use case in which I have to roll over the file based on file size and date whichever is first and also I need to keep the count of rolled over files to 2 only. I was thinking to use following :
<configuration>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>test.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>test.%i.log.zip</fileNamePattern>
<maxIndex>2</maxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>5MB</maxFileSize>
</triggeringPolicy>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="FILE" />
</root>
</configuration>
But the above configuration achieves the use case rolling file based on filesize and keeping max count of rolled files to 2. But it does not do the rolling based on date. I need a rolling based on date also, in addition to file size while keeping the number of rolled files to 2. Is there any way to achieve this?
I think below should work. TimeBasedRollingPolicy uses pattern you provide for fileNamePattern which decides whether to append the data to existing file or new one.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/logs/${appName}/${HOSTNAME}.log</file>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- Rollover every day -->
<fileNamePattern>/logs/${appName}/${HOSTNAME}.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxHistory>2</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>5MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<root level="info">
<appender-ref ref="FILE" />
</root>
</configuration>
I'm using logback to save all my INFO into log file and every day I would like to save this file in a specific folder archived
This is how I have configured it:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<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>${CATALINA_HOME}/logs/DartFleetViewer.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} - %msg%n
</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>${CATALINA_HOME}/logs/archived/DartFleetViewer.%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>30</maxHistory>
</rollingPolicy>
</appender>
<!-- <appender name="FILE-ERROR"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${DEV_HOME}/error.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} - %msg%n
</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
rollover daily
<fileNamePattern>${DEV_HOME}/archived/error.%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender> -->
<!-- Send logs to both console and file audit -->
<root level="info">
<appender-ref ref="FILE-AUDIT" />
<appender-ref ref="STDOUT" />
</root>
<!-- <root level="error">
<appender-ref ref="FILE-ERROR" />
</root> -->
</configuration>
The odd thing is that I find this in my folder
almost all of the files are empty or with few code lines, for example I don't find nothing about yesterday, In the last file I only find
2016-08-02 02:18:06 - SCHEDULED ACTIVITY TO DELETE OLD NOTIFICATIONS
2016-08-02 05:18:06 - SCHEDULED ACTIVITY TO DELETE OLD NOTIFICATIONS
2016-08-02 08:18:06 - SCHEDULED ACTIVITY TO DELETE OLD NOTIFICATIONS
This is information about today and not yesterday.
Furthermore yesterday I had some errors, warnings and a lot of info log.
Do you see some error in my configuration file?
UPDATE:
Since I receive warning during deployment I have changed my logback configuration in order to avoid all warning and I use only time rollback. It seems to work:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
</Pattern>
</encoder>
</appender>
<appender name="FILE-AUDIT"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${CATALINA_HOME}/logs/DartFleetViewer.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>${CATALINA_HOME}/logs/archived/DartFleetViewer.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory>
<totalSizeCap>10GB</totalSizeCap>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>
%d{yyyy-MM-dd HH:mm:ss} - %msg%n
</pattern>
</encoder>
</appender>
<!-- Send logs to both console and file audit -->
<root level="info">
<appender-ref ref="FILE-AUDIT" />
<appender-ref ref="STDOUT" />
</root>
<!-- <root level="error">
<appender-ref ref="FILE-ERROR" />
</root> -->
</configuration>
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>
I am working on a project in which I am logging bunch of stuff in a file and I want to make sure my log file is getting rolled as soon as a fixed limit for file is reached. I have a below logback.xml file but it looks like file size is not working. I see my file size as 793M but limit I have is 100M
<configuration>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>process.log</file>
<triggeringPolicy
class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>100MB</maxFileSize>
</triggeringPolicy>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>process%i.log</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>9</maxIndex>
</rollingPolicy>
<encoder>
<pattern>%date %level [%thread] %msg%n</pattern>
<!-- this improves logging throughput -->
<immediateFlush>true</immediateFlush>
</encoder>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder
by default -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="FILE" />
<appender-ref ref="STDOUT" />
</root>
</configuration>
What wrong I am doing here? And also what is the best policy we should follow in production in terms of logging? We are logging bunch of stuff in a file and we don't want to fill up the disk with this log file.
Instead of FileAppender, you should be using a RollingFileAppender.
See: http://logback.qos.ch/manual/appenders.html
You are specifying settings/properties that are intended to be used by the RollingFileAppender and are ignored by FileAppender.
For a good example usage and configuration, refer to this link:
http://examples.javacodegeeks.com/enterprise-java/logback/logback-rollingfileappender-example/
Sample logback.xml using a RollingFileAppender and ConsoleAppender. The RollingFileAppender is both size and time based:
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true">
<appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<charset>UTF-8</charset>
<Pattern>%d %-4relative [%thread] %-5level %logger{35} - %msg%n</Pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/srv/logs/application.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>logFile.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>5MB</maxFileSize>
</triggeringPolicy>
<encoder>
<charset>UTF-8</charset>
<pattern>%d %-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="consoleAppender" />
<appender-ref ref="FILE"/>
</root>
</configuration>
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>