Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

logback externalization

Tags:

slf4j

logback

Currently I have logback configuration file i.e logback.xml which is src/main/resources. I want to set the logging level but i want control outside of war.

One solution which i could think of is to externalize it in properties file, load it on server startup and substitute the placeholder. Is there any better way of doing it ? Can't we keep logback.xml outside of war file ?

    <configuration>
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${catalina.base}/logs/logFile.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- daily rollover -->
            <fileNamePattern>logFile.%d{yyyy-MM-dd}.log</fileNamePattern>
            <!-- keep 7 days' worth of history -->
            <maxHistory>7</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>%date [%thread] %-5level %logger{35} - %msg%n</pattern>
        </encoder>
    </appender>
    
    <root level="DEBUG">
        <appender-ref ref="FILE"/>
    </root>
</configuration>
like image 243
Pankaj Avatar asked Jan 12 '13 00:01

Pankaj


People also ask

Is Logback deprecated?

This method is deprecated and exists solely for backward compatibility reasons. Logback components should use getMarkerList() and cater for all available markers and not only the first marker. This fixes LOGBACK-1569 reported by Pelle Krøgholt .

What is root level in Logback?

"Root" level does not restrict levels of other loggers, it merely sets the default. So <root level="INFO"> and <logger name="some.name" level="DEBUG"> are perfectly suitable together, and you don't need to relax the "root" level. So both examples should log on debug level for logger named com.

What is Logback additivity?

1.2 Logback AdditivityAppenders are added to the loggers. One logger may include more than one appenders. Thus, its log messages are written more than one desired destination systems. Additivity is exactly about this point. The output of a log statement of logger A will go to all the appenders in A and its ancestors.


2 Answers

If you just want to change the log level you don't need to override the whole file, you can do this:

<root level="${log.level:-INFO}">

Then if you set the system property:

-Dlog.level=DEBUG

It will override the default of 'INFO'.

like image 148
David Roussel Avatar answered Oct 27 '22 01:10

David Roussel


I suggest you use 2 logback configuration files. The first one in the classpath defined as:

<configuration scan="true">
    <property file="/otherfolder/project.properties" />
    <include file="/otherfolder/logback.xml"/>
</configuration>

and you put all the configuration in the other one. Logback will scan both files for updates.

like image 36
Assertnotnull Avatar answered Oct 27 '22 00:10

Assertnotnull