Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep rolling log file as per file size?

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.

like image 398
john Avatar asked Feb 07 '16 22:02

john


1 Answers

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>
like image 156
pczeus Avatar answered Oct 09 '22 06:10

pczeus