logback 实例

POM : 

        <!-- log -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.1</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>1.7.1</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>log4j-over-slf4j</artifactId>
            <version>1.6.6</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jul-to-slf4j</artifactId>
            <version>1.7.1</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>1.1.2</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.1.2</version>
        </dependency>

Class : Log

package com.xindatai.ibs.environ.util.constant;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Log {
    public static final Logger LOGGER = LoggerFactory.getLogger("ibs.log");
    
    public static final Logger DEVICE_LOGGER = LoggerFactory.getLogger("device.log");

}

logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="30 seconds" debug="false">
    <!-- Console -->
    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <!-- <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern> -->
            <pattern>[%-5level %d{yyyy-MM-dd HH:mm:ss} %c{0}] - %m%n</pattern>
        </encoder>
    </appender>

    <appender name="ibs.log" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${catalina.base}/logs/ibs.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${catalina.base}/logs/ibs.%d.gz</fileNamePattern>
        </rollingPolicy>
        <encoder>
            <pattern>[%-5level %d{yyyy-MM-dd HH:mm:ss} %c{0}] - %m%n</pattern>
        </encoder>
    </appender>

    <appender name="device.log" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${catalina.base}/logs/device.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${catalina.base}/logs/device.%d.gz</fileNamePattern>
        </rollingPolicy>
        <encoder>
            <pattern>[%-5level %d{yyyy-MM-dd HH:mm:ss} %c{0}] - %m%n</pattern>
        </encoder>
    </appender>

    <logger name="device" additivity="false">
        <appender-ref ref="device.log" />
    </logger>

    <logger name="ibs.log" additivity="false">
        <appender-ref ref="ibs.log" />
    </logger>

    <root level="INFO">
        <appender-ref ref="console" />
    </root>
</configuration>

Class : 

Log.LOGGER.warn("从picker获取环境检测及控制设备的信息:" + new Date() + "--开始搜集");

Log.DEVICE_LOGGER.warn("执行营业时间空调及净化器策略--Start");

File : 

啦啦啦

啦啦啦

posted @ 2017-06-06 16:57  limeOracle  阅读(1433)  评论(1编辑  收藏  举报