赞
踩
当我们需要在项目中打印日志并输出到指定的日志文件中时,我们需要进行配置log4j进行日志的打印。
步骤如下:
如果是动态web工程需要引入如下两个jar包:
commons-logging-版本号.jar
log4j-版本号.jar
如果是maven工程 需要在pom中引入如下两个约束:
- <dependency>
- <groupId>log4j</groupId>
- <artifactId>log4j</artifactId>
- <version>1.2.17</version>
- </dependency>
- <dependency>
- <groupId>commons-logging</groupId>
- <artifactId>commons-logging</artifactId>
- <version>1.1.1</version>
- </dependency>
当在项目中引入了如上两个约束后,项目启动后,会在开发路径下:resources下,项目部署后classes路径下自动寻找配置文件log4j.properties
如果想要自定义log4j.properties配置文件位置,需要在web.xml中添加log4j的监听。
- <!-- 配置log4j配置文件路径 -->
- <context-param>
- <param-name>log4jConfigLocation</param-name>
- <param-value>classpath:log4j.properties</param-value>
- </context-param>
- <!-- 60s 检测日志配置 文件变化 -->
- <context-param>
- <param-name>log4jRefreshInterval</param-name>
- <param-value>60000</param-value>
- </context-param>
-
- <!-- 配置Log4j监听器 -->
- <listener>
- <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
- </listener>
demo:
- log4j.rootLogger=error,Console
-
- log4j.logger.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
- #log4j.logger.org.hibernate.type.descriptor.sql.BasicExtractor=TRACE
- log4j.logger.org.hibernate.tool.hbm2ddl=DEBUG
- #log4j.logger.org.hibernate.SQL=DEBUG
-
- log4j.appender.Console=org.apache.log4j.ConsoleAppender
- log4j.appender.Console.Target=System.out
- log4j.appender.Console.layout=org.apache.log4j.PatternLayout
- log4j.appender.Console.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH\:mm\:ss,SSS}][%c]%m%n
-
- log4j.appender.OneFile=org.apache.log4j.DailyRollingFileAppender
- log4j.appender.OneFile.File=../logs/AI_COMM_MANAGER.log
- log4j.appender.OneFile.DatePattern = '.'yyyy-MM-dd
- log4j.appender.OneFile.Threshold=ALL
- log4j.appender.OneFile.layout=org.apache.log4j.PatternLayout
- log4j.appender.OneFile.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH\:mm\:ss,SSS}][%c]%m%n
-
- log4j.logger.MyScan=DEBUG,scan
- log4j.additivity.MyScan=false
- log4j.appender.scan=org.apache.log4j.DailyRollingFileAppender
- log4j.appender.scan.File=../logs/scan.log
- log4j.appender.scan.DatePattern = '.'yyyy-MM-dd
- log4j.appender.scan.Threshold=ALL
- log4j.appender.scan.layout=org.apache.log4j.PatternLayout
- log4j.appender.scan.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH\:mm\:ss,SSS}][%c]%m%n
-
- log4j.logger.Count=DEBUG,count
- log4j.additivity.Count=false
- log4j.appender.count=org.apache.log4j.RollingFileAppender
- log4j.appender.count.File=../logs/count.log
- log4j.appender.FILE.MaxFileSize=10MB
- log4j.appender.FILE.Append = true
- log4j.appender.count.Threshold=ALL
- log4j.appender.count.layout=org.apache.log4j.PatternLayout
- log4j.appender.count.layout.ConversionPattern=[%p][%d{yyyy-MM-dd HH\:mm\:ss,SSS}][%c]%m%n
rootLogger参数配置的后面两个表示输出位置
也可以通过log4j.logger.自定义log名字=日志等级,名字
在代码里面通过名字获取指定的log对象,打印到自定义的日志文件中
private final static Log logger = LogFactory.getLog("Http");
注意导包一定是 apache.commons.logging下的包
备注:打印日志的工具类 还有logback等,很多,很杂,但是apache的log很经典,很好用
也可以参考 https://blog.csdn.net/qq_37733625/article/details/79046227
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。