Understanding logback.xml file

Rohit Bajaj
Rohit Bajaj used Ask the Experts™
I am using the following file in my java maven project :
<?xml version="1.0" encoding="UTF-8"?>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
                %d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n
        </encoder>    </appender>
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- daily rollover -->

            <!-- keep 30 days' worth of history capped at 3GB total size -->


            <pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n

    <logger name="chatAdapter" level="debug" additivity="false">
        <appender-ref ref="STDOUT" />
    <logger name="SMRunner" level="debug" additivity="false">
        <appender-ref ref="STDOUT" />
    <logger name="stateMachine" level="debug" additivity="false">
        <appender-ref ref="STDOUT" />

    <root level="error">
        <appender-ref ref="STDOUT"/>

Open in new window

This file resides in my top directory and inside it i have different modules like chatAdapter, stateMachine etc.

I have two questions :
1) Whats the meaning of defining root tag here ? Is it doing anything
2) As all of chatAdapter, stateMachine etc.. have the same logging conditions... Can i somehow avoid writing all of them and specify a common one for all the modules and yet have only one logback.xml file in the  project ?

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Root tag defines a Root logger. There must be at least on logger in every logging framework, otherwise its useless. Logback mandates to have root logger. Additional loggers, using logger tag, are optional. It can be used to define logger with different log levels (and different appenders, if needed).

In your case you have defined root logger with error level, and logger are with debug level. Appender (STDOUT) is common for all. So you can keep root logger and only one logger in logback.

In future based on requirement, you can add more loggers with different level or different appender for different modules.

You can refer the manual at http://logback.qos.ch/manual/configuration.html

Hope it helps.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial