log4j使用方法

来源:互联网 发布:淘宝打包员招聘要求 编辑:程序博客网 时间:2024/06/03 14:53

把log4j.properties放在src目录下,具体配置实例如下:

# This is the configuring for logging displayed in the Application Serverlog4j.rootCategory=INFO, stdout, logfile#stdout configurelog4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern= %d %p [%c] - <%m>%n#logfile configurelog4j.appender.logfile=org.apache.log4j.DailyRollingFileAppenderlog4j.appender.logfile.File=../logs/scm.loglog4j.appender.logfile.layout=org.apache.log4j.PatternLayoutlog4j.appender.logfile.layout.ConversionPattern= %d %p [%c] - <%m>%n# Control logging for other open source packages# Changing the log level to DEBUG when debuglog4j.logger.org.springframework=INFOlog4j.logger.org.springframework.jdbc.core.JdbcTemplate=DEBUG# debug log for ibatis2.3#log4j.logger.java.sql.Connection=DEBUG#log4j.logger.java.sql.Statement=INFO#log4j.logger.java.sql.PreparedStatement=INFO#log4j.logger.java.sql.ResultSet=INFO# debug log for ibatis3log4j.logger.java.sql.Connection=DEBUGlog4j.logger.java.sql.Statement=DEBUGlog4j.logger.java.sql.PreparedStatement=DEBUGlog4j.logger.java.sql.ResultSet=INFOlog4j.logger.com.pds=DEBUG# Changing the log level to DEBUG will display SQL Hibernate generatedlog4j.logger.org.hibernate=INFOlog4j.logger.org.hibernate.SQL=ERRORlog4j.logger.org.hibernate.cache=ERRORlog4j.logger.net.sf.ehcache=ERRORlog4j.logger.org.apache.commons.validator.ValidatorResources=INFO#Struts OgnlUtil issues unimportant warnings log4j.logger.com.opensymphony.xwork2.util.OgnlUtil=error log4j.logger.com.opensymphony.xwork2.ognl.OgnlValueStack=error

使用方法:在类成员里定义

private Logger log = Logger.getLogger(getClass());
该类需要打日志的地方: log.info("------------------------------常量配置完成-------------------------");

控制台输出如下:

2017-10-28 11:38:57,035 INFO [org.hibernate.cfg.annotations.EntityBinder] - <------------------------------常量配置完成------------------------->