spring4 + log4j2+ slf4j的配置
阿新 • • 發佈:2019-02-20
log4j2的一些配置介紹
log4j2 相比log4j 解決了一些 死鎖問題 改進了效能
下來 我直接掛配置留作本人日後參考
1.maven配置
<properties>
<!-- spring版本號 -->
<spring.version>4.3.2.RELEASE</spring.version>
<!-- log4j日誌檔案管理包版本 -->
<slf4j.version>1.7.21</slf4j.version>
<log4j.version>2.7</log4j.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId >javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<!-- spring核心包 -->
<!-- springframe start -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId >spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-web</artifactId>
<version>${log4j.version}</version>
<scope>runtime</scope>
</dependency>
</dependencies>
2.log4j2.xml配置
<?xml version="1.0" encoding="UTF-8"?>
#這一行我設定為OFF 表示不記錄log4j2本身的日誌
<configuration status="OFF">
<properties>
<property name="LOG_HOME">${web:rootDir}/logs</property>
<property name="FILE_NAME">likR</property>
</properties>
<appenders>
<Console name="Console" target="SYSTEM_OUT">
#這ThresholdFilter是個類似攔截器 level 填寫日誌顯示型別
<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
</Console>
<File name="Error" fileName="${LOG_HOME}/error.log">
<ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="%d{yyyy.MM.dd 'at' HH:mm:ss z} %-5level %class{36} %L %M - %msg%xEx%n"/>
</File>
<RollingFile name="RollingFile" fileName="${LOG_HOME}/${FILE_NAME}.log"
filePattern="${LOG_HOME}/$${date:yyyy-MM}/${FILE_NAME}-%d{MM-dd-yyyy}-%i.log.gz">
<PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss z} %-5level %class{36} %L %M - %msg%xEx%n"/>
#日誌檔案最大值 第二天壓縮
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="10 MB"/>
</Policies>
</RollingFile>
</appenders>
#root 預設載入
<loggers>
<root level="info">
<appender-ref ref="RollingFile"/>
<appender-ref ref="Console"/>
<appender-ref ref="Error" />
</root>
</loggers>
</configuration>
3.web.xml配置
注意!! 是log4jConfiguration!!!
<context-param>
<!-- 日誌配置檔案路徑 -->
<param-name>log4jConfiguration</param-name>
<param-value>classpath:conf/log4j2.xml</param-value>
</context-param>
4.程式碼中使用
private Logger logger = LoggerFactory.getLogger(getClass());
logger .error(“{}”,Object);
文章太簡單?沒辦法 ,您想了解更多 請谷歌!