如何更改maven依赖关系中的日志级别

时间:2018-01-18 07:41:01

标签: java spring maven logback apache-commons

我有一个spring项目,其中logback用于使用sl4j进行日志记录。但是我有一个使用apache-commons而不是sl4j的maven依赖。在这个依赖项目中,我需要显示一个DEBUG级别日志。我已将项目中logback.xml的日志级别更改为DEBUG,但是没有打印该调试日志。

如果您需要更多信息,这是我的pom.xml: 调试级别记录器所在的依赖项目是" org.jasig.cas.client" 。您可以看到" apache commons日志记录被排除在pom中。还附上我的logback.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.betterknow.cas</groupId>
    <artifactId>myProject</artifactId>
    <version>0.7.5</version>
    <packaging>jar</packaging>


    <repositories>
        <repository>
            <id>spring-milestone</id>
            <name>Spring Portfolio Milestone Repository</name>
            <url>http://repo.springsource.org/milestone/</url>
        </repository>
    </repositories>

    <properties>
        <spring.version>3.2.4.RELEASE</spring.version>
        <spring.security.version>3.1.4.RELEASE</spring.security.version>
        <slf4j.version>1.7.5</slf4j.version>
    </properties>


    <dependencies>

        <dependency>
            <groupId>com.myTeam</groupId>
            <artifactId>spring-mock</artifactId>
            <version>1.0.0-SNAPSHOT</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.myTeam</groupId>
            <artifactId>va-registrator-client</artifactId>
            <version>1.0.6</version>
        </dependency>
        <dependency>
            <groupId>com.betterknow</groupId>
            <artifactId>spring-yaml-properties-persister</artifactId>
            <version>1.0.0</version>
        </dependency>

        <dependency>
            <groupId>com.betterknow</groupId>
            <artifactId>connector-core</artifactId>
            <version>0.3.10</version>
        </dependency>

        <dependency>
            <groupId>com.betterknow</groupId>
            <artifactId>connector-security</artifactId>
            <version>1.0.7</version>
        </dependency>

        <dependency>
            <groupId>com.betterknow</groupId>
            <artifactId>properties-configuration</artifactId>
            <version>1.0.3</version>
        </dependency>

        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>javax.mail-api</artifactId>
            <version>1.5.2</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.6.0</version>
        </dependency>

        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.13</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.6.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.6.0</version>
        </dependency>

        <dependency>
            <groupId>org.jdom</groupId>
            <artifactId>jdom2</artifactId>
            <version>2.0.6</version>
        </dependency>

        <dependency>
            <groupId>org.yaml</groupId>
            <artifactId>snakeyaml</artifactId>
            <version>1.13</version>
        </dependency>

        <dependency>
            <groupId>org.apache.shale</groupId>
            <artifactId>shale-test</artifactId>
            <version>1.0.5</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test-mvc</artifactId>
            <version>1.0.0.M2</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <artifactId>logging-service-client</artifactId>
            <groupId>com.betterknow</groupId>
            <exclusions>
                <exclusion>
                    <artifactId>joda-time</artifactId>
                    <groupId>joda-time</groupId>
                </exclusion>
                <exclusion>
                    <groupId>org.restlet.jee</groupId>
                    <artifactId>org.restlet.ext.httpclient</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.restlet.jee</groupId>
                    <artifactId>org.restlet.ext.slf4j</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>commons-io</groupId>
                    <artifactId>commons-io</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-api</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.apache.httpcomponents</groupId>
                    <artifactId>httpclient</artifactId>
                </exclusion>
            </exclusions>
            <version>1.0.11</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>3.2.5.RELEASE</version>
            <exclusions>
                <exclusion>
                    <artifactId>commons-logging</artifactId>
                    <groupId>commons-logging</groupId>
                </exclusion>
            </exclusions>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.jasig.cas.client</groupId>
            <artifactId>cas-client-core</artifactId>
            <version>3.1.12</version>
            <exclusions>
                <exclusion>
                    <artifactId>commons-logging</artifactId>
                    <groupId>commons-logging</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-cas</artifactId>
            <version>${spring.security.version}</version>
            <exclusions>
                <exclusion>
                    <artifactId>spring-jdbc</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>spring-tx</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>commons-logging</artifactId>
                    <groupId>commons-logging</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>${spring.security.version}</version>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.0.13</version>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>${slf4j.version}</version>
        </dependency>

        <dependency>
            <groupId>commons-httpclient</groupId>
            <artifactId>commons-httpclient</artifactId>
            <version>3.1</version>
            <exclusions>
                <exclusion>
                    <artifactId>commons-logging</artifactId>
                    <groupId>commons-logging</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.3.1</version>
            <exclusions>
                <exclusion>
                    <artifactId>commons-logging</artifactId>
                    <groupId>commons-logging</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.0</version>
        </dependency>

        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>1.9.5</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
        </dependency>


        <dependency>
            <groupId>org.jdom</groupId>
            <artifactId>jdom2</artifactId>
            <version>2.0.6</version>
        </dependency>

    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>commons-codec</groupId>
                <artifactId>commons-codec</artifactId>
                <version>1.8</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aop</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-beans</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-expression</artifactId>
                <version>${spring.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-enforcer-plugin</artifactId>
                <version>1.1.1</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>cobertura-maven-plugin</artifactId>
                <version>2.7</version>
                <configuration>
                    <instrumentation>
                        <includes>
                            <include>com/betterknow/cas/controller/*.class</include>
                            <include>com/betterknow/cas/service/CASUserDetailsService.class</include>
                        </includes>
                        <!-- <ignores>
                            <ignore>com.myTeam.esp.dao.entity.*</ignore>
                        </ignores> -->
                        <excludes>
                            <exclude>com/betterknow/cas/common/*.class</exclude>
                        </excludes>
                    </instrumentation>
                    <encoding>UTF-8</encoding>
                </configuration>
                <executions>
                    <execution>
                        <id>clean</id>
                        <phase>pre-site</phase>
                        <goals>
                            <goal>clean</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>instrument</id>
                        <phase>site</phase>
                        <goals>
                            <goal>instrument</goal>
                            <goal>cobertura</goal>
                        </goals>
                        <configuration>
                            <encoding>UTF-8</encoding>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <version>1.4</version>
                    <configuration>
                        <transformers>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>com.betterknow.cas.service.CASConnectorMain</mainClass>
                                <manifestEntries>
                                    <Version>${project.version}</Version>
                                </manifestEntries>
                            </transformer>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>META-INF/spring.handlers</resource>
                            </transformer>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>META-INF/spring.schemas</resource>
                            </transformer>
                        </transformers>
                        <!-- This filter is needed to disable Jetty's security manager -->
                        <filters>
                            <filter>
                                <artifact>*:*</artifact>
                                <excludes>
                                    <exclude>META-INF/*.SF</exclude>
                                    <exclude>META-INF/*.DSA</exclude>
                                    <exclude>META-INF/*.RSA</exclude>
                                </excludes>
                            </filter>
                        </filters>
                        <createDependencyReducedPom>false</createDependencyReducedPom>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

Logback.xml:

<configuration scan="true" scanPeriod="30 seconds">
    <property name="pattern" value="%date{ISO8601} [%thread] %-5level %logger{32} - %msg%n" />
            <property resource="logback-connector.properties" />
            <include optional="true" resource="logback-connector.xml" />

               <appender name="ADDITIONAL"
                      class="ch.qos.logback.core.rolling.RollingFileAppender">
                 <file>/var/log/standalone_servers/cas-connector-qa/cas-connector-qa_ADDITIONAL.log</file>
                 <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
                    <level>WARN</level>
                 </filter>
                 <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">

                    <fileNamePattern>/var/log/standalone_servers/cas-connector-qa/cas-connector-qa_ADDITIONAL.%d{yyyy_MM_dd}.%i.zip
                    </fileNamePattern>
                    <maxHistory>30</maxHistory>
                    <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                        <maxFileSize>500MB</maxFileSize>
                    </timeBasedFileNamingAndTriggeringPolicy>
                    <totalSizeCap>3GB</totalSizeCap>
                    <cleanHistoryOnStart>true</cleanHistoryOnStart>
                 </rollingPolicy>
                 <encoder>
                    <pattern>${pattern}%ex{4}</pattern>
                 </encoder>
               </appender>


              <appender name="MAIN"
                      class="ch.qos.logback.core.rolling.RollingFileAppender">
                      <file>/var/log/standalone_servers/cas-connector-qa/cas-connector-qa_MAIN.log</file> 
                 <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
                    <fileNamePattern>/var/log/standalone_servers/cas-connector-qa/cas-connector-qa_MAIN.%d{yyyy_MM_dd}.%i.zip
                    </fileNamePattern>
                    <maxHistory>30</maxHistory>
                    <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                        <maxFileSize>500MB</maxFileSize>
                    </timeBasedFileNamingAndTriggeringPolicy>
                    <totalSizeCap>3GB</totalSizeCap>
                 </rollingPolicy>
                 <encoder>
                    <pattern>${pattern}%ex{4}</pattern>
                </encoder>
             </appender>

            <appender name="CYCLIC" class="ch.qos.logback.core.read.CyclicBufferAppender">
                <MaxSize>512</MaxSize>
            </appender>

           <root level="DEBUG">
                <appender-ref ref="MAIN" />
                <appender-ref ref="ADDITIONAL" />
                <appender-ref ref="CYCLIC" />
            </root>

</configuration>

1 个答案:

答案 0 :(得分:1)

这将阻止使用commons-logging进行日志记录:

   <dependency>
        <groupId>org.jasig.cas.client</groupId>
        <artifactId>cas-client-core</artifactId>
        <version>3.1.12</version>
        <exclusions>
            <exclusion>
                <artifactId>commons-logging</artifactId>
                <groupId>commons-logging</groupId>
            </exclusion>
        </exclusions>
    </dependency>

您想要的是使用SLF4-Logback进行日志记录的方法,还可以使用JCL记录使用JCL的情况以及无法更改的情况(此处为cas-client-core依赖项)。

SLF4J提供a way to doslf4j-jcl(重点是我的):

  

我们的一些用户在切换到SLF4J API后意识到了这一点   在某些情况下,JCL的使用是强制性的,并且它们可以使用SLF4J   是个问题。对于这个不常见但重要的案例,SLF4J提供了一个JCL   绑定,在文件slf4j-jcl.jar中找到。 JCL绑定将   将通过SLF4J API进行的所有日志记录调用委托给JCL。因此,如果为   某些原因,现有的应用程序必须使用JCL,这是你的一部分   应用程序仍然可以以某种方式对SLF4J API进行编码   对更大的应用程序环境透明。你的选择   SLF4J API对于应用程序的其余部分是不可见的   继续使用JCL。