log4j如何配置到spring中
本篇主要写一个完整的配置,只是一个大概的流程,至于配置文件的详细信息很多我也在学习,大家想了解的话可以百度一下,通过下面的流程配置好后,就能通过Logger/Log来以不同的方式记录log信息了。
1.首先在web.xml中添加如下信息。
log4j.properties也可以换成xml文件配置,只要指定相应的路径就行了。
<listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>/log4j.properties</param-value> </context-param>
2.log4j.properties
具体如何配置,可以baidu一下。
# $Id: Action.java 502296 2007-02-01 17:33:39Z niallp $
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
log4j.rootLogger = WARN, stdout,R
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
#log4j.appender.stdout.Threshold = WARN
#log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = %d{ISO8601} %-5p [%F:%L] : %m%n
log4j.appender.R = org.apache.log4j.FileAppender
log4j.appender.R.File = D:/apache-tomcat-6.0.35/logs/qc.log
#log4j.appender.stdout.Threshold = WARN
#log4j.appender.stdout.Target = System.out
log4j.appender.R.layout = org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern = %d{ISO8601} %-5p [%F:%L] : %m%n
#log4j.logger.Time = ERROR,T
#log4j.appender.T = org.apache.log4j.FileAppender
#log4j.appender.T.File = D:/apache-tomcat-7.0.29/logs/qc.txt
##log4j.appender.stdout.Threshold = WARN
##log4j.appender.stdout.Target = System.out
#log4j.appender.T.layout = org.apache.log4j.PatternLayout
#log4j.appender.T.layout.ConversionPattern = %-5p [%F:%L] : %m%n
3.log4j.xml
控制台输出
<?xml version="1.0" encoding="GBK" ?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> <!-- 输出日志到控制台 ConsoleAppender --> <appender name="console" class="org.apache.log4j.ConsoleAppender"> <param name="Threshold" value="info"></param> <layout class="org.apache.log4j.TTCCLayout"> <param name="ConversionPattern" value="TTCCLayout"></param> </layout> </appender> <root> <priority value="debug" /> <appender-ref ref="console" /> </root> </log4j:configuration>
如果是使用log4j.properties文件的话,在启动tomcat时会有下面的WARN信息:
log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
log4j:WARN Please initialize the log4j system properly.
如果想去掉这些信息的话,可以将文件挪到/WEB-INF/src下面。具体为什么能够去掉,我也不知道。望知道的告诉Me一声,谢谢。