log4j2 修改为 logback

2017-09-30  本文已影响0人  mgw1986

logback官网

步骤1:

pom.xml文件修改

  <!-- log4j2+ slf4j -->
 <dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>${slf4j.api.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>${log4j2.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>${log4j2.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-slf4j-impl</artifactId>
    <version>${log4j2.version}</version>
</dependency>
 修改为:
<!-- logback + slf4j -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.21</version>
</dependency>
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>${logback.version}</version>
</dependency>

步骤2:

删除log4j2.xml文件,新建logback.xml 文件,复制下面的内容

<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false">
    <!-- 日志的存放路径,绝对路径 -->
    <property name="logPath" value="${logpath}/logs" />
    <property name="fileName" value="oms" />
    <!-- true 打印日志到控制台,false 打印日志到文件 -->
    <property name="debug" value="true" />
    <timestamp key="byDay" datePattern="yyyyMMdd" />
    <!-- 打印日志到控制台 -->
    <appender name="constole" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss} %-5p %c{36}:%L - %m%n</pattern>
        </encoder>
    </appender>
    <!-- 打印日志到按日期滚动的文件 -->
    <appender name="rollFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${logPath}/${fileName}.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${logPath}/backup/${fileName}_%d{yyyyMMdd}.log</fileNamePattern>
            <!-- 默认保存15天的日志 -->
            <maxHistory>1</maxHistory>
            <!-- 总的存档日志最大3GB -->
            <totalSizeCap>3GB</totalSizeCap>
        </rollingPolicy>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss} %-5p %c{36}:%L - %m%n</pattern>
        </encoder>
    </appender>
    <!-- Hibernate 定制 -->  
    <logger name="org.hibernate.SQL" level="debug" />
    <logger name="org.hibernate" level="error" />
    <!-- druid 控制 -->
    <logger name="com.alibaba.druid" level="error" />
    <root level="info">
        <if condition='property("debug").contains("false")'>
            <then>
                <appender-ref ref="rollFile" />
            </then>
            <else>
                <appender-ref ref="constole" />
            </else>
        </if>
    </root>
</configuration>
上一篇下一篇

猜你喜欢

热点阅读