跟杨春娟学SpringMVC笔记:日志log4j2及slf4j的使用

跟杨春娟学SpringMVC笔记:日志log4j2及slf4j的使用

完成:第一遍

1.如何使用日志log4j2及slf4j?

步骤一:配置maven project
BuildPath添加tomcat
修改JDK和web版本,maven update
添加spring-webmvc依赖

步骤二:Dependencies里add添加org.slf4j slf4j-api的依赖
添加org.apache.logging.log4j log4j-core依赖
和org.apache.logging.log4j log4j-api依赖
和org.apache.logging.log4j log4j-slf4j-impl依赖
和org.apache.logging.log4j log4j-web依赖

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.adbycool</groupId>
  <artifactId>SpringMVC_LogProject</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>SpringMVC_LogProject Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-webmvc</artifactId>
    	<version>5.2.8.RELEASE</version>
    </dependency>
    <dependency>
    	<groupId>org.slf4j</groupId>
    	<artifactId>slf4j-api</artifactId>
    	<version>2.0.0-alpha1</version>
    </dependency>
    <dependency>
    	<groupId>org.apache.logging.log4j</groupId>
    	<artifactId>log4j-core</artifactId>
    	<version>2.13.3</version>
    </dependency>
    <dependency>
    	<groupId>org.apache.logging.log4j</groupId>
    	<artifactId>log4j-api</artifactId>
    	<version>2.13.3</version>
    </dependency>
    <dependency>
    	<groupId>org.apache.logging.log4j</groupId>
    	<artifactId>log4j-slf4j-impl</artifactId>
    	<version>2.13.3</version>
    </dependency>
    <dependency>
    	<groupId>org.apache.logging.log4j</groupId>
    	<artifactId>log4j-web</artifactId>
    	<version>2.13.3</version>
    	<scope>runtime</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>SpringMVC_LogProject</finalName>
     <plugins>
       <plugin>
             <groupId>org.apache.maven.plugins</groupId>
             <artifactId>maven-compiler-plugin</artifactId>
             <version>3.1</version>
             <configuration>
                 <source>14</source>     
                 <target>14</target>      
             </configuration>
       </plugin>
  </plugins>
  </build>
</project>

步骤三:编写Controller
新建private static final Logger logger = LoggerFactory.getLogger(UserController.class);
使用:logger.info(“method start! Controller show()”);

package com.adbycool.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;



@Controller
@RequestMapping("user")
public class UserController {

	private static final Logger logger = LoggerFactory.getLogger(UserController.class);

	public String show() {
		
		logger.info("method start! Controller show()");
		return "show";
	}
	
}

步骤四:配置src/main/resources下的log4j2.xml属性文件

配置log4j2.xml

<?xml version="1.0" encoding="UTF-8" ?>
<Configuration status="ERROR" monitorInterval="30">
	<Properties>
		<Property name="LOG_HOME">C:\java\log</Property>
	</Properties>
	<Appenders>
		<Console name="Console" target="SYSTEM_OUT">
			<ThresholdFilter level="TRACE" onMatch="ACCEPT" onMismatch="DENY" />
			<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger{36}:%L [%t] - %msg%n" />
		</Console>
		<RollingFile name="RollingFileInfo" fileName="${LOG_HOME}/info.log" 
		filePattern="${LOG_HOME}/$${date:yyyy-MM}/info-%d{yyyy-MM-dd}-%i.log">
			<ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
			<PatternLayout pattern="%d{yyyy-MM-dd HH:ss:mm.SSS} %-5level %logger{36}:%L [%t] - %msg%n" />
			<Policies>
				<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
				<sizeBasedTriggeringPolicy size="100 MB"/>
			</Policies>
			<DefaultRolloverStrategy max="100"/>
		</RollingFile>
		<RollingFile name="RollingFileError" fileName="${LOG_HOME}/error.log" filePattern="${LOG_HOME}/$${date:yyyy-MM}/error-%d{yyyy-MM-dd}.log">
			<ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
			<PatternLayout pattern="%d{yyyy-MM-dd HH:ss:mm.SSS} %-5level %logger{36}:%L [%t] - %msg%n" />
			<Policies>
				<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
				<sizeBasedTriggeringPolicy size="100 MB"/>
			</Policies>
			<DefaultRolloverStrategy max="100"/>
		</RollingFile>
	</Appenders>
	
	<Loggers>
		<logger name="org.springframework" level="INFO"/>
		<root level="ALL">
			<appender-ref ref="Console"/>
			<appender-ref ref="RollingFileInfo"/>
			<appender-ref ref="RollingFileError"/>
		</root>
	</Loggers>
</Configuration>
©️2020 CSDN 皮肤主题: 创作都市 设计师:CSDN官方博客 返回首页