Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4j2 : How to configure multiple Appender with same type in Yaml configuration

I'm trying to have 2 RollingRandomAccessFile in the same YAML configuration. I'm able to do it in XML but not in YAML. As a result, I want two files "application.log" and "payload.log".

Here's my working XML configuration :

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="debug">
    <Appenders>

        <Console name="CONSOLE" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>

        <RollingRandomAccessFile name="PAYLOAD" fileName="logs/payload.log"
                                 filePattern="logs/$${date:yyyy-MM}/payload-%d{MM-dd-yyyy}-%i.log.gz">
            <PatternLayout>
                <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n</pattern>
            </PatternLayout>
            <Policies>
                <SizeBasedTriggeringPolicy size="1 KB"/>
            </Policies>
        </RollingRandomAccessFile>

        <RollingRandomAccessFile name="APPLICATION" fileName="logs/application.log"
                                 filePattern="logs/$${date:yyyy-MM}/application-%d{MM-dd-yyyy}-%i.log.gz">
            <PatternLayout>
                <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n</pattern>
            </PatternLayout>
            <Policies>
                <SizeBasedTriggeringPolicy size="1 KB"/>
            </Policies>
        </RollingRandomAccessFile>

    </Appenders>

    <Loggers>
        <Root level="info" additivity="false">
            <appender-ref ref="CONSOLE" level="trace"/>
            <appender-ref ref="APPLICATION" level="trace"/>
            <appender-ref ref="PAYLOAD" level="trace"/>
        </Root>
    </Loggers>

</Configuration>

Here's my non-working YAML configuration. The second declaration of RollingRandomAccessFile overrides the first one, resulting with only "application.log" :

Configuration:
  status: debug

  Appenders:
    Console:
      name: CONSOLE
      target: SYSTEM_OUT
      PatternLayout:
        Pattern: "%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"
    RollingRandomAccessFile:
      name: PAYLOAD
      fileName: logs/payload.log
      filePattern: "logs/$${date:yyyy-MM}/payload-%d{MM-dd-yyyy}-%i.log.gz"
      PatternLayout:
        Pattern: "%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"
      Policies:
        SizeBasedTriggeringPolicy:
          size: 1 KB
    RollingRandomAccessFile:
      name: APPLICATION
      fileName: logs/application.log
      filePattern: "logs/$${date:yyyy-MM}/application-%d{MM-dd-yyyy}-%i.log.gz"
      PatternLayout:
        Pattern: "%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"
      Policies:
        SizeBasedTriggeringPolicy:
          size: 1 KB

  Loggers:
    Root:
      level: debug
      AppenderRef:
        - ref: CONSOLE
        - ref: PAYLOAD
        - ref: APPLICATION

Here's what I tried using List at the Appender level and with that there's no ouput, even the console :

Configuration:
  status: debug

  Appenders:
    - Console:
      name: CONSOLE
      target: SYSTEM_OUT
      PatternLayout:
        Pattern: "%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"
    - RollingRandomAccessFile:
      name: PAYLOAD
      fileName: logs/payload.log
      filePattern: "logs/$${date:yyyy-MM}/payload-%d{MM-dd-yyyy}-%i.log.gz"
      PatternLayout:
        Pattern: "%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"
      Policies:
        SizeBasedTriggeringPolicy:
          size: 1 KB
    - RollingRandomAccessFile:
      name: APPLICATION
      fileName: logs/application.log
      filePattern: "logs/$${date:yyyy-MM}/application-%d{MM-dd-yyyy}-%i.log.gz"
      PatternLayout:
        Pattern: "%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"
      Policies:
        SizeBasedTriggeringPolicy:
          size: 1 KB

  Loggers:
    Root:
      level: debug
      AppenderRef:
        - ref: CONSOLE
        - ref: PAYLOAD
        - ref: APPLICATION

If I replace the second RollingRandomAccessFile by a RollingFile, it works perfectly.

like image 655
Daniel Marcotte Avatar asked May 05 '15 19:05

Daniel Marcotte


People also ask

How do I use console Appenders in Log4j2?

Log4j2 ConsoleAppender ConfigurationThe target poperty specifies the target of the logging messages i.e. SYSTEM_OUT or SYSTEM_ERR . The follow attribute tells whether the appender should honor the reassignments of System. out or System. err made after the logging configuration has been initialized.

What is rolling file Appender in Log4j2?

Log4j2 RollingFileAppender is an OutputStreamAppender that writes log messages to files, following a configured triggering policy about when a rollover (backup) should occur. It also has a configured rollover strategy about how to rollover the file.

What is configuration status in Log4j2?

Configuration: the root element of a log4j2 configuration file; the status attribute represents the level at which internal log4j events should be logged. Appenders: this element contains a list of appenders; in our example, an appender corresponding to the System console is defined.


2 Answers

I finally found the solution, was misusing the list

Configuration:
  status: debug

  Appenders:
    Console:
      name: CONSOLE
      target: SYSTEM_OUT
      PatternLayout:
        Pattern: "%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"
    RollingRandomAccessFile:
      - name: PAYLOAD
        fileName: logs/payload.log
        filePattern: "logs/$${date:yyyy-MM}/payload-%d{MM-dd-yyyy}-%i.log.gz"
        PatternLayout:
          Pattern: "%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"
        Policies:
          SizeBasedTriggeringPolicy:
            size: 1 KB
      - name: APPLICATION
        fileName: logs/application.log
        filePattern: "logs/$${date:yyyy-MM}/application-%d{MM-dd-yyyy}-%i.log.gz"
        PatternLayout:
          Pattern: "%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"
        Policies:
          SizeBasedTriggeringPolicy:
            size: 1 KB

  Loggers:
    Root:
      level: debug
      AppenderRef:
        - ref: CONSOLE
        - ref: PAYLOAD
        - ref: APPLICATION
like image 92
Daniel Marcotte Avatar answered Oct 10 '22 16:10

Daniel Marcotte


I had to set different threshold levels for each of the Logger and this is how it looks:

---
Configuration:
  status: info
  name: My Application Logger
  thresholdFilter:
    level: debug
  appenders:
    Console:
      name: STDOUT
      PatternLayout:
        Pattern: "%d %p %c{1.} [%t] %m%n"
    RollingRandomAccessFile:
      name: RollingRandomAccessFile
      fileName: logs/app.log
      filePattern: logs/archive/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log.gz
      PatternLayout:
        Pattern: "%d %p %c{1.} [%t] %m%n"
      Policies:
        SizeBasedTriggeringPolicy:
          size: 20 MB
  Loggers:
    logger:
      - name: org.apache.logging.log4j.test2
        level: debug
        additivity: false
        AppenderRef:
          - ref: RollingRandomAccessFile
    Root:
      level: debug
      AppenderRef:
        - ref: RollingRandomAccessFile
          level: warn
        - ref: STDOUT
...

The default log level becomes that configured to Root Logger. If you want to override the level, add the level to each AppenderRef.

like image 20
James Jithin Avatar answered Oct 10 '22 17:10

James Jithin