spring mvc中如何集成log4j呢?

java-教程王 Java教程 发布时间:2022-01-03 09:13:06 阅读数:19206 1
下文笔者讲述spring mvc中集成log4j的方法分享,如下所示:

步骤一、添加maven依赖

Log4j所需的jar包:commons-logging.jar和log4j.jar

//直接依赖包,最新版本去maven网站找
<dependency>
	<groupId>commons-logging</groupId>
	<artifactId>commons-logging</artifactId>
	<version>1.1</version>
</dependency>
<dependency>
	<groupId>org.apache.logging.log4j</groupId>
	<artifactId>log4j-api</artifactId>
	<version>2.0.2</version>
</dependency>
<dependency>
	<groupId>org.apache.logging.log4j</groupId>
	<artifactId>log4j-core</artifactId>
	<version>2.0.2</version>
</dependency>

//或者依赖slf4j
<properties>
	<slf4j.api.version>1.7.5</slf4j.api.version>
</properties>
<dependencies>
<dependency>
  <groupId>org.slf4j</groupId>  
  <artifactId>slf4j-log4j12</artifactId>  
  <version>${slf4j.api.version}</version>  
</dependency>
</dependencies>

步骤二、log4j配置

log4j日志的配置可以用log4j.xml,log4j.properties这2种形式配置
//log4j.properties配置
### 设置###
log4j.rootLogger = WARN,stdout,D,E
### 输出信息到控制台 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%p][%d{yyyy-MM-dd HH:mm:ss} %l] ?%m%n
 
#log4j.appender.stdout = org.apache.log4j.ConsoleAppender
#log4j.appender.stdout.Target = System.out
#log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
#log4j.appender.stdout.layout.ConversionPattern = [%p][%d{yyyy-MM-dd HH:mm:ss} %l] ?%m%n
#log4j.appender.stdout.layout.ConversionPattern = [%p][%d{yyyy-MM-dd HH:mm:ss} %l] ?%m%n
### 输出DEBUG 级别以上的日志到=D://logs/error.log ###
#log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D = org.apache.log4j.RollingFileAppender
log4j.appender.D.File = log/Wifi_sy/Wifi_sy_warn.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = WARN?
log4j.appender.D.MaxFileSize = 10240KB
log4j.appender.D.MaxBackupIndex = 3?
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss} [%c:%L:[%p]] %m%n
### 输出ERROR 级别以上的日志到=D://logs/error.log ###
log4j.appender.E = org.apache.log4j.RollingFileAppender
log4j.appender.E.File = log/Wifi_sy/Wifi_sy_err.log
log4j.appender.E.Append = true
log4j.appender.E.Threshold = ERROR?
log4j.appender.E.MaxFileSize = 10240KB
log4j.appender.E.MaxBackupIndex = 3?
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss} [%l:[%p]] %m%n

步骤三web.xml配置

<context-param>
	<param-name>log4jConfigLocation</param-name>
	<param-value>classpath:log4j.properties</param-value><!-- log4j配置文件路径 -->
</context-param>
<!--log4jRefreshInterval为100000表示 开一条watchdog线程每10秒扫描一下配置文件的变化;   -->
<context-param>
	<param-name>log4jRefreshInterval</param-name>
	<param-value>100000</param-value>
</context-param>
<listener>
	<!-- 容器初始化(销毁)时开启(关闭)Log4j -->
	<!-- 1. 动态的改变记录级别和策略,不需要重启Web应用; 2. 把log文件定在 /WEB-INF/logs/ 而不需要写绝对路径。 -->
	<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

log4j使用示例


获得logger 对象
private static final Logger logger=Logger.getLogger(Test.class);

 打印日志
logger.fatal("致命错误");
logger.error("严重警告");
logger.warn("警告");
logger.info("普通信息");
logger.debug("调试信息");
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaCourse/202201/2142.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者