简体   繁体   English

作为 Windows 服务运行时的 Tomcat 日志记录

[英]Tomcat Logging while running as a windows service

I have a web application running under Tomcat.我有一个在 Tomcat 下运行的 Web 应用程序。

Logging for various features is implemented using java.util.logging.各种功能的日志记录是使用 java.util.logging 实现的。

When running tomcat via startup.bat the logging files are created and record the logging as expected.当通过 startup.bat 运行 tomcat 时,会创建日志文件并按预期记录日志。

When running as a windows service using the same war file the output from logging is virtually absent.当使用相同的 war 文件作为 Windows 服务运行时,日志记录的输出几乎不存在。 There is some ( Barely any ) log output in stdout.log and stderr.log but not what is expected. stdout.log 和 stderr.log 中有一些(几乎没有)日志输出,但不是预期的。

Does this sound familiar to anyone?这对任何人来说听起来都很熟悉吗? what steps did you take to correct it?你采取了什么步骤来纠正它?

Regards问候

stdout and stderr log files should have been created in the logs directory of your Tomcat installation. stdout 和 stderr 日志文件应该已在 Tomcat 安装的日志目录中创建。

To configure logging use the "Configure Tomcat" application.要配置日志记录,请使用“配置 Tomcat”应用程序。 You can set the error level and the location of the log files there.您可以在那里设置错误级别和日志文件的位置。

When tomcat is started as a service, its default logging configuration actually overrides any application-specific logging configuration.当 tomcat 作为服务启动时,它的默认日志配置实际上会覆盖任何特定于应用程序的日志配置。 As Aaron suggested the best way to set the configuration is using tomcatw //ES//tomcat_service_name, eg tomcat_service_name could be Tomcat5 depending on your installation preferences (also, use tomcatw, not tomcat - both executables can be found in C:\\Program Files\\Apache Software Foundation\\Tomcat 5.0\\bin).由于 Aaron 建议设置配置的最佳方法是使用 tomcatw //ES//tomcat_service_name,例如,根据您的安装首选项,tomcat_service_name 可以是 Tomcat5(另外,使用 tomcatw,而不是 tomcat - 两个可执行文件都可以在 C:\\Program Files 中找到\\Apache 软件基金会\\Tomcat 5.0\\bin)。 This post explains how you can ensure that you use the correct service name (if there are no values in the dialog then the name is probably incorrect).这篇文章解释了如何确保使用正确的服务名称(如果对话框中没有值,则该名称可能不正确)。

I would post an image but I do not have sufficient rep.我会发布一张图片,但我没有足够的代表。 On the Java VM tab, enter the configuration in Java Options, eg -Dlog4j.configuration=service.log4j.properties在 Java VM 选项卡上,在 Java Options 中输入配置,例如 -Dlog4j.configuration=service.log4j.properties

The file that contains your logging configuration can then be placed in tomcat server classes directory, eg C:\\Program Files\\Apache Software Foundation\\Tomcat 5.0\\server\\classes.然后可以将包含日志配置的文件放在 tomcat 服务器类目录中,例如 C:\\Program Files\\Apache Software Foundation\\Tomcat 5.0\\server\\classes。

There are several places where you can put the logging configuration file.有几个地方可以放置日志配置文件。 I initially tried specifying a full path for the logging configuration file but log4j appears to check only certain paths.我最初尝试为日志记录配置文件指定完整路径,但 log4j 似乎只检查某些路径。 As this post states, place -Dlog4j.debug as the first line in Java Options and you can debug what log4j is doing.正如这篇文章所述,将 -Dlog4j.debug 作为 Java 选项中的第一行,您可以调试 log4j 正在做什么。 The server classes directory is listed as one possible search path.服务器类目录被列为一种可能的搜索路径。

When creating your log4j.properties file, name it something other than log4j.properties, eg service.log4j.properties.创建 log4j.properties 文件时,将其命名为 log4j.properties 以外的其他名称,例如 service.log4j.properties。 Naming it log4j.properties runs the risk that an application-specific log4j.properties file could be found, which would override your configuration but, which would then be subsequently ignored by the tomcat service.将其命名为 log4j.properties 存在可能会找到特定于应用程序的 log4j.properties 文件的风险,该文件会覆盖您的配置,但随后将被 tomcat 服务忽略。

Make sure that the Tomcat environment is the same in both cases.确保 Tomcat 环境在两种情况下都相同。 Check the paths and settings in the Tomcat service editor.检查 Tomcat 服务编辑器中的路径和设置。 Don't use the standard Windows properties dialog for services but start tomcat.exe with //ES//name where name is the name of the service.不要对服务使用标准的 Windows 属性对话框,而是使用//ES//name启动 tomcat.exe,其中name是服务的名称。

As it turns out this is a configuration problem.事实证明这是一个配置问题。

When started from the startup.bat tomcat will reference the logging.properties in the conf directory.当从 startup.bat 启动时,tomcat 将引用 conf 目录中的 logging.properties。

When started as a service the jdk/jre/lib logging.properties comes into play.当作为服务启动时, jdk/jre/lib logging.properties 开始发挥作用。

When running as a windows service, all console output is redirected to stdout.log in the tomcat logs directory .当作为 Windows 服务运行时,所有控制台输出都被重定向到 tomcat 日志目录中的 stdout.log。

Attempts to capture logging in custom files with custom logging.properties files have so far failed.迄今为止,尝试使用自定义 logging.properties 文件捕获自定义文件中的日志记录失败。

This is probably a permissions issue when using Tomcat 9 on windows.这可能是在 Windows 上使用 Tomcat 9 时的权限问题。

The service.bat install tool from the bin folder of tomcat is registering tomcat as a local service account. tomcat 的bin 文件夹中的service.bat install工具正在将tomcat 注册为本地服务帐户。

This account type doesn't have enough permissions (among which writing its log files and compiling jsps).这个账户类型没有足够的权限(其中写日志文件和编译jsps)。 To fix this you can change the service account type and use a local user account to run the service.要解决此问题,您可以更改服务帐户类型并使用本地用户帐户运行该服务。

Alternatively the account can be changed to a local system acocunt (which used to be the case for the service installer), but due to security reasons, this is no longer recommended.或者,可以将帐户更改为本地系统帐户(服务安装程序曾经是这种情况),但出于安全原因,不再推荐这样做。

See also Tomcat Service gets installed with “Local Service” account另请参阅使用“本地服务”帐户安装 Tomcat 服务

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM