Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rolling logback logs on filesize and time

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

like image 967
Naftuli Kay Avatar asked Apr 08 '10 18:04

Naftuli Kay


People also ask

What is rolling policy in Logback?

Size-based rolling policy allows to rollover based on file on each log file. For example, we can rollover to a new file when the log file reaches 10 MB in size. The maxFileSize is used to specify the size of each file when it gets rolled over.

Is Logback synchronous?

Yes, it's synchronous by default.

Does Logback use SLF4J?

Logback natively implements the SLF4J API.


2 Answers

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> 
like image 144
Robert H Avatar answered Sep 18 '22 12:09

Robert H


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.

like image 33
AndreLDM Avatar answered Sep 21 '22 12:09

AndreLDM