目录

only log4j

# log4j.rootLogger = [ level ] , appenderName, appenderName, ... 
# level 是日志记录的优先级
# appenderName就是指定日志信息输出到哪个地方。您可以同时指定多个输出目的地。
log4j.rootLogger = debug,INFO,ERR


# 日志记录类型(当前是滚动追加)
log4j.appender.INFO = org.apache.log4j.RollingFileAppender
# 文件名
log4j.appender.INFO.File = /tmp/log.log
# 文件多大时滚动
log4j.appender.INFO.MaxFileSize = 100mb
# 每次启动是追加还是覆盖,true 追加,false覆盖
log4j.appender.INFO.Append = true
# 输出级别
log4j.appender.INFO.Threshold = info
# 指定日志输出格式
log4j.appender.INFO.layout=org.apache.log4j.PatternLayout
# 日志输出格式字段
log4j.appender.INFO.layout.ConversionPattern=%d-[HL] %p %t %c - %m%n

log4j.appender.ERR = org.apache.log4j.RollingFileAppender
log4j.appender.ERR.File = /tmp/error.log
log4j.appender.ERR.MaxFileSize = 100mb
log4j.appender.ERR.Append = true
log4j.appender.ERR.Threshold = error
log4j.appender.ERR.layout=org.apache.log4j.PatternLayout
log4j.appender.ERR.layout.ConversionPattern=%d %p %t %c: %m%n


#%m   输出代码中指定的消息
#%p  输出优先级,即DEBUG,INFO,WARN,ERROR,FATAL
#%r  输出自应用启动到输出该log信息耗费的毫秒数
#%c  输出所属的类目,通常就是所在类的全名
#%t  输出产生该日志事件的线程名
#%d  输出日志时间点的日期或时间
#%你  换行

slf4j + logback

mvn

<dependencies>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.12</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.1.2</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>1.1.2</version>
        </dependency>
    </dependencies>

logback.xml

<configuration scan="true" scanPeriod="5 seconds">

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <target>System.out</target>
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>[%d{yyyy-MM-dd HH:mm:ss}] %green([%thread]) %highlight(%-5level) %boldMagenta(%logger{120}) %M %L -
                %cyan(%msg%n)
            </pattern>
        </layout>
    </appender>

    <appender name="ROLLING_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>logs/app.log</file>
        <append>true</append>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %thread %logger{80} - %msg%n</pattern>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <fileNamePattern>logs/app.%i.log</fileNamePattern>
            <minIndex>1</minIndex>
            <maxIndex>3</maxIndex>
        </rollingPolicy>
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <maxFileSize>512MB</maxFileSize>
        </triggeringPolicy>
    </appender>

    <appender name="ASYNC_ROLLING_FILE" class="ch.qos.logback.classic.AsyncAppender">
        <discardingThreshold>0</discardingThreshold>
        <queueSize>512</queueSize>
        <appender-ref ref="ROLLING_FILE"/>
    </appender>

    <!--对包指定级别(常用于本应用)-->
    <logger name="cn.tongdun.gaea" level="INFO" additivity="false">
        <appender-ref ref="ASYNC_ROLLING_FILE"/>
    </logger>

    <!--对某个类单独指定级别(常用于debug某个类)-->
    <logger name="cn.tongdun.gaea.canary.boot.persist.MessageHandleService" level="DEBUG" additivity="false">
        <appender-ref ref="ASYNC_ROLLING_FILE"/>
    </logger>

    <!--默认级别为WARN,输出到ASYNC(常用于第三方库的日志)-->
    <root level="WARN">
        <appender-ref ref="ASYNC_ROLLING_FILE"/>
    </root>

</configuration>

main

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

/**
 * Created by caj on 16/3/7.
 */
public class App {
    private static final Logger logger = LoggerFactory.getLogger(App.class.getName());
    public static void main(String[] args) {

        logger.info("hello word");
    }
}

slf4j + log4j

mvn

<dependencies>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.12</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.5</version>
        </dependency>
</dependencies>

log4j.properties

log4j.rootLogger=info, ERR
log4j.appender.ERR = org.apache.log4j.RollingFileAppender
log4j.appender.ERR.File = /Users/caj/workdir/JavaProject/cc/error.log
log4j.appender.ERR.MaxFileSize = 100mb
log4j.appender.ERR.Append = true
log4j.appender.ERR.Threshold = info
log4j.appender.ERR.layout=org.apache.log4j.PatternLayout
log4j.appender.ERR.layout.ConversionPattern=%d %p %t %c: %m%n

main

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

/**
 * Created by caj on 16/3/7.
 */
public class App {
    private static final Logger logger = LoggerFactory.getLogger(App.class.getName());
    public static void main(String[] args) {

        logger.info("hello word");
    }
}