spring mvc配置集成log4j
springmvc中集成log4j非常简单,按照以下步骤就可以配置完成。
1. 添加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>
2. 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 级别以上的日志到=E://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 级别以上的日志到=E://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
3. 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>
4. 配置完成,log4j使用
1.获得logger的副本(参数是类的class对象)
private static final Logger logger=Logger.getLogger(Test.class);
2.打印日志
logger.fatal("致命错误");
logger.error("严重警告");
logger.warn("警告");
logger.info("普通信息");
logger.debug("调试信息");
版权声明:本文为JAVASCHOOL原创文章,未经本站允许不得转载。