java 如何从 Tomcat 6 中的 Web 应用程序内部登录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2589527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 21:56:05  来源:igfitidea点击:

How do I log from inside my web application in Tomcat 6

javalog4jtomcat6

提问by Carlos

How do I log from within my web application deployed on Tomcat 6? Where should I expect the logging output to go (internal tomcat log files, or will another logfile be generated)? I see a ton of documentation but am having a hard time finding a direct answer to the above questions. Where should I expect the logging to show up (currently it is log4j is not generating a log file and it is not showing up in my console). I am trying to follow http://www.laliluna.de/articles/log4j-tutorial.html.

如何从部署在 Tomcat 6 上的 Web 应用程序中登录?我应该在哪里期望日志输出(内部 tomcat 日志文件,或者会生成另一个日志文件)?我看到了大量文档,但很难找到上述问题的直接答案。我应该在哪里看到日志记录(目前是 log4j 没有生成日志文件,也没有显示在我的控制台中)。我正在尝试关注http://www.laliluna.de/articles/log4j-tutorial.html

### 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

### file appender
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.maxFileSize=100KB
log4j.appender.file.maxBackupIndex=5
log4j.appender.file.File=test.log
log4j.appender.file.threshold=info
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

log4j.rootLogger=debug, stdout

In my application I define a log object:

在我的应用程序中,我定义了一个日志对象:

private static org.apache.log4j.Logger log = Logger.getLogger(MyClass.class);
log.error("LOGGING!");

Thanks for the help.

谢谢您的帮助。

采纳答案by Martin Tilsted

2 things to try:

2件事要尝试:

1: Change test.log to /tmp/test.log so you know exactly where the file is supossed to be.

1:将 test.log 更改为 /tmp/test.log,这样您就可以确切地知道文件所在的位置。

2: Put your log4j.properties config file in your apache-tomcat-6.0.x/lib directory together with the log4j-1.2.15.jar file. And don't have any log4j files in your webapps/*/WEB-INF/lib

2:将您的 log4j.properties 配置文件与 log4j-1.2.15.jar 文件一起放在您的 apache-tomcat-6.0.x/lib 目录中。并且您的 webapps/*/WEB-INF/lib 中没有任何 log4j 文件

That's the way I am doing it, and its working for me. Here is a usefull snippet from my log4j.properties (Remember to do a mk /tmp/logs if you use this config)

这就是我这样做的方式,它对我有用。这是我的 log4j.properties 中的一个有用片段(如果您使用此配置,请记住执行 mk /tmp/logs)

log4j.rootLogger=debug, root
log4j.appender.root=org.apache.log4j.FileAppender
log4j.appender.root.layout = org.apache.log4j.PatternLayout
log4j.appender.root.layout.conversionPattern = %d [%t] %-5p %c - %m%n
log4j.appender.root.file = /tmp/logs/root.log
log4j.appender.root.append = true

log4j.category.mside = DEBUG,msideAppender
log4j.category.javashare = DEBUG,msideAppender

log4j.additivity.mside = false
log4j.additivity.mside.msideAppender = false
log4j.additivity.javashare = false

#Define msideAppender.
log4j.appender.msideAppender = org.apache.log4j.RollingFileAppender
log4j.appender.msideAppender.MaxFileSize=10MB
log4j.appender.msideAppender.MaxBackupIndex=7
log4j.appender.msideAppender.file = /tmp/logs/mside.log
log4j.appender.msideAppender.layout = org.apache.log4j.PatternLayout
log4j.appender.msideAppender.layout.conversionPattern = %d [%t] %-5p %c - %m%n
log4j.appender.msideAppender.append = true

回答by luis.espinal

IIRC Tomcat v4/v5 sends standard output to the catalina.out file, so any log4j output using a console appender would go to that file as well. Not sure if this still the case with newer versions of Tomcat, though.

IIRC Tomcat v4/v5 将标准输出发送到 catalina.out 文件,因此任何使用控制台附加程序的 log4j 输出也将转到该文件。不过,不确定较新版本的 Tomcat 是否仍然如此。

回答by Futzilogik

Your log4j configuration will be picked up by log4j if you put it into the classpath of your web application, e.g. in WEB-INF/classes/. Make sure that your log4j.jar is in WEB-INF/lib.

如果您将 log4j 配置放入 Web 应用程序的类路径中,例如在 WEB-INF/classes/ 中,那么您的 log4j 配置将被 log4j 获取。确保您的 log4j.jar 在 WEB-INF/lib 中。

The output of the ConsoleAppender that you defined, which is logging on stdout, will go to ${CATALINA_BASE}/logs/catalina.out, as any Tomcat stdout output.

您定义的 ConsoleAppender 的输出(记录在标准输出上)将作为任何 Tomcat 标准输出输出到 ${CATALINA_BASE}/logs/catalina.out。

As to the RollingFileAppender, you should define the correct path. If you want your web application's logs to appear in Tomcat's logs directory, change the file for this appender to:

至于 RollingFileAppender,你应该定义正确的路径。如果您希望 Web 应用程序的日志出现在 Tomcat 的日志目录中,请将此 appender 的文件更改为:

log4j.appender.file.File=${catalina.base}/logs/test.log

回答by luis.espinal

BTW, use the system property -Dlog4j.debug system property. That shouldtell you where the heck log4j is sending its output.

顺便说一句,使用系统属性 -Dlog4j.debug 系统属性。这应该会告诉您 log4j 将其输出发送到何处。

Also, if your tomcat install is in a *nix system, or if you are running on Windows with cygwin installed, you could use the findcommand to detect what files get changed right after you send a HTTP request to Tomcat (which you know should produce a logging output)

另外,如果你的tomcat安装在* nix的系统,或者如果您在Windows上使用Cygwin安装在运行,你可以使用查找命令来检测一下文件得到改变你发送一个HTTP请求到Tomcat(你知道应该之后产生日志输出)

cd <your tomcat install>    
ls -ltr `find . -type f -ls` | tail -10

That should show you the last 10 files that were updated or changed. It won't work if there are files in your app with spaces in their file names, though.

这应该会显示最近更新或更改的 10 个文件。但是,如果您的应用程序中有文件名中包含空格的文件,则它将不起作用。

回答by user3391471

I have tried following way and make sure it works well:

我尝试了以下方法并确保它运行良好:

  1. put your own log4j.properties place at a path;
  2. then update your catalina.sh under tomcat, add below similar line:

    JAVA_OPTS="-Dlog4j.configuration=file:${yourownlog4jpath}"

  1. 把你自己的 log4j.properties 放在一个路径上;
  2. 然后在 tomcat 下更新你的 catalina.sh,在类似的行下面添加:

    JAVA_OPTS="-Dlog4j.configuration=file:${yourownlog4jpath}"