1、在工程中引入log4j-1.2.12.jar和commons-logging.jar.
commons-logging.jar的版本为1.0.3,低版本的配置稍有不同
2、在WEB-INF/classes目录下新建commons-logging.properties文件,文件内容如下:
commons-logging.jar的版本为1.0.3,低版本的配置稍有不同
2、在WEB-INF/classes目录下新建commons-logging.properties文件,文件内容如下:
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger
此行将log实现指定为Log4j
3、在WEB-INF/classes目录下新建log4j.properties文件,简单的配置文件示例如下:
3、在WEB-INF/classes目录下新建log4j.properties文件,简单的配置文件示例如下:
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j.properties</param-value>
</context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j.properties</param-value>
</context-param>
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>60000</param-value>
</context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>60000</param-value>
</context-param>
注意一:在上述实现类中也可以选用Log4JCategoryLog,但是会出现一些异常:FAIL - Encountered exception java.lang.NoSuchMethodError: org.apache.log4j.Category.log(****)
原因:commons-logger.jar和你的log4j.jar版本不兼容。前者是对后者的一个封装,而hibernate中主要是使用前者。前者的功能就是:它封装了一些通常的日志类,比如log4j,java.util.logging.Logger 等,使用的log4j,但是由于版本不对,commons-logger中用了log4j中的那个类,在当前的log4j版本中已经不存在了,所以报错
解决方法:将class目录下commons-logging.properties
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JCategoryLog
更改为
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JCategoryLog
更改为
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger
注意二:在配置过程中我log4j.properties放于WEB-INF目录下,并修改相应的log4jConfigLocation,结果没有成功,不过用上述的配置没有问题
注意三:在配置输出路径时由于粗心写成如下:
log4j.appender.file.File=D:/log.log
没有看到任何结果,后来发现路径写错拉,这点要注意啊,呵呵
log4j.appender.file.File=D:/log.log
网友例一:功能比较多
LOG4J的配置之简单使它遍及于越来越多的应用中了:Log4J配置文件实现了输出到控制台、文件、回滚文件、发送日志邮件、输出到数据库日志表、自定义标签等全套功能。择其一二使用就够用了,
log4j.rootLogger=DEBUG,CONSOLE,A1,
im
log4j.addivity.org.apache= true
# 应用于控制台
log4j.appender.CONSOLE= org.apache.log4j.ConsoleAppender
log4j.appender.Threshold= DEBUG
log4j.appender.CONSOLE.Target= System.out
log4j.appender.CONSOLE.layout= org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
#log4j.appender.CONSOLE.layout.ConversionPattern=[start]%d{DATE}[DATE]%n%p[PRIORITY]%n%x[NDC]%n%t[THREAD] n%c[CATEGORY]%n%m[MESSAGE] %n%n
#应用于文件
log4j.appender.FILE= org.apache.log4j.FileAppender
log4j.appender.FILE.File= file.log
log4j.appender.FILE.Append= false
log4j.appender.FILE.layout= org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
# Use this layout for LogFactor 5 analysis
# 应用于文件回滚
log4j.appender.ROLLING_FILE= org.apache.log4j.RollingFileAppender
log4j.appender.ROLLING_FILE.Threshold= ERROR
log4j.appender.ROLLING_FILE.File= rolling.log
log4j.appender.ROLLING_FILE.Append= true
log4j.appender.ROLLING_FILE.MaxFileSize= 10KB
log4j.appender.ROLLING_FILE.MaxBackupIndex=1
log4j.appender.ROLLING_FILE.layout= org.apache.log4j.PatternLayout
log4j.appender.ROLLING_FILE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
#应用于socket
log4j.appender.SOCKET= org.apache.log4j.RollingFileAppender
log4j.appender.SOCKET.RemoteHost= localhost
log4j.appender.SOCKET.Port=5001
log4j.appender.SOCKET.LocationInfo= true
# Set up for Log Facter 5
log4j.appender.SOCKET.layout= org.apache.log4j.PatternLayout
log4j.appender.SOCET.layout.ConversionPattern=[start]%d{DATE}[DATE]%n%p[PRIORITY]%n%x[NDC]%n%t[THREAD]%n%c[CATEGORY]%n%m[MESSAGE] %n%n
# Log Factor 5 Appender
log4j.appender.LF5_APPENDER= org.apache.log4j.lf5.LF5Appender
log4j.appender.LF5_APPENDER.MaxNumberOfRecords=2000
# 发送日志给邮件
log4j.appender.MAIL= org.apache.log4j.net.SMTPAppender
log4j.appender.MAIL.Threshold= FATAL
log4j.appender.MAIL.BufferSize=10
log4j.appender.MAIL.From= web@www.wuset.com
log4j.appender.MAIL.SMTPHost= www.wusetu.com
log4j.appender.MAIL.Subject= Log4J Message
log4j.appender.MAIL.To= web@www.wusetu.com
log4j.appender.MAIL.layout= org.apache.log4j.PatternLayout
log4j.appender.MAIL.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
# 用于数据库
log4j.appender.DATABASE= org.apache.log4j.jdbc.JDBCAppender
log4j.appender.DATABASE.URL=jdbc:mysql://localhost:3306 /test
log4j.appender.DATABASE.driver= com.mysql.jdbc.Driver
log4j.appender.DATABASE.user= root
log4j.appender.DATABASE.password=
log4j.appender.DATABASE.sql=INSERT INTO LOG4J (Message) VALUES ('[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n')
log4j.appender.DATABASE.layout= org.apache.log4j.PatternLayout
log4j.appender.DATABASE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
log4j.appender.A1= org.apache.log4j.DailyRollingFileAppender
log4j.appender.A1.File= SampleMessages.log4j
log4j.appender.A1.DatePattern= yyyyMMdd-HH'.log4j'
log4j.appender.A1.layout= org.apache.log4j.xml.XMLLayout
#自定义Appender
log4j.appender.im = net.cybercorlin.util.logger.appender.IMAppender
log4j.appender.im.host = mail.cybercorlin.net
log4j.appender.im.username = username
log4j.appender.im.password = password
log4j.appender.im.recipient = corlin@cybercorlin.net
log4j.appender.im.layout= org.apache.log4j.PatternLayout
log4j.appender.im.layout.ConversionPattern =[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
log4j.addivity.org.apache= true
# 应用于控制台
log4j.appender.CONSOLE= org.apache.log4j.ConsoleAppender
log4j.appender.Threshold= DEBUG
log4j.appender.CONSOLE.Target= System.out
log4j.appender.CONSOLE.layout= org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
#log4j.appender.CONSOLE.layout.ConversionPattern=[start]%d{DATE}[DATE]%n%p[PRIORITY]%n%x[NDC]%n%t[THREAD] n%c[CATEGORY]%n%m[MESSAGE] %n%n
#应用于文件
log4j.appender.FILE= org.apache.log4j.FileAppender
log4j.appender.FILE.File= file.log
log4j.appender.FILE.Append= false
log4j.appender.FILE.layout= org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
# Use this layout for LogFactor 5 analysis
# 应用于文件回滚
log4j.appender.ROLLING_FILE= org.apache.log4j.RollingFileAppender
log4j.appender.ROLLING_FILE.Threshold= ERROR
log4j.appender.ROLLING_FILE.File= rolling.log
log4j.appender.ROLLING_FILE.Append= true
log4j.appender.ROLLING_FILE.MaxFileSize= 10KB
log4j.appender.ROLLING_FILE.MaxBackupIndex=1
log4j.appender.ROLLING_FILE.layout= org.apache.log4j.PatternLayout
log4j.appender.ROLLING_FILE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
#应用于socket
log4j.appender.SOCKET= org.apache.log4j.RollingFileAppender
log4j.appender.SOCKET.RemoteHost= localhost
log4j.appender.SOCKET.Port=5001
log4j.appender.SOCKET.LocationInfo= true
# Set up for Log Facter 5
log4j.appender.SOCKET.layout= org.apache.log4j.PatternLayout
log4j.appender.SOCET.layout.ConversionPattern=[start]%d{DATE}[DATE]%n%p[PRIORITY]%n%x[NDC]%n%t[THREAD]%n%c[CATEGORY]%n%m[MESSAGE] %n%n
# Log Factor 5 Appender
log4j.appender.LF5_APPENDER= org.apache.log4j.lf5.LF5Appender
log4j.appender.LF5_APPENDER.MaxNumberOfRecords=2000
# 发送日志给邮件
log4j.appender.MAIL= org.apache.log4j.net.SMTPAppender
log4j.appender.MAIL.Threshold= FATAL
log4j.appender.MAIL.BufferSize=10
log4j.appender.MAIL.From= web@www.wuset.com
log4j.appender.MAIL.SMTPHost= www.wusetu.com
log4j.appender.MAIL.Subject= Log4J Message
log4j.appender.MAIL.To= web@www.wusetu.com
log4j.appender.MAIL.layout= org.apache.log4j.PatternLayout
log4j.appender.MAIL.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
# 用于数据库
log4j.appender.DATABASE= org.apache.log4j.jdbc.JDBCAppender
log4j.appender.DATABASE.URL=jdbc:mysql://localhost:3306 /test
log4j.appender.DATABASE.driver= com.mysql.jdbc.Driver
log4j.appender.DATABASE.user= root
log4j.appender.DATABASE.password=
log4j.appender.DATABASE.sql=INSERT INTO LOG4J (Message) VALUES ('[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n')
log4j.appender.DATABASE.layout= org.apache.log4j.PatternLayout
log4j.appender.DATABASE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
log4j.appender.A1= org.apache.log4j.DailyRollingFileAppender
log4j.appender.A1.File= SampleMessages.log4j
log4j.appender.A1.DatePattern= yyyyMMdd-HH'.log4j'
log4j.appender.A1.layout= org.apache.log4j.xml.XMLLayout
#自定义Appender
log4j.appender.im = net.cybercorlin.util.logger.appender.IMAppender
log4j.appender.im.host = mail.cybercorlin.net
log4j.appender.im.username = username
log4j.appender.im.password = password
log4j.appender.im.recipient = corlin@cybercorlin.net
log4j.appender.im.layout= org.apache.log4j.PatternLayout
log4j.appender.im.layout.ConversionPattern =[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
网友例二:比较实用
#root输出源配置
log4j.rootLogger = DEBUG , rootlog
log4j.appender.rootlog = org.apache.log4j.RollingFileAppender
log4j.appender.rootlog.MaxFileSize = 50000KB
log4j.appender.rootlog.MaxBackupIndex = 0
log4j.appender.rootlog.file = D:/jakarta-tomcat- 4.1.30 /webapps/ROOT/logs/all.log
log4j.appender.rootlog.layout = org.apache.log4j.PatternLayout
log4j.appender.rootlog.layout.ConversionPattern = %d %-5p %m%n
#debug输出源配置
log4j.logger.debuglog = DEBUG , debuglog
log4j.appender.debuglog = org.apache.log4j.RollingFileAppender
log4j.appender.debuglog.MaxFileSize = 50000KB
log4j.appender.debuglog.MaxBackupIndex = 0
log4j.appender.debuglog.file = D:/jakarta-tomcat- 4.1.30 /webapps/ROOT/logs/debug.log
log4j.appender.debuglog.layout = org.apache.log4j.PatternLayout
log4j.appender.debuglog.layout.ConversionPattern = %m%n
#info输出源配置
log4j.logger.infolog = DEBUG , infolog
log4j.appender.infolog = org.apache.log4j.RollingFileAppender
log4j.appender.infolog.MaxFileSize = 50000KB
log4j.appender.infolog.MaxBackupIndex = 0
log4j.appender.infolog.file = D:/jakarta-tomcat- 4.1.30 /webapps/ROOT/logs/info.log
log4j.appender.infolog.layout = org.apache.log4j.PatternLayout
log4j.appender.infolog.layout.ConversionPattern = %m%n
#warn输出源配置
log4j.logger.warnlog = DEBUG , warnlog
log4j.appender.warnlog = org.apache.log4j.RollingFileAppender
log4j.appender.warnlog.MaxFileSize = 50000KB
log4j.appender.warnlog.MaxBackupIndex = 0
log4j.appender.warnlog.file = D:/jakarta-tomcat- 4.1.30 /webapps/ROOT/logs/warn.log
log4j.appender.warnlog.layout = org.apache.log4j.PatternLayout
log4j.appender.warnlog.layout.ConversionPattern = %d %X %-5p %m%n
#error输出源配置
log4j.logger.errorlog = DEBUG , errorlog
log4j.appender.errorlog = org.apache.log4j.RollingFileAppender
log4j.appender.errorlog.MaxFileSize = 50000KB
log4j.appender.errorlog.MaxBackupIndex = 0
log4j.appender.errorlog.file = D:/jakarta-tomcat- 4.1.30 /webapps/ROOT/logs/error.log
log4j.appender.errorlog.layout = org.apache.log4j.PatternLayout
log4j.appender.errorlog.layout.ConversionPattern = %d %x %-5p %m%n
log4j.rootLogger = DEBUG , rootlog
log4j.appender.rootlog = org.apache.log4j.RollingFileAppender
log4j.appender.rootlog.MaxFileSize = 50000KB
log4j.appender.rootlog.MaxBackupIndex = 0
log4j.appender.rootlog.file = D:/jakarta-tomcat- 4.1.30 /webapps/ROOT/logs/all.log
log4j.appender.rootlog.layout = org.apache.log4j.PatternLayout
log4j.appender.rootlog.layout.ConversionPattern = %d %-5p %m%n
#debug输出源配置
log4j.logger.debuglog = DEBUG , debuglog
log4j.appender.debuglog = org.apache.log4j.RollingFileAppender
log4j.appender.debuglog.MaxFileSize = 50000KB
log4j.appender.debuglog.MaxBackupIndex = 0
log4j.appender.debuglog.file = D:/jakarta-tomcat- 4.1.30 /webapps/ROOT/logs/debug.log
log4j.appender.debuglog.layout = org.apache.log4j.PatternLayout
log4j.appender.debuglog.layout.ConversionPattern = %m%n
#info输出源配置
log4j.logger.infolog = DEBUG , infolog
log4j.appender.infolog = org.apache.log4j.RollingFileAppender
log4j.appender.infolog.MaxFileSize = 50000KB
log4j.appender.infolog.MaxBackupIndex = 0
log4j.appender.infolog.file = D:/jakarta-tomcat- 4.1.30 /webapps/ROOT/logs/info.log
log4j.appender.infolog.layout = org.apache.log4j.PatternLayout
log4j.appender.infolog.layout.ConversionPattern = %m%n
#warn输出源配置
log4j.logger.warnlog = DEBUG , warnlog
log4j.appender.warnlog = org.apache.log4j.RollingFileAppender
log4j.appender.warnlog.MaxFileSize = 50000KB
log4j.appender.warnlog.MaxBackupIndex = 0
log4j.appender.warnlog.file = D:/jakarta-tomcat- 4.1.30 /webapps/ROOT/logs/warn.log
log4j.appender.warnlog.layout = org.apache.log4j.PatternLayout
log4j.appender.warnlog.layout.ConversionPattern = %d %X %-5p %m%n
#error输出源配置
log4j.logger.errorlog = DEBUG , errorlog
log4j.appender.errorlog = org.apache.log4j.RollingFileAppender
log4j.appender.errorlog.MaxFileSize = 50000KB
log4j.appender.errorlog.MaxBackupIndex = 0
log4j.appender.errorlog.file = D:/jakarta-tomcat- 4.1.30 /webapps/ROOT/logs/error.log
log4j.appender.errorlog.layout = org.apache.log4j.PatternLayout
log4j.appender.errorlog.layout.ConversionPattern = %d %x %-5p %m%n
网友例二:其余的
在spring+hibernate+struts项目开发中的log4j配置
关键词: log4j.rootLogge
##*********************自定义log4j配置*******************************##
### copy by MSY ###
### 设置根Logger级别INFO , 输出地MSY,MSY2 ##
log4j.rootLogger = DEBUG , MSY , MSY2
设置com.hcinfo.velocity域对应的级别INFO , DEBUG , WARN , ERROR和输出地MSY,MSY2
log4j.category.com.hcinfo.velocity = DEBUG
### 定义stdout的输出到控制断 ###
#log4j.appender.MSY = org.apache.log4j.ConsoleAppender
#log4j.appender.MSY.Target = System.out
### 定义stdout布局模式为PatternLayout ###
#log4j.appender.MSY.layout = org.apache.log4j.PatternLayout
### 定义stdout的输出格式 ###
#log4j.appender.MSY.layout.ConversionPattern = %d{ABSOLUTE} %5p %c{ 1 }:%L - %m%n
### 定义MSY输出到文件(文件大小到达指定尺寸的时候产生一个新的文件) ###
log4j.appender.MSY2 = org.apache.log4j.RollingFileAppender
### 文件位置 ###
log4j.appender.MSY2.File = e:/sys2.txt
### 文件最大长度 ###
log4j.appender.MSY2.MaxFileSize = 500KB
### 备份文件数目 ###
log4j.appender.MSY2.MaxBackupIndex = 1
log4j.appender.MSY2.layout = org.apache.log4j.PatternLayout
log4j.appender.MSY2.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss , SSS} [ %t ] [ %c ] [ %p ] - %m%n
### 配置日志输出的格式##
### 格式说明
# -X号:X信息输出时左对齐;
# %p:日志信息级别
# %d{}:日志信息产生时间
# %c:日志信息所在地(类名)
# %m:产生的日志具体信息
# %n:输出日志信息换行
### 下面是定义 hibernate LOG4J 配置格式 ###
### direct log messages to stdout ###
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 = %d{ABSOLUTE} %5p %c{ 1 }:%L - %m%n
### direct messages to file hibernate.log ###
#log4j.appender.file = org.apache.log4j.FileAppender
#log4j.appender.file.File = hibernate.log
#log4j.appender.file.layout = org.apache.log4j.PatternLayout
#log4j.appender.file.layout.ConversionPattern = %d{ABSOLUTE} %5p %c{ 1 }:%L - %m%n
### set log levels - for more verbose logging change 'info' to 'debug' ###
### 在次可定义输出输出端stdout ###
log4j.rootLogger = warn , stdout
log4j.logger.net.sf.hibernate = info
### log just the SQL
#log4j.logger.net.sf.hibernate.SQL = debug
### log JDBC bind parameters ###
log4j.logger.net.sf.hibernate.type = info
### log schema export/update ###
log4j.logger.net.sf.hibernate.tool.hbm2ddl = debug
### log cache activity ###
#log4j.logger.net.sf.hibernate.cache = debug
### log jdbc resource acquisition
#log4j.logger.net.sf.hibernate.impl.BatcherImpl = debug
### enable the following line if you want to track down connection ###
### leakages when using DriverManagerConnectionProvider ###
#log4j.logger.net.sf.hibernate.connection.DriverManagerConnectionProvider = trace
## 定义 log4j 的 spring 配置 ##
log4j.rootLogger = DEBUG , Spring
log4j.appender.Spring = org.apache.log4j.ConsoleAppender
log4j.appender.Spring.Target = System.out
log4j.appender.Spring.layout = org.apache.log4j.PatternLayout
log4j.appender.Spring.layout.ConversionPattern = %d{ABSOLUTE} %5p %c{ 1 }:%L - %m%n
####
### copy by MSY ###
### 设置根Logger级别INFO , 输出地MSY,MSY2 ##
log4j.rootLogger = DEBUG , MSY , MSY2
设置com.hcinfo.velocity域对应的级别INFO , DEBUG , WARN , ERROR和输出地MSY,MSY2
log4j.category.com.hcinfo.velocity = DEBUG
### 定义stdout的输出到控制断 ###
#log4j.appender.MSY = org.apache.log4j.ConsoleAppender
#log4j.appender.MSY.Target = System.out
### 定义stdout布局模式为PatternLayout ###
#log4j.appender.MSY.layout = org.apache.log4j.PatternLayout
### 定义stdout的输出格式 ###
#log4j.appender.MSY.layout.ConversionPattern = %d{ABSOLUTE} %5p %c{ 1 }:%L - %m%n
### 定义MSY输出到文件(文件大小到达指定尺寸的时候产生一个新的文件) ###
log4j.appender.MSY2 = org.apache.log4j.RollingFileAppender
### 文件位置 ###
log4j.appender.MSY2.File = e:/sys2.txt
### 文件最大长度 ###
log4j.appender.MSY2.MaxFileSize = 500KB
### 备份文件数目 ###
log4j.appender.MSY2.MaxBackupIndex = 1
log4j.appender.MSY2.layout = org.apache.log4j.PatternLayout
log4j.appender.MSY2.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss , SSS} [ %t ] [ %c ] [ %p ] - %m%n
### 配置日志输出的格式##
### 格式说明
# -X号:X信息输出时左对齐;
# %p:日志信息级别
# %d{}:日志信息产生时间
# %c:日志信息所在地(类名)
# %m:产生的日志具体信息
# %n:输出日志信息换行
### 下面是定义 hibernate LOG4J 配置格式 ###
### direct log messages to stdout ###
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 = %d{ABSOLUTE} %5p %c{ 1 }:%L - %m%n
### direct messages to file hibernate.log ###
#log4j.appender.file = org.apache.log4j.FileAppender
#log4j.appender.file.File = hibernate.log
#log4j.appender.file.layout = org.apache.log4j.PatternLayout
#log4j.appender.file.layout.ConversionPattern = %d{ABSOLUTE} %5p %c{ 1 }:%L - %m%n
### set log levels - for more verbose logging change 'info' to 'debug' ###
### 在次可定义输出输出端stdout ###
log4j.rootLogger = warn , stdout
log4j.logger.net.sf.hibernate = info
### log just the SQL
#log4j.logger.net.sf.hibernate.SQL = debug
### log JDBC bind parameters ###
log4j.logger.net.sf.hibernate.type = info
### log schema export/update ###
log4j.logger.net.sf.hibernate.tool.hbm2ddl = debug
### log cache activity ###
#log4j.logger.net.sf.hibernate.cache = debug
### log jdbc resource acquisition
#log4j.logger.net.sf.hibernate.impl.BatcherImpl = debug
### enable the following line if you want to track down connection ###
### leakages when using DriverManagerConnectionProvider ###
#log4j.logger.net.sf.hibernate.connection.DriverManagerConnectionProvider = trace
## 定义 log4j 的 spring 配置 ##
log4j.rootLogger = DEBUG , Spring
log4j.appender.Spring = org.apache.log4j.ConsoleAppender
log4j.appender.Spring.Target = System.out
log4j.appender.Spring.layout = org.apache.log4j.PatternLayout
log4j.appender.Spring.layout.ConversionPattern = %d{ABSOLUTE} %5p %c{ 1 }:%L - %m%n
####
web.xml中的修改:
这样配置文件以及日志文件可以在spring的帮助下,放到开发环境中的任意位置
在这里定位配置文件,需要的是从root开始的绝对路径
,或者设置个
<
context-param
>
< param-name > log4jConfigLocation </ param-name >
< param-value > classpath:log4j.properties </ param-value >
</ context-param >
< param-name > log4jConfigLocation </ param-name >
< param-value > classpath:log4j.properties </ param-value >
</ context-param >
<!--
Spring默认刷新Log4j配置文件的间隔,单位为millisecond
-->
< context-param >
< param-name > log4jRefreshInterval </ param-name >
< param-value > 60000 </ param-value >
</ context-param >
<!-- Spring log4j Config loader -->
< listener >
< listener-class > org.springframework.web.util.Log4jConfigListener </ listener-class >
</ listener >
< context-param >
< param-name > log4jRefreshInterval </ param-name >
< param-value > 60000 </ param-value >
</ context-param >
<!-- Spring log4j Config loader -->
< 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 >
< servlet >
< servlet-name > log4j </ servlet-name >
< servlet-class >
org.springframework.web.util.Log4jConfigServlet
</ servlet-class >
< load-on-startup > 0 </ load-on-startup >
</ servlet >
< param-name > log4jConfigLocation </ param-name >
< param-value > classpath:log4j.properties </ param-value >
</ context-param >
< servlet >
< servlet-name > log4j </ servlet-name >
< servlet-class >
org.springframework.web.util.Log4jConfigServlet
</ servlet-class >
< load-on-startup > 0 </ load-on-startup >
</ servlet >
==================================
什么都做完了,那该测试了,
在action中增加
static
Logger _log
=
Logger.getLogger(
this
.getClass().getName);
// log4j(1) 然后就可以在需要的地方进行日志记载
_log.debug( " 做下测试.debug " );
_log.info( " 做下测试.info " );
_log.warn( " 做下测试.warn " );
_log.error( " 做下测试.error " );
_log.fatal( " 做下测试.fatal " );
// log4j(1) 然后就可以在需要的地方进行日志记载
_log.debug( " 做下测试.debug " );
_log.info( " 做下测试.info " );
_log.warn( " 做下测试.warn " );
_log.error( " 做下测试.error " );
_log.fatal( " 做下测试.fatal " );
补充:
有时候log日志的位置总觉的绝对路径不爽,那你可以用参数啊,比如${webapp.root}/WEB-INF/log/就是你的项目路径里的了,具体在哪里你自己看了