Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logback different files for different levels

I have this appender in my logback.xml

<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <File>logFile.log</File>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <FileNamePattern>logFile.%d{yyyy-MM-dd}.log</FileNamePattern>
        <maxHistory>5</maxHistory>
    </rollingPolicy>

    <layout class="ch.qos.logback.classic.PatternLayout">
        <Pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{35} - %msg%n</Pattern>
    </layout>
</appender> 

<root>
    <level value="ALL" />
    <appender-ref ref="FILE" />
</root>

So at the moment I am saving all my logs to one file. How can I make it like, one file saves only error logs and other saves all other?

And I want to keep using only 1 instance of logger in my code, something like this:

 private static final Logger log = LoggerFactory.getLogger(Main.class);
like image 773
Jaanus Avatar asked Nov 08 '12 07:11

Jaanus


1 Answers

Started looking for logback categories, found filters.

Just add filter description to your appender:

    <filter class="ch.qos.logback.classic.filter.LevelFilter">
        <level>ERROR</level>
        <onMatch>ACCEPT</onMatch>
        <onMismatch>DENY</onMismatch>
    </filter>
like image 193
Jaanus Avatar answered Sep 19 '22 10:09

Jaanus