Tomcat的servlet的日志记录 [英] Tomcat servlet logging

查看:185
本文介绍了Tomcat的servlet的日志记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Servlet容器和创造使用Tomcat 6.0.26 Web应用程序。我有'TODO:登录散落在我的code。我看到有存在:

I'm new to Servlet containers and have created a web application using Tomcat 6.0.26. I have 'TODO: log' scattered throughout my code. I see there exists:

myServlet.getServletContext()日志()

myServlet.getServletContext().log()

这似乎写在Tomcat的'localhost'的'/日志目录pfixed文件$ P $。我不需要任何先进的日志记录功能,但我想一个日期,时间,消息和最低堆栈跟踪。此外,我创建了我的需要的日志记录功能,以及各种使用servlet的一些类。我需要注入SevletContext到这些类,因此他们可以登录?

which appears to write to a file prefixed with 'localhost' in the Tomcat '/logs' directory. I don't need any advanced logging capability, but I'd like a date, time, message and stack trace at minimum. In addition, I've created some classes used by my various servlets that need logging capabilities as well. Do I need to inject a SevletContext into these classes so they can log?

看来,来自Apache log4j的是一个流行的日志记录包,但我不知道这是否值得设置它的麻烦。

It appears that log4j from Apache is a popular logging package, but I'm not sure if it worth the trouble of setting it up.

什么是记录我的需求的推荐方法?

What would be the recommended way of logging for my needs?

推荐答案

在除此之外的所有东西BalusC有上面提到的,在你的java code(的servlet /豆/不管)刚刚导入和初始化记录仪

In addition to all the stuff BalusC has mentioned above, in your java code (servlets/beans/whatever) just import and initialize the Logger

package my.app;

import org.apache.log4j.Logger;

private static Logger logger = Logger.getLogger("classname");
// use any string you want 

然后在任何记录点,你可以登录各级像

Then at any logging point you can log at various levels like

if (logger.isDebugEnabled()) {
            logger.debug("Some log at this point");
        }

...

logger.info("Some info message here");

这将取决于你是否在log4j.propeties设置调试或INFO出现在日志

These will appear in the logs depending on whether you set DEBUG or INFO in the log4j.propeties

看看例子在的http:// www.java2s.com/$c$c/Java/Language-Basics/Examplelog4jConfigurationFile.htm 和所有的同一篇文章中相关的示例

Take a look at the examples on http://www.java2s.com/Code/Java/Language-Basics/Examplelog4jConfigurationFile.htm and all the Related examples in the same article

这篇关于Tomcat的servlet的日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆