Understanding logback.xml file

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

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

        </rollingPolicy>

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

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

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

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 ?

Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
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