log4j学习笔记一(简单配置log4j)
Posted 浅书流年
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了log4j学习笔记一(简单配置log4j)相关的知识,希望对你有一定的参考价值。
1.导入log4j-1.2.17.jar包
2.在web项目的web.xml中加入以下配置
<!-- 配置Log4j -->
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webApp.root</param-value>
</context-param>
<!-- 表示每个3秒动态扫描log4j.properties配置文件的变化 -->
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>3000</param-value>
</context-param>
<!-- 配置Log4j end -->
3.在项目的src目录下创建log4j的配置文件log4j.properties并写入配置
### set log levels ###
log4j.rootLogger = DEBUG,stdout
### \\u8F93\\u51FA\\u5230\\u63A7\\u5236\\u53F0 ###
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 = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
### sql ###
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG
log4j.logger.com.ibatis=DEBUG
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG
### file ###
log4j.appender.file = org.apache.log4j.RollingFileAppender
log4j.appender.file.Append=true
log4j.appender.file.MaxFileSize=5000KB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout = org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern =%d{yyyyMMdd HH\\:mm\\:ss,SSS} %5p [%c] %m%n
log4j.appender.file.encoding=UTF-8
4.创建测试类
package com.egiupt.webService;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class Log4jTest {
public static Logger logger=Logger.getLogger(Log4jTest.class);
@ResponseBody
@RequestMapping("/log4j")
public void login(HttpSession session){
logger.info("请求登录");
logger.debug("debug");
logger.warn("warn");
}
}
6.运行项目发送请求后可以再控制台看到
以上是关于log4j学习笔记一(简单配置log4j)的主要内容,如果未能解决你的问题,请参考以下文章