文章标签 ‘log4j’
20173 月17

log4j 换成 logback 注意事项

pom.xml

 

<dependency>  
    <groupId>ch.qos.logback</groupId>  
    <artifactId>logback-classic</artifactId>  
    <version>1.2.2</version>  
</dependency>  
<dependency>  
    <groupId>org.logback-extensions</groupId>  
    <artifactId>logback-ext-spring</artifactId>  
    <version>0.1.4</version>  
</dependency>  
<dependency>  
    <groupId>org.slf4j</groupId>  
    <artifactId>jcl-over-slf4j</artifactId>  
    <version>1.7.25</version>  
</dependency>
<dependency>  
    <groupId>org.slf4j</groupId>  
    <artifactId>log4j-over-slf4j</artifactId>  
    <version>1.7.25</version>  
</dependency>

 

排除其他包的log4j引用,特别是dubbo的、shiro的

 

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.8.4</version>
            <exclusions>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>javax.ws.rs-api</artifactId>
            <version>2.0</version>
            <exclusions>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-ehcache</artifactId>
            <version>1.3.2</version>
            <exclusions>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-web</artifactId>
            <version>1.3.2</version>
            <exclusions>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

配置文件:

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

<!-- For assistance related to logback-translator or configuration  -->
<!-- files in general, please contact the logback user mailing list -->
<!-- at http://www.qos.ch/mailman/listinfo/logback-user             -->
<!--                                                                -->
<!-- For professional support please see                            -->
<!--    http://www.qos.ch/shop/products/professionalSupport         -->
<!--                                                                -->
<configuration>
    <appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
        <file>/monitor.log</file>

        <!-- Policy定义如何滚动,按文件大小滚动生成日志 -->
        <!-- 如果是按文件大小滚动生成日志,前面的file标签可省略,而使用fileNamePattern标签定义的名字 -->
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <fileNamePattern>/monitor.%i.log.gz
            </fileNamePattern>
            <!-- 归档日志的下标,替换fileNamePattern的%i,最多3个归档文件 -->
            <minIndex>1</minIndex>
            <maxIndex>5</maxIndex>
        </rollingPolicy>

        <!-- triggeringPolicy定义什么时候滚动,下面是定义了文件大小超过100M的时候产生归档文件 -->
        <triggeringPolicy
                class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <maxFileSize>300MB</maxFileSize>
        </triggeringPolicy>

        <!-- append是否接着上次写文件结尾继续写,默认为true -->
        <append>true</append>
        <encoding>GBK</encoding>

        <!-- layout,定义格式 -->
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>[%-5level] %d{HH:mm:ss.SSS} [%thread] %logger %caller{2} - %msg%n</pattern>
        </layout>
    </appender>
    <logger name="org.springframework" level="info"/>
    <logger name="org.nutz.dao" level="debug"/>
    <logger name="net.sf.ehcache" level="info"/>
    <logger name="druid.sql" level="info"/>
    <logger name="com.alibaba.druid" level="info"/>
    <logger name="org.apache.shiro" level="info"/>
    <logger name="org.quartz" level="info"/>
    <root level="debug">
        <appender-ref ref="Console"/>
    </root>
</configuration>