乐九娱乐城二八杠赌钱

Log4j2 & Slf4j 的配置

/ 默認分類 / 0 條評論 / 30瀏覽

pom.xml的配置 版本可以根據自己的需要升級對應的版本號

 <dependencies>
    <!-- log配置:Log4j2 + Slf4j -->
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.2</version>
    </dependency>
    <dependency> <!-- 橋接:告訴Slf4j使用Log4j2 -->
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j-impl</artifactId>
        <version>2.2</version>
    </dependency>
    <dependency> <!-- 橋接:告訴commons logging使用Log4j2 -->
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-jcl</artifactId>
        <version>2.2</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.10</version>
    </dependency>
 
</dependencies>

log4j2.xml 文件的配置 此文件在resources目錄下即可.

    <?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <!-- 輸出到控制臺 -->
        <Console name="Console" target="SYSTEM_OUT">
            <!-- 需要記錄的級別 -->
            <!-- <ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="DENY" /> -->
            <PatternLayout pattern="%d{yyyy-MM-dd HH????ss,SSS}:%4p %t (%F:%L) - %m%n"/>
        </Console>

        <!-- 輸出到文件,按天或者超過80MB分割 -->
        <RollingFile name="RollingFile" fileName="../logs/app.log"
                     filePattern="../logs/$${date:yyyy-MM}/xjj-%d{yyyy-MM-dd}-%i.log.gz">
            <!-- 需要記錄的級別 -->
            <!-- <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY" /> -->
            <PatternLayout pattern="%d{yyyy-MM-dd HH????ss,SSS}:%4p %t (%F:%L) - %m%n"/>
            <Policies>
                <OnStartupTriggeringPolicy/>
                <TimeBasedTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="80 MB"/>
            </Policies>
        </RollingFile>
    </Appenders>

    <Loggers>
        <Root level="debug"> <!-- 全局配置 -->
            <AppenderRef ref="Console"/>
            <AppenderRef ref="RollingFile"/>
        </Root>
    </Loggers>

</Configuration>

使用:

static public Logger logger = LoggerFactory.getLogger(Log4j2Test.class);