常用日志框架配置总结

常用日志框架配置总结

Scroll Down

案例中全部使用的slf4j日志门面

log4j

导入jar包

<!--jcl使用slf4j-->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>jcl-over-slf4j</artifactId>
    <version>1.7.30</version>
</dependency>
<!--jul使用slf4j-->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>jul-to-slf4j</artifactId>
    <version>1.7.30</version>
</dependency>
<!--使用log4j+slf4j-->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.30</version>
</dependency>

log4j.properties

log4j.rootLogger=info,console
log4j.logger.zone.lxy=debug,rollingFile

# 控制台输出
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p [%t] %c - %m%n

# 日志文件输出
log4j.appender.rollingFile = org.apache.log4j.DailyRollingFileAppender
# 最小每分钟产生一个文件
log4j.appender.rollingFile.DatePattern='.'yyyy-MM-dd-HH-mm
log4j.appender.rollingFile.File = D:/log/log4j.log
log4j.appender.rollingFile.Append = true
log4j.appender.rollingFile.layout = org.apache.log4j.PatternLayout
log4j.appender.rollingFile.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss} %-5p [%t] %c - %m%n
log4j.appender.rollingFile.encoding=UTF-8


log4j2

导入jar包

<!--jcl使用slf4j-->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>jcl-over-slf4j</artifactId>
    <version>1.7.30</version>
</dependency>
<!--jul使用slf4j-->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>jul-to-slf4j</artifactId>
    <version>1.7.30</version>
</dependency>
<!--log4j使用slf4j-->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>log4j-over-slf4j</artifactId>
    <version>1.7.30</version>
</dependency>
<!--使用log4j2+slf4j-->
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-slf4j-impl</artifactId>
    <version>2.14.0</version>
</dependency>

log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" monitorInterval="5">
    <Appenders>
        <!--控制台输出-->
        <Console name="console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %highlight{%-5level} [%thread] %cyan{%c} - %msg%n"
                           disableAnsi="false" noConsoleNoAnsi="false"/>
        </Console>
        <!--拆分日志文件-->
        <!--每秒生成一个日志文件-->
        <RollingFile name="rollingFile" fileName="D:/log/log4j2.log"
                     filePattern="D:/log/log4j2-%d{yyyy-MM-dd-HH-mm-ss}.log">
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-5level [%thread] %c - %msg%n"/>
            <Policies>
                <!--按照时间节点拆分-->
                <TimeBasedTriggeringPolicy/>
            </Policies>
            <!--同一个目录下限定文件个个数-->
            <DefaultRolloverStrategy max="7"/>
        </RollingFile>
    </Appenders>
    <Loggers>
        <Root level="info">
            <AppenderRef ref="console"/>
        </Root>
        <Logger name="zone.lxy" level="debug" additivity="false">
            <AppenderRef ref="rollingFile"/>
            <AppenderRef ref="console"/>
        </Logger>
    </Loggers>
</Configuration>

logback

导入jar包

<!--jcl使用slf4j-->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>jcl-over-slf4j</artifactId>
    <version>1.7.30</version>
</dependency>
<!--jul使用slf4j-->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>jul-to-slf4j</artifactId>
    <version>1.7.30</version>
</dependency>
<!--log4j使用slf4j-->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>log4j-over-slf4j</artifactId>
    <version>1.7.30</version>
</dependency>
<!--使用logback+slf4j-->
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.2.3</version>
</dependency>

logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>%d{yyyy-MM-dd HH:mm:ss} %highlight(%-5level) [%thread] %cyan(%-30c{30}) - %msg%n</pattern>
        </encoder>
    </appender>

    <appender name="rollFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>D:/log/logback.log</file>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level [%thread] %-30c{30} - %msg%n</pattern>
        </encoder>
        <!--每秒生成一个日志文件-->
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>D:/log/logback.%d{yyyy-MM-dd_HH-mm-ss}.log</fileNamePattern>
            <!--保存最近7天的日志-->
            <maxHistory>7</maxHistory>
        </rollingPolicy>
    </appender>

    <root level="info">
        <appender-ref ref="console"/>
    </root>
    <logger name="zone.lxy" level="debug" additivity="false">
        <appender-ref ref="console"/>
        <appender-ref ref="rollFile"/>
    </logger>

</configuration>