Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring + Logback(slf4j) logging without jcl-over-slf4j

If i add Logback and Slf4j to my Spring MVC (version 4.1.0) project by adding maven dependencies like this:

            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>jcl-over-slf4j</artifactId>
                <version>1.7.21</version>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>1.7.21</version>
                <scope>compile</scope>
            </dependency>
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
                <version>1.1.7</version>
                <scope>runtime</scope>
            </dependency>

and exclude commons-logging dependency, then i get in log file, in addition to application logs, some spring events INFO level like:

[RMI TCP Connection(4)-127.0.0.1][org.springframework.web.context.ContextLoader] - Root WebApplicationContext: initialization started

If i add dependencies without jcl-over-slf4j, and without excluding commons-logging in pom.xml, then i get only application logs.

This is my logback.xml:

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

    <property name="LOG_HOME" value="logs" />
    <property name="APP_NAME" value="App" />

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">        
        <encoder>
            <charset>UTF-8</charset>
            <pattern>%-5level %d{yyyy-MM-dd HH:mm:ss.SSS} %thread %logger{50} - %msg%n</pattern>            
        </encoder>
    </appender>

    <appender name="FILE"  class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <FileNamePattern>${LOG_HOME}/${APP_NAME}.log.%d{yyyy-MM-dd}.log</FileNamePattern>
            <MaxHistory>30</MaxHistory>
        </rollingPolicy>

        <encoder>
            <charset>UTF-8</charset>
            <pattern>%-5level %d{yyyy-MM-dd HH:mm:ss.SSS} %thread %logger{50} - %msg%n</pattern>                
        </encoder>

        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>10MB</MaxFileSize>
        </triggeringPolicy>
    </appender>  

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

    <logger name="org.springframework" level="INFO" appender-ref="FILE"/>
</configuration>

As i understand it, this happens because when i don't use the JCL-bridge, logback can't catch, or translate JCL spring inner logs. Please, correct me, if i am wrong.

Can i configure logback.xml for getting standart spring log events without JCL-bridge?

like image 859
RocketBoom Avatar asked Oct 19 '22 05:10

RocketBoom


1 Answers

Check theirs docs, because it is a good source for information with a lot of examples. Back to your question: Spring and JCL:

The mandatory logging dependency in Spring is the Jakarta Commons Logging API (JCL). We compile against JCL and we also make JCL Log objects visible for classes that extend the Spring Framework. It’s important to users that all versions of Spring use the same logging library: migration is easy because backwards compatibility is preserved even with applications that extend Spring.The way we do this is to make one of the modules in Spring depend explicitly on commons-logging (the canonical implementation of JCL), and then make all the other modules depend on that at compile time. If you are using Maven for example, and wondering where you picked up the dependency on commons-logging, then it is from Spring and specifically from the central module calledspring-core.

Logback configuration, here is good point:

A more common choice amongst SLF4J users, which uses fewer step sand generates fewer dependencies, is to bind directly to Logback. This removes the extra binding step because Logback implements SLF4J directly, so you only need to depend on two libaries not four (jcl-over-slf4j and logback). If you do that you might also need to exclude the slf4j-api dependency from other external dependencies (not Spring), because you only want one version of that API on the classpath.

like image 59
Hrabosch Avatar answered Nov 03 '22 00:11

Hrabosch