浏览 1345 次
锁定老帖子 主题:web 应用下的日志记录(转)
该帖已经被评为隐藏帖
|
|
---|---|
作者 | 正文 |
发表时间:2010-04-27
在WEB-INF/class下建立log4j.properties,用来设定log4j的配置信息。 范例: log4j.rootLogger=debug, stdout, R log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout # Pattern to output the caller's file name and line number. log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n log4j.appender.R=org.apache.log4j.RollingFileAppender log4j.appender.R.File=example.log log4j.appender.R.MaxFileSize=100KB # Keep one backup file log4j.appender.R.MaxBackupIndex=1 log4j.appender.R.layout=org.apache.log4j.PatternLayout log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n 说明 : 1.配置根Logger,其语法为: log4j.rootLogger = [ level ] , appenderName, appenderName, … 其中,level是日志记录的优先级,分为OFF、FATAL、ERROR、WARN、INFO、DEBUG、ALL或者您定义的级别。Log4j建议只使用四个级别,优 先级从高到低分别是ERROR、WARN、INFO、DEBUG。通过在这里定义的级别,您可以控制到应用程序中相应级别的日志信息的开关。比如在这里定 义了INFO级别,则应用程序中所有DEBUG级别的日志信息将不被打印出来。appenderName就是指定日志信息输出到哪个地方。您可以同时指定多个输出目的地。 2.配置日志信息输出目的地Appender,其语法为: log4j.appender.appenderName = fully.qualified.name.of.appender.classlog4j.appender.appenderName.option1 = value1 …log4j.appender.appenderName.option = valueN 其中,Log4j提供的appender有以下几种: org.apache.log4j.ConsoleAppender(控制台), org.apache.log4j.FileAppender(文件), org.apache.log4j.DailyRollingFileAppender(每天产生一个日志文件), org.apache.log4j.RollingFileAppender(文件大小到达指定尺寸的时候产生一个新的文件), org.apache.log4j.WriterAppender(将日志信息以流格式发送到任意指定的地方) 3.配置日志信息的格式(布局),其语法为: log4j.appender.appenderName.layout = fully.qualified.name.of.layout.classlog4j.appender.appenderName.layout.option1 = value1 …log4j.appender.appenderName.layout.option = valueN 其中,Log4j提供的layout有以下几种: org.apache.log4j.HTMLLayout(以HTML表格形式布局), org.apache.log4j.PatternLayout(可以灵活地指定布局模式), org.apache.log4j.SimpleLayout(包含日志信息的级别和信息字符串), org.apache.log4j.TTCCLayout(包含日志产生的时间、线程、类别等等信息) 二、编写servlet,自动完成log4j的初始化 import javax.servlet.http.HttpServlet; import org.apache.log4j.PropertyConfigurator; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class Log4jInit extends HttpServlet { private static final long serialVersionUID = 1L; public void init() { String prefix = getServletContext().getRealPath("/"); String file = getInitParameter("log4j-init-file"); // if the log4j-init-file is not set, then no point in trying if (file != null) { PropertyConfigurator.configure(prefix + file); } } public void doGet(HttpServletRequest req, HttpServletResponse res) { } } 三、在web.xml中配置该servlet <servlet> <servlet-name>log4j-init</servlet-name> <servlet-class>com.foo.Log4jInit</servlet-class> <init-param> <param-name>log4j-init-file</param-name> <param-value>WEB-INF/classes/log4j.properties</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> 四、使用log4j 无论是在jsp中还是在java中,首先应引入类库 import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; 使用时:Log log = LogFactory.getLog("logName"); 这里似乎有个疑问了,为什么没有直接使用org.apache.log4j.Logger 和 org.apache.log4j.LogManager? 在Jakarta Commons 有一段描述: The default LogFactory implementation uses the following discovery process to determine what type of Log implementation it should use (the process terminates when the first positive match - in order - is found): Look for a configuration attribute of this factory named org.apache.commons.logging.Log (for backwards compatibility to pre-1.0 versions of this API, an attribute org.apache.commons.logging.log is also consulted). Look for a system property named org.apache.commons.logging.Log (for backwards compatibility to pre-1.0 versions of this API, a system property org.apache.commons.logging.log is also consulted). If the Log4J logging system is available in the application class path, use the corresponding wrapper class ( Log4JLogger ). If the application is executing on a JDK 1.4 system, use the corresponding wrapper class ( Jdk14Logger ). Fall back to the default simple logging wrapper ( SimpleLog ). 在大多数情况下,日志继承使得将log4j作为底层的支持成为首选。 来源:http://hi.baidu.com/lkf0217/blog/item/7d9fbf51088fcd2d42a75b3f.html 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |