How to keep rolling log file as per file size? - java

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>

Related

Is there any triggering policy like SizeAndTimeBasedTriggeringPolicy with FixedWindowRollingPolicy in logback.xml?

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>

Logback configuration into Java project

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>

Logback : different different files for different log levels

I know that this type of question have been answered, but in my case i have tried every config and still doesn't work. I need a fresh view to my config (I am sure i'm missing something). both of the appenders log all levels
I want to log info >= for all packages to the console, and the errors only the erros to log file. In my case, both of them log info
Here is config.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- CONSOLE -->
<property name="LOG_PATTERN" value="%d [%thread] %-5level %logger{36} - %msg%n" />
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<charset>UTF-8</charset>
<pattern>${LOG_PATTERN}</pattern>
</encoder>
</appender>
<!-- FILE FOR ERROR ONLY -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/Users/dev/Desktop/JAC/logs/frontend/errors.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>/Users/dev/Desktop/JAC/logs/frontend/errors_%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>5MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<charset>UTF-8</charset>
<pattern>${LOG_PATTERN}</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="CONSOLE" />
</root>
<root level="INFO">
<appender-ref ref="FILE" />
</root>
</configuration>
Thanks in advance
May be, it's not the more beautiful solution. But, in this case, log level INFO is logged in the console, and error is logged in a file. The debug level will be not printed. And INFO and ERROR are in only one exit.
And my application package is fr.myuser.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="DEV_HOME" value="/home/myUser" />
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<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-ERROR"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<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}/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 console -->
<logger name="fr.myuser" level="INFO">
<appender-ref ref="STDOUT" />
</logger>
<root level="ERROR">
<appender-ref ref="FILE-ERROR" />
</root>
</configuration>

Logback's debug output not shown

I'm running a Java servlet inside Tomcat 7.0.52.
This is my logback.xml file
<configuration debug="true">
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>/home/my-user/my-log.log</file>
<append>true</append>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="FILE" />
</root>
</configuration>
Unfortunately I'm nowhere seeing Logback's debug output. Neither in catalina.out, nor in my-log.log or on the console. Where is it?
Just to add: Changes in logback.xml are reflected in the logging output.
In your logback configuration, you define a file appender. What you do not do is define a ConsoleAppender, which will log to stdout/stderr.
catalina.out being the redirection of stdout/stderr used by the Tomcat init script, adding this to your configuration should solve your problem.
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<target>System.out</target>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>debug</level>
</filter>
<encoder>
<pattern><![CDATA[
%-20(%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread]) %-5level %logger{80}[%L] - %msg%n
]]></pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>/home/my-user/my-log.log</file>
<append>true</append>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="STDOUT"/>
<appender-ref ref="FILE" />
</root>

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

Categories