Skip to content

slfj +logback

xml
  <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.25</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.3</version>
        </dependency>

logback.xml 配置

xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="10 minutes" debug="false" >


    <property name="CONSOLE_LOG_PATTERN" value="[ %X{PtxId} ] %d %thread %level %replace(%caller{1}){'\t|Caller.{1}0|\r\n', ''} %msg%n" />




        <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
            <encoder>
                <pattern>${CONSOLE_LOG_PATTERN}</pattern>
            </encoder>
        </appender>
        <root level="DEBUG">
            <appender-ref ref="STDOUT" />
        </root>


</configuration>

slfj log4j

xml
 <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.25</version>
        </dependency>
   <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.12</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>

log4j.properties

properties
 error > warn > info > debug
log4j.rootLogger = debug,cons,F

log4j.appender.cons=org.apache.log4j.ConsoleAppender

log4j.appender.cons.Target = System.out

##org.apache.log4j.TTCCLayout
log4j.appender.cons.layout=org.apache.log4j.PatternLayout
log4j.appender.cons.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n


log4j.appender.F=org.apache.log4j.FileAppender

log4j.appender.F.File =./server.log
#log4j.appender.F.Threshold=Error
log4j.appender.F.layout=org.apache.log4j.PatternLayout
log4j.appender.F.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n

slfj log4fj2

xml
<dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.25</version>
</dependency>
  <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.8.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.8.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <version>2.8.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-web</artifactId>
            <version>2.8.2</version>
            <scope>runtime</scope>
        </dependency>

   <!--log4j2 异步依赖-->
    <dependency>
        <groupId>com.lmax</groupId>
        <artifactId>disruptor</artifactId>
        <version>3.3.6</version>
    </dependency>

log4j2.xml

xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="off" monitorInterval="120">
    <properties>
        <property name="LOG_HOME">D:/dev/projects/java/projects/demoProjects/log-demo/server.log</property>
    </properties>
    <appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>


        <!--RollingFile 为全局同步 RandomAccessFile 为异步-->
        <RollingRandomAccessFile name="rootAppeder"
                                 fileName="${LOG_HOME}/rattanapi.log"
                                 filePattern="${LOG_HOME}/$${date:yyyy-MM}/rattanapi-root-%d{yyyy-MM-dd}-%i.log">
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %ex%msg%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1"/>
                <SizeBasedTriggeringPolicy size="100 MB"/>
            </Policies>
            <DefaultRolloverStrategy max="30"/>
        </RollingRandomAccessFile>


        <!--错误日志输出-->
        <RollingRandomAccessFile name="errorAppeder"
                                 fileName="${LOG_HOME}/rattanapi-error.log"
                                 filePattern="${LOG_HOME}/$${date:yyyy-MM}/rattanapi-error-%d{yyyy-MM-dd}-%i.log">
            <Filters>
                <ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
            </Filters>
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %ex%msg%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1"/>
                <SizeBasedTriggeringPolicy size="100 MB"/>
            </Policies>
            <DefaultRolloverStrategy max="30"/>
        </RollingRandomAccessFile>


    </appenders>


    <loggers>
        <asyncRoot level="info">
            <!--根据配置文件是否打开 console输出 -->
            <appender-ref ref="Console"/>
<!--            <appender-ref ref="rootAppeder"/>-->
<!--            <appender-ref ref="errorAppeder"/>-->
        </asyncRoot>
    </loggers>
</configuration>