Java 如何配置 Tomcat JULI 日志记录来滚动日志文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/136300/
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-08-11 08:43:21  来源:igfitidea点击:

How to configure Tomcat JULI logging to roll log files?

javatomcatlogginglog4j

提问by

I have a several webapps which use java.util.logging. Tomcat 5.5 is configured to use the Juli logger so that each webapp has its own log file. The problem is that Juli does not have properties for maximum file size and file count. Using Juli the files will grow unbounded and only roll at the end of the day. Also, an unlimited number of log files are retained.

我有几个使用 java.util.logging 的 webapp。Tomcat 5.5 配置为使用 Juli 记录器,以便每个 web 应用程序都有自己的日志文件。问题是 Juli 没有最大文件大小和文件数的属性。使用 Juli 文件将无限增长,并且只会在一天结束时滚动。此外,保留无限数量的日志文件。

You can see the FileHandler properties on this page - Apache Tomcat 5.5 Documentation
There is no limit or count property (the following lines do nothing)
org.apache.juli.FileHandler.limit=102400
org.apache.juli.FileHandler.count=5

您可以在此页面上看到 FileHandler 属性 - Apache Tomcat 5.5 文档
没有限制或计数属性(以下几行什么都不做)
org.apache.juli.FileHandler.limit=102400
org.apache.juli.FileHandler.count=5

Without changing the webapps is there a way to get unique log files for each application with some type of bounds on the log file sizes?

在不更改 webapps 的情况下,有没有办法为每个应用程序获取唯一的日志文件,并对日志文件大小有某种类型的限制?

UPDATE:The solution I found was not use the Juli logger at all! java.util.logging.FileHandler.limit=102400
java.util.logging.FileHandler.count=5

更新:我发现的解决方案根本不使用 Juli 记录器! java.util.logging.FileHandler.limit=102400
java.util.logging.FileHandler.count=5

Thanks,

谢谢,

Greg

格雷格

回答by Matt

Update:I see your point now after reading more. "Tomcat's JULI implementation is not intended to be a fully-featured logging libary, only a simple bridge to those libraries. However, JULI does provide several properties for configuring the its handlers. These are listed below." Funny that they say that the default java.util.Logging implementation is too limited then they work around it by providing an even more limiting implementation.

更新:阅读更多内容后,我现在明白您的观点。“Tomcat 的 JULI 实现并不打算成为一个功能齐全的日志库,只是一个连接这些库的简单桥梁。但是,JULI 确实提供了几个用于配置其处理程序的属性。这些属性如下所列。” 有趣的是,他们说默认的 java.util.Logging 实现太有限,然后他们通过提供更有限的实现来解决它。

FileHandler javadocs

文件处理器 javadocs

  • java.util.logging.FileHandler.limit specifies an approximate maximum amount to write (in bytes) to any one file. If this is zero, then there is no limit. (Defaults to no limit).
  • java.util.logging.FileHandler.count specifies how many output files to cycle through (defaults to 1).
  • java.util.logging.FileHandler.limit 指定写入任何一个文件的近似最大数量(以字节为单位)。如果这是零,则没有限制。(默认为无限制)。
  • java.util.logging.FileHandler.count 指定要循环的输出文件数量(默认为 1)。

for the one file per web app, you probably want to separate it by the name of the logger and it depends on how the loggers are created for each app. If they're based off the package or class name then you can filter the logs based on that. It looks like the sample on the link you provided tells how to do this

对于每个 Web 应用程序一个文件,您可能希望通过记录器的名称将其分开,这取决于为每个应用程序创建记录器的方式。如果它们基于包或类名,那么您可以基于此过滤日志。看起来您提供的链接上的示例告诉了如何执行此操作

org.apache.catalina.core.ContainerBase.[Catalina].[localhost].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].handlers = \
   2localhost.org.apache.juli.FileHandler

回答by Jonas Klemming

EDIT: Just realized you wanted per webapp logging which might not be possible with this setup...

编辑:刚刚意识到您想要每个 webapp 日志记录,而此设置可能无法实现...

My suggestion, assuming your are using Tomcat 6.0, is to compile the extra component for full commons-logging and use Log4j to configure rolling logs.

假设您使用的是 Tomcat 6.0,我的建议是编译额外的组件以获得完整的 commons-logging 并使用 Log4j 来配置滚动日志。

Building instructions here http://tomcat.apache.org/tomcat-6.0-doc/building.html

此处构建说明 http://tomcat.apache.org/tomcat-6.0-doc/building.html

Then replace the tomcat-juli.jar in the /bin directory of Tomcat and place the tomcat-juli-adapters.jar in the /lib directory along with log4j.jar and log4j.properties.

然后替换Tomcat /bin目录下的tomcat-juli.jar,将tomcat-juli-adapters.jar和log4j.jar、log4j.properties放在/lib目录下。

Then using something like:

然后使用类似的东西:

<appender name="file" class="org.apache.log4j.RollingFileAppender">
  <param name="File" value="logfile.log"/>
  <param name="Threshold" value="INFO"/>
  <param name="MaxFileSize" value="10MB"/>
  <param name="MaxBackupIndex" value="3"/>
  <layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern" value="%d{ISO8601} %p %m%n"/>
  </layout>
</appender>