记录 Java Web 应用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/614666/
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
Logging Java web applications?
提问by James McMahon
I am planning to implement logging into a web application that I am currently working on but I am struggling with some of the details. What is the best way to go about logging a Java web application?
我计划实现登录我目前正在开发的 Web 应用程序,但我正在努力解决一些细节问题。记录 Java Web 应用程序的最佳方法是什么?
Specifically;
具体来说;
- Where does the configuration file go in .war package file?
- Where do people log to, relative or absolute path flat file, or a database?
- Does Log4J logging go directly into the application server log file automatically or is that something you have to set up? In this case I am using Tomcat, but I often use Jrun.
- Any other gotchas I should be aware of for web application logging?
- .war 包文件中的配置文件在哪里?
- 人们在哪里登录,相对或绝对路径平面文件,或数据库?
- Log4J 日志记录是自动直接进入应用程序服务器日志文件还是您必须设置?在这种情况下,我使用的是 Tomcat,但我经常使用 Jrun。
- 我应该注意 Web 应用程序日志记录的任何其他问题?
Currently I am using Log4J but I imagine that the best practices would apply universally to all logging implementations.
目前我正在使用 Log4J,但我想最佳实践将普遍适用于所有日志记录实现。
EDIT:
One addition to the questions up top.
编辑:
对上面问题的补充。
- Where do you initilize the log configuration?
- 你在哪里初始化日志配置?
In a traditional app, I do this at the entry point;
在传统应用程序中,我在入口点执行此操作;
DOMConfigurator.configureAndWatch("log4j.xml");
What would the web application equivalent be?
Web 应用程序的等价物是什么?
采纳答案by cherouvim
I place my configuration on the default package: src/
我将我的配置放在默认包上:src/
and log to files using the ${catalina.home} system property:
并使用 ${catalina.home} 系统属性登录到文件:
log4j.appender.???.file=${catalina.home}/logs/system.log
回答by Andrey Vityuk
I would recommend you to use SLF4J. This is simple logging facade which supports most of popular logging systems (Log4j, commons-logging, Java Logging APIand Logback). Using it, you will able to replace your underline logging systemto any other, by simple CLASSPATH update.
我建议您使用SLF4J。这是一个简单的日志外观,支持大多数流行的日志系统(Log4j、commons-logging、Java Logging API和Logback)。使用它,您将能够通过简单的 CLASSPATH 更新将您的下划线日志系统替换为任何其他系统。
The other benefit of SLF4Jare parameterized calls, which reduces ugly logging code.
SLF4J的另一个好处是参数化调用,它减少了丑陋的日志代码。
Actually, they recommends to use SLF4Jwith Logback. Logbackis a successor of Log4J. And it was designed by the same author.
实际上,他们建议将SLF4J与Logback一起使用。 Logback是Log4J的继承者。它是由同一作者设计的。
回答by Boris Pavlovi?
Where does the configuration file go in .war package file?Root of the classpath.
.war 包文件中的配置文件在哪里?类路径的根。
Where do people log to, relative or absolute path flat file, or a database?Depends on the need. It's always better to use relative paths. Databases are good if you implement another application which will fetch logs from it and send them using email/sms
人们在哪里登录,相对或绝对路径平面文件,或数据库?视需要而定。使用相对路径总是更好。如果您实现另一个应用程序,该应用程序将从数据库中获取日志并使用电子邮件/短信发送它们,则数据库很好
Does Log4J logging go directly into the application server log file automatically or is that something you have to set up? In this case I am using Tomcat, but I often use Jrun.If you use Console appender, yes, it's going to be logged in your servlet container log file.
Log4J 日志记录是自动直接进入应用程序服务器日志文件还是您必须设置?在这种情况下,我使用的是 Tomcat,但我经常使用 Jrun。如果您使用 Console appender,是的,它将被记录在您的 servlet 容器日志文件中。
Any other gotchas I should be aware of for web application logging?If you are logging from different threads use logback, it's thread-safe and it exposes parameterized log messages.
我应该注意 Web 应用程序日志记录的任何其他问题?如果您从不同的线程进行日志记录,请使用logback,它是线程安全的,并且会公开参数化的日志消息。
回答by James A. N. Stauffer
Logging to a DB adds another failure point. We had a situation where the prod servers logged to a DB and someone ran an expensive query on that DB that slowed it so much that the prod servers got very, very slow. Logging to a file can cause issues if you run out of space but it seems less likely to slow down the whole app.
记录到数据库会增加另一个故障点。我们遇到过这样的情况,prod 服务器登录到一个数据库,有人在该数据库上运行了一个昂贵的查询,这使它减慢了速度,以至于 prod 服务器变得非常非常慢。如果空间不足,记录到文件可能会导致问题,但似乎不太可能降低整个应用程序的速度。
回答by gustafc
It might be a good idea to place the config file somewhere where an admin can modify it without rebuilding your web app (e.g., so they can turn on detailed logging without waking you up in the middle of the night).
将配置文件放在管理员可以修改它而无需重建您的 Web 应用程序的某个地方可能是个好主意(例如,这样他们就可以打开详细的日志记录而不会在半夜叫醒您)。
Unfortunately, there's no "official" way for locating externalized resources from a web app (correct me if I'm wrong). The most common way of doing it I've seen is to look through the directories in the classpath.
不幸的是,没有“官方”的方法可以从 Web 应用程序中定位外部资源(如果我错了,请纠正我)。我见过的最常见的做法是查看类路径中的目录。
回答by grayger
I recommend to call log API (log4j) via slf4j. Even if you use log4j, web container or depending modules may use different log API such as Java.util.logging or Jakarta commons logging. Slf4j provides bridge modules that redirect them to slf4j API. As a result, all log messages are written by log4j in that case.
我建议通过 slf4j 调用日志 API (log4j)。即使您使用 log4j,Web 容器或依赖模块也可能使用不同的日志 API,例如 Java.util.logging 或 Jakarta commons logging。Slf4j 提供了桥接模块,将它们重定向到 slf4j API。因此,在这种情况下,所有日志消息都由 log4j 写入。
回答by Daniel
The excellent paper How to Do Application Logging Righthas a bunch of gotchas.
优秀的论文How to Do Application Logging Right有很多问题。
I believe that your other questions have been answered by other people on this page.
我相信您的其他问题已在此页面上得到其他人的回答。
I also recommend that you use SLF4J.
我还建议您使用 SLF4J。
One last thing: having speakable representations of objectscan save some time.
最后一件事:拥有可说的对象表示可以节省一些时间。
回答by Thorbj?rn Ravn Andersen
Note that if you just need a bit of logging, the servlet standard specifies that you can get the ServletContext and use the log methods there. That is the generic servlet equivalent of System.out.println.
请注意,如果您只需要一点日志记录,servlet 标准指定您可以获取 ServletContext 并在那里使用日志方法。这是 System.out.println 的通用 servlet 等价物。
回答by Pascal Thivent
- Where does the configuration file go in .war package file?
- .war 包文件中的配置文件在哪里?
At the root of the classpath but... Don't put the configuration file in the war package. You don't want to repackage and redeploy the application if you change the logging configuration, do you ? A better practice would be to put the configuration file somewhere in the classpath outside the war.
在classpath的根目录但是...不要把配置文件放在war包里。如果更改日志记录配置,您不想重新打包和重新部署应用程序,是吗?更好的做法是将配置文件放在War之外的类路径中。
- Where do people log to, relative or absolute path flat file, or a database?
- 人们在哪里登录,相对或绝对路径平面文件,或数据库?
I usually log to the file system on a separate partition(log files can grow very fast and should never block the application or the operating system if they become too big). I use most of time an absolute pathbased on the following model: /var/projects/<PROJECT_NAME>/<PRODUCT>/<CLUSTER_NAME>/logs/<INSTANCE_NAME>.log where <PROJECT_NAME> is the project name, <PRODUCT> can be Apache, Tomcat, Weblogic,..., <CLUSTER_NAME> the name of the cluster and <INSTANCE_NAME> the name of the instance inside the cluster. Logging to the file system is faster than in a database. The drawback is that logs aren't centralized if you are using several instances and physical machines. But merging can easily be done with a script.
我通常登录到单独分区上的文件系统(日志文件可以增长得非常快,如果它们变得太大,则永远不应该阻止应用程序或操作系统)。我大部分时间使用基于以下模型的绝对路径: /var/projects/<PROJECT_NAME>/<PRODUCT>/<CLUSTER_NAME>/logs/<INSTANCE_NAME>.log 其中 <PROJECT_NAME> 是项目名称,<PRODUCT>可以是 Apache、Tomcat、Weblogic 等,<CLUSTER_NAME> 是集群名称,<INSTANCE_NAME> 是集群内部实例的名称。记录到文件系统比在数据库中更快。缺点是如果您使用多个实例和物理机,日志不会集中。但是合并可以通过脚本轻松完成。
- Does Log4J logging go directly into the application server log file automatically or is that something you have to set up? In this case I am using Tomcat, but I often use Jrun.
- Log4J 日志记录是自动直接进入应用程序服务器日志文件还是您必须设置?在这种情况下,我使用的是 Tomcat,但我经常使用 Jrun。
Application server logs are application server logs, not application logs. Don't write to them but set up a logger tool (e.g. log4j) and write to application logs (understand dedicated).
应用服务器日志是应用服务器日志,而不是应用日志。不要写入它们,而是设置一个记录器工具(例如 log4j)并写入应用程序日志(了解专用)。
- Any other gotchas I should be aware of for web application logging?
- 我应该注意 Web 应用程序日志记录的任何其他问题?
If you are using log4j, don't forget to use the isDebugEnabled() before to log:
如果您使用的是 log4j,请不要忘记在登录之前使用 isDebugEnabled():
if(logger.isDebugEnabled()) {
logger.debug("Bla Bla Bla");
}
回答by david a.
- Where does the configuration file go in .war package file?
- .war 包文件中的配置文件在哪里?
Usually, I do not place any logging configuration into the application, rather leaving that to the appserver admins to configure logging server-wide. In the rare cases I want the log4j configuration deployed with a webapp, WEB-INF is the usual path.
通常,我不会在应用程序中放置任何日志记录配置,而是将其留给应用服务器管理员来配置服务器范围的日志记录。在极少数情况下,我希望使用 webapp 部署 log4j 配置,WEB-INF 是通常的路径。
- Where do people log to, relative or absolute path flat file, or a database?
- 人们在哪里登录,相对或绝对路径平面文件,或数据库?
Again, depends on appserver settings. One common log file for a appserver and rotating on a daily basis is the usual setup. If there are any app-specific needs, the admin may configure a separate logfile for an app (distinguished by package / class names).
同样,取决于应用程序服务器设置。一个用于应用服务器并每天轮换的常见日志文件是通常的设置。如果有任何特定于应用程序的需求,管理员可以为应用程序配置一个单独的日志文件(按包/类名区分)。
- Does Log4J logging go directly into the application server log file automatically or is that something you have to set up? In this case I am using Tomcat, but I often use Jrun.
- Log4J 日志记录是自动直接进入应用程序服务器日志文件还是您必须设置?在这种情况下,我使用的是 Tomcat,但我经常使用 Jrun。
See above. For tomcat used for development purposes, I'd just look for its logging (log4j) configuration and add app-specific specific there.
往上看。对于用于开发目的的 tomcat,我只是查找它的日志记录 (log4j) 配置并在那里添加特定于应用程序的内容。
- Any other gotchas I should be aware of for web application logging?
Performance. Limit the ,log level to a minimum (i.e. WARN or ERROR) once you go live. Use
if (log.isDebugEnabled()) { log.debug("..."); }and alike constructs in your code.
- 我应该注意 Web 应用程序日志记录的任何其他问题?表现。一旦上线,将日志级别限制到最低(即警告或错误)。
if (log.isDebugEnabled()) { log.debug("..."); }在您的代码中使用 和类似的结构。

