2

I am trying to get log output in JSON format. I achieved it by configuring logback.xml file. Is it possible to achieve same by YML file ?

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <layout class="ch.qos.logback.contrib.json.classic.JsonLayout">
        <jsonFormatter class="ch.qos.logback.contrib.jackson.JacksonJsonFormatter">
            <prettyPrint>true</prettyPrint>
        </jsonFormatter>
        <timestampFormat>yyyy-MM-dd' 'HH:mm:ss.SSS</timestampFormat>
    </layout>
</appender>

2 Answers2

5

If your goal is to have different configurations for different environments, you can use spring's profile dependent configuration feature

From my logback-spring.xml:

...
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">

    <springProfile name="dev">
        <encoder>
            <pattern>${CONSOLE_LOG_PATTERN}</pattern>
            <charset>utf8</charset>
        </encoder>
    </springProfile>

    <springProfile name="qa,prod">
        <encoder class="net.logstash.logback.encoder.LogstashEncoder"/>
    </springProfile>

</appender>
...

You should also check the paragraph about the Environment Properties

sebi88
  • 108
  • 1
  • 6
  • I don't want in xml configuration. – Mohammed Imran Jan 22 '19 at 13:50
  • I'm afraid it's not possible to configure an appender only from the YML file and not using a logback.xml or logback-spring.xml file (okay, you [can also do it progmatically](https://stackoverflow.com/questions/16910955/programmatically-configure-logback-appender/43261739) ), why is it important to not have an xml? – sebi88 Jan 23 '19 at 17:15
  • 2
    thanks, I want it in yml file for changing log level. I came to know yml configuration can override logback. So I used logback.xml for json format and yml file for log level – Mohammed Imran Jan 28 '19 at 11:46
2

As mentioned above in comment It is not possible to configure only in YML file for JSON Layout in logback. My requirement was to set log level ERROR/INFO from YML file. I came to know We can override logback log level by YML file in spring boot. For JSON format configuration one can use logback.xml and for setting log level use YML file