3

I have a logback.xml file which configures how the logging should be done in my spring boot application. I want to change it to a .properties file. But I am unable to find any documentation.

<?xml version="1.0" encoding="UTF-8"?>

<property name="LOG_FILE" value="${user.home}/my_logs/logs"/>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
        <level>INFO</level>
    </filter>
    <layout class="ch.qos.logback.classic.PatternLayout">
        <Pattern>
            %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger %c:%L - %msg%n
        </Pattern>
    </layout>
</appender>

<appender name="FILE-AUDIT"
          class="ch.qos.logback.core.rolling.RollingFileAppender">
    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
        <level>DEBUG</level>
    </filter>
    <file>${LOG_FILE}-debug.log</file>
    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
        <Pattern>
            %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger %c:%L - %msg%n
        </Pattern>
    </encoder>

    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <!-- rollover weekly -->
        <fileNamePattern>${LOG_FILE}-debug.%d{yyyy-MM-dd}.%i.log
        </fileNamePattern>
        <maxHistory>7</maxHistory>
        <timeBasedFileNamingAndTriggeringPolicy
                class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
            <maxFileSize>10MB</maxFileSize>
        </timeBasedFileNamingAndTriggeringPolicy>
    </rollingPolicy>

</appender>

<appender name="FILE-ERROR"
          class="ch.qos.logback.core.rolling.RollingFileAppender">
    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
        <level>ERROR</level>
    </filter>
    <file>${LOG_FILE}-error.log</file>
    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
        <Pattern>
            %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger %c:%L - %msg%n
        </Pattern>
    </encoder>

    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <!-- rollover weekly -->
        <fileNamePattern>${LOG_FILE}-error.%d{yyyy-MM-dd}.%i.log
        </fileNamePattern>
        <maxHistory>7</maxHistory>
        <timeBasedFileNamingAndTriggeringPolicy
                class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
            <maxFileSize>10MB</maxFileSize>
        </timeBasedFileNamingAndTriggeringPolicy>
    </rollingPolicy>

</appender>
<appender name="FILE-INFO"
          class="ch.qos.logback.core.rolling.RollingFileAppender">
    <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
        <level>INFO</level>
    </filter>
    <file>${LOG_FILE}-info.log</file>
    <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
        <Pattern>
            %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger %c:%L - %msg%n
        </Pattern>
    </encoder>

    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <!-- rollover weekly -->
        <fileNamePattern>${LOG_FILE}-info.%d{yyyy-MM-dd}.%i.log
        </fileNamePattern>
        <maxHistory>7</maxHistory>
        <timeBasedFileNamingAndTriggeringPolicy
                class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
            <maxFileSize>10MB</maxFileSize>
        </timeBasedFileNamingAndTriggeringPolicy>
    </rollingPolicy>

</appender>

<root level="ALL">
    <appender-ref ref="STDOUT"/>
    <appender-ref ref="FILE-AUDIT"/>
    <appender-ref ref="FILE-ERROR"/>
    <appender-ref ref="FILE-INFO"/>
</root>

<logger name="com.*******.*****.webservices" level="DEBUG"/>
<logger name="org.springframework" level="WARN"/>
<logger name="springfox.documentation" level="WARN"/>
<logger name="org.hibernate" level="ERROR"/>
<logger name="org.apache.http" level="WARN"/>
<logger name="org.apache.activemq" level="WARN"/>
<logger name="com.zaxxer.hikari" level="WARN"/>
<logger name="net.sf.ehcache" level="WARN"/>
<logger name="org.apache.velocity" level="WARN"/>
<logger name="com.amazonaws" level="WARN"/>
<logger name="org.apache.tomcat" level="WARN"/>

How to convert the above .xml file to .properties file. Is there any documentation or example available. I have a documentation which shows how to configure log4j.properties file, can I use the same?

Naanavanalla
  • 1,412
  • 2
  • 27
  • 52

1 Answers1

5

Logback by itself only supports XML and Groovy for configuration. However, Spring Boot has its own logging configuration which is configured via your application properties file, which is then used for configuring whichever logging framework you are using. Refer to the Logging chapter of the Spring Boot documentation as well as the How-to Logging Guide.

As that How-to guide says, the application properties method of configuration only really handles setting logging levels and optionally writing to a file in addition to the console. If you want to do something more complicated than that, which from the look of your logback.xml file you do, then you need to use a full logging configuration file like you're already doing. There isn't a .properties method of configuring Logback.

Log4j is a different logging system than Logback, so I'm not sure why you'd be looking at log4j documentation unless you wanted to use Log4j instead of Logback. You could certainly remove Logback from your project and use Log4j instead, and then use a Log4j configuration file (such as log4j.properties), and that would let you configure your logging using a properties file rather than an XML file, but I would expect that would be just a small part of the process you'd want to use to decide which logging framework to use for your application.