14

I've been reading the logback manual for 2 hours and still can't figure how to do what I need.

It is as simple as the title says: I want to log only the errors to a file, and the other levels (including ERROR) to console.

This is the root section of my logcat.xml file:

    <root level="TRACE" >
        <appender-ref ref="CONSOLE_APPENDER" />
        <appender-ref ref="FILE_APPENDER" />
    </root>

The problem with this configuration is that it logs every level >= TRACE to both appenders.

I could let the root with only console, and define a file logger:

    <logger name='file_logger' level='ERROR' >
        <appender-ref ref="FILE_APPENDER" />
    </logger>

But then I'd have to call the normal logger like this:

LoggerFactory.getLogger(ClientClass.class);

And the file logger like this:

LoggerFactory.getLogger("file_logger");

I don't wan't to choose the logger for each class. I just want to get the root logger from the factory using the class as parameter, and have it do the correct thing depending on the level.

Is this possible?

Mister Smith
  • 27,417
  • 21
  • 110
  • 193
  • Does this answer your question? [How to create 2 different ROOT loggers with logback?](https://stackoverflow.com/questions/18827633/how-to-create-2-different-root-loggers-with-logback) – rogerdpack Dec 19 '19 at 16:07

3 Answers3

25

Put this into your file appender definition:

<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
    <level>ERROR</level>
</filter>

The ThresholdFilter is in logback-classic.jar.

steffen
  • 16,138
  • 4
  • 42
  • 81
  • 2
    Nice. I'm using [logback for android](https://github.com/tony19/logback-android) and seems to work too. – Mister Smith Oct 30 '13 at 17:28
  • 3
    This can also help; XSD for logback.xml. That can help you with configuration variables. https://github.com/enricopulatzo/logback-XSD – az3 Nov 21 '14 at 14:21
  • Thanks, you approach worked. However, this defeats the whole purpose of `level` attribute on the `root` tag, since this its primary feature - determine log level of the appender. – skryvets Jul 21 '19 at 23:21
7

I don't understand why wrong answer here is upvoted. The guy wants ONLY error messages in his file.

Here is the correct answer:

<filter class="ch.qos.logback.classic.filter.LevelFilter">
  <level>ERROR</level>
  <onMatch>ACCEPT</onMatch>
  <onMismatch>DENY</onMismatch>
</filter>

Reference: https://logback.qos.ch/manual/filters.html#levelFilter

Robin
  • 40
  • 5
  • It is because the solution upvoted uses ThresholdFilter that filters everything bellow error. – Kratos Mar 06 '19 at 08:02
  • I understand that. I don't understand how does it address the problem stated by the author. Author wants to log ONLY errors into the file. – Anton Pryamostanov Mar 07 '19 at 11:12
  • Good point. The rest of us assumed he wanted "error and above" :) update: it appears in logback land ERROR is the highest level, unlike log4j that has "fatal" go figure...http://logback.qos.ch/manual/architecture.html#basic_selection – rogerdpack Dec 19 '19 at 16:06
  • I think the opposite of this is what the answer should be. ACCEPT and DENY should exchange their places – Hardik Rana May 04 '20 at 17:20
  • There are developers that understand the problem behind a description and there are othrs that take things literally. It's obvious that the op wanted to have his error messages persisted. So if there would be a FATAL level, it's pretty clear that these should go into that logfile too. That's the whole point of log _levels_. Funny conversation later: "Fatal messages? Nooo, we can't see them any more - you wanted to persist only errors" :) – steffen Mar 05 '22 at 10:50
2

Refer below code:

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>INFO</level>
            <onMatch>DENY</onMatch>
            <onMismatch>ACCEPT</onMismatch>
        </filter>
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>
Hardik Rana
  • 476
  • 1
  • 6
  • 16