0

I'm working on logger implementation and trying to print logger statements in a different pattern using an ERROR level log.

This is the XML config using for printing loggers. Is there any way to add multiple patterns and print the statements based on condition.

<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_HOME:-logs}/${LOG_FILE_NAME:-eportal-daas}.log</file>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>
            %d{yyyy-MM-dd HH:mm:ss} %-5level [%thread] %logger - %M - %L :[trackId=%X{trackId}]|uuid=%uuid|accountName=%X{accountName}|appName=%X{appName}|userName=%X{userName}|exceptionIssueCode=%expRoot|
                :errorMessage=%msg
                :stackTrace=%ex %n
            </Pattern>
        </encoder>
    </appender>

Below is the sample statement using the java logger code

logger.error("********* Application Started Successfully *********");

2020-01-29 13:14:00 ERROR [main] com.apptium.Application - main - 64 :[trackId=]|uuid=66d1caaa-17ae-46c9-abf2-28f0da789353|accountName=|appName=|userName=|exceptionIssueCode=EP03NANIL -> UNEXPECTED ERROR|
                :errorMessage=********* Application Started Successfully *********
                :stackTrace= 

Technically speaking, this does not error message. The root level is configured as an ERROR. I need to print in a different pattern. I'm using slf4j.

Giri
  • 31
  • 7

1 Answers1

0

You can add multiple appender with different patterns and filter for levels:

<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
  <filter class="ch.qos.logback.classic.filter.LevelFilter">
    <level>INFO</level>
    <onMatch>ACCEPT</onMatch>
    <onMismatch>DENY</onMismatch>
  </filter>
  <encoder>
    <Pattern>
  ...
</appender>

<appender name="fout"...
</appender>

<root level="INFO">
  <appender-ref ref="FILE" />
  <appender-ref ref="fout" />
</root>

https://stackoverflow.com/a/5653532/9928822

Peer
  • 3
  • 3
  • Link-only answers are not acceptable by the policy. Would you elaborate on how this addresses the OP's concern? – Yash Jan 29 '20 at 15:55