Steve
Steve

Reputation: 4695

Conditionally format logs in logback

I'm currently formatting my logs in logback like below. However, when I run my app locally I don't want all this metadata gunking up my logs, I just wanna see the message. How can I say "If an environment variable = 'local' then format this way, else format as below"?

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <Target>System.out</Target>
        <encoder>
            <Pattern>
                date=%d{yyyy-MM-dd HH:mm:ss} | rte=${RTEID} | runId=%X{RunId} | interface=%X{Interface} | class=%class{0}.%method | level=%-5level | message=%msg %replace(%xException){'\n','\u2028'}%nopex%n
            </Pattern>
        </encoder>
    </appender>

Upvotes: 1

Views: 426

Answers (1)

Cliff Pereira
Cliff Pereira

Reputation: 473

bit late. But if you are using spring boot you can use logback-spring.xml

and inside you can do something like

<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
        <springProfile name="dev-local">
            <pattern>FIST PATTERN</pattern>
        </springProfile>
        <springProfile name="!dev-local">
            <pattern>SECOND PATTERN</pattern>
        </springProfile>
    </encoder>
</appender>

Upvotes: 2

Related Questions