Eclipse+Maven构建SpringMVC+log4j2

1、首先将前面构建好的SpringMVC项目拷贝一份,具体如何使用Maven构建SpringMVC项目的地址为https://www.cnblogs.com/wylwyl/p/13273278.html

2、修改pom.xml文件,添加log4j2的依赖,加入的内容如下

  <properties>
    <spring.version>5.2.1.RELEASE</spring.version>
    <log4j2.version>2.13.3</log4j2.version>
  </properties>    


<dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-api</artifactId>
      <version>${log4j2.version}</version>
    </dependency>
    
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core</artifactId>
      <version>${log4j2.version}</version>
    </dependency>

3、在src/main/resources目录下添加log4j2.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!-- 配置LoggerConfig,即Appenders的日志级别为WARN -->
<Configuration status="WARN">
    <!-- Appenders支持配置多个Appender,支持向不同的目标输送日志,本例为配置向控制台输出 -->
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{yyyy/MM/dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
        </Console>
    </Appenders>
    
    
    <!-- Loggers支持配置多个Logger,可引用不同的目标Appender,也可根据业务需求定制特定要求的Appender -->
    <Loggers>
        <Root level="info">
            <AppenderRef ref="Console" />
        </Root>
    </Loggers>
    
</Configuration>

4、修改web.xml文件,加入下面的代码,如果项目中使用的是Servlet2.5以上版本,则不需要配置用于log4j2的监听,所以现在一般不需要在单独为log4j2配Listener

  <context-param>
      <param-name>log4jConfiguration</param-name>
      <param-value>classpath:log4j2.xml</param-value>
  </context-param>

5、在HelloSpringController.java使用

import java.util.logging.Logger;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloSpringController {
	
	private static Logger logger = Logger.getLogger(HelloSpringController.class.getName());
	
	String message = "Welcome to Spring MVC!";
	
	@RequestMapping("/hello")
	public ModelAndView showMessage(@RequestParam(value = "name", required = false, defaultValue = "Spring") String name) {
		
		logger.info("this is a log");
		
		ModelAndView mv = new ModelAndView("hellospring");//指定视图
		mv.addObject("message", message);
		mv.addObject("name", name);
		return mv;
	}
}

 

6、结果

Eclipse+Maven构建SpringMVC+log4j2

 

7、可以通过修改log4j2.xml文件中level字段的值,观察一下日志输出的结果

在log4j2中, 共有8个级别,按照从低到高为:ALL < TRACE < DEBUG < INFO < WARN < ERROR < FATAL < OFF。

程序会打印高于或等于所设置级别的日志,设置的日志等级越高,打印出来的日志就越少

 

Eclipse+Maven构建SpringMVC+log4j2

 

另外log4j2如何将日志输出到文件的简单配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
  <properties>
    <property name="error-file">./log4j2learn.txt</property>
    <Property name="console-pattern">%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] [%level] %logger{36} - %msg%n</Property>
  </properties>
  <Appenders>
    <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </Console>
    
    <File name="LogFile" fileName="${error-file}" createOnDemand="true">
      <PatternLayout pattern="${console-pattern}" />
    </File>
    
  </Appenders>
  
  <Loggers>
    <Root level="trace">
      <AppenderRef ref="Console"/>
      <AppenderRef ref="LogFile"/>
    </Root>
  </Loggers>
  
</Configuration>

 

参考:https://blog.csdn.net/weixin_34203426/article/details/93118648

上一篇:log4j2


下一篇:Springboot - log4j2