7

I am writing a custom spring boot starter project for logging. My logback-spring.xml become large and want to put the appenders in separate file (console-appender.xml and file-appender.xml) and want to include in the logback-spring.xml file like below. I place all the three xmls in src/main/resources folder in my custom starter project.

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

    <springProperty scope="context" name="JSONLOGSENABLED" source="spring.logs.json.enabled" defaultValue="true" />
    <springProperty scope="context" name="JSONLOGSAPPENDER" source="spring.logs.json.appender" defaultValue="console" />

    <if condition='property("JSONLOGSENABLED").equalsIgnoreCase("true")'>
        <then>

        <include file="{path to file}/console-appender.xml"/>
        <include file="{path to file}/file-appender.xml"/>
        <root level="INFO">
            <if condition='property("JSONLOGSAPPENDER").equalsIgnoreCase("file")'>
                <then>
                    <appender-ref ref="FILEJSON" />
                </then> 
            <else>
                <appender-ref ref="CONSOLEJSON" />
           </else>
           </if>
        </root>
    </then>
    </if>

    <logger
        name="org.springframework.web.filter.CommonsRequestLoggingFilter">
        <level value="DEBUG" />
    </logger>

</configuration>

file-appender.xml

<appender name="FILE" class="ch.qos.logback.core.FileAppender">
    <file>testFile.log</file>
    <append>true</append>
    <!-- set immediateFlush to false for much higher logging throughput -->
    <immediateFlush>true</immediateFlush>
    <!-- encoders are assigned the type
         ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
    <encoder>
      <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
    </encoder>
  </appender>

But when use this starter in my spring boot application, it is not able to resolve the console and file appender files.

I have tried putting following paths :

 <include file="/src/main/resources/console-appender.xml"/>
 <include file="./console-appender.xml"/>
 <include resource="console-appender.xml"/>

How to include files properly in this case ?

sagar kancherla
  • 129
  • 1
  • 3
  • 12

1 Answers1

11

Yes it's possible. Logback uses Joran so you have to follow the Joran rules.
For more info see documentation.

Anyway this is the preferred way to do what you're looking for.
For instance I define two files, then you adapt it as you prefer.

logback-spring.xml

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

<configuration>
    <include resource="console-appender.xml"/>

    <root level="debug">
        <appender-ref ref="CONSOLE"/>
    </root>
</configuration>   

console-appender.xml

<included>
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%date{"yyyy-MM-dd HH:mm:ss.SSS", Europe/Rome} [%mdc{id}] [%-11thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>
</included>

Please note the <included> tag is mandatory

Roberto Manfreda
  • 2,345
  • 3
  • 25
  • 39