您如何使用 Log4j 将 stdout 和 stderr 写入/捕获到文件并使用 Windows 和 Tomcat 5.5 (Java)?

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

How do you use Log4j to write/capture stdout and stderr to a file and using Windows and Tomcat 5.5 (Java)?

javatomcatlog4jstdoutstderr

提问by user717236

I am using Windows 2008 R2 and Apache Tomcat 5.5, for your information.

我正在使用 Windows 2008 R2 和 Apache Tomcat 5.5,供您参考。

STDOUT and STDERR can be automatically logged through Apache Tomcat properties, via Logging tab -> Redirect Stdout and Redirect Stderror textboxes.

STDOUT 和 STDERR 可以通过 Apache Tomcat 属性自动记录,通过 Logging 选项卡 -> Redirect Stdout 和 Redirect Stderror 文本框。

But I want to control this through log4j.

但是我想通过log4j来控制这个。

I'm trying to leverage ConsoleAppender and the TimeAndSizeRollingAppender classto rollover what would normally be controlled by Apache Tomcat's innate logging.

我正在尝试利用 ConsoleAppender 和TimeAndSizeRollingAppender 类来翻转通常由 Apache Tomcat 的固有日志记录控制的内容。

Basically, however Tomcat redirects stdout and stderr to a file, I want to do the same thing using log4j and the log4j.propertiesfile.

基本上,但是 Tomcat 将 stdout 和 stderr 重定向到一个文件,我想使用 log4j 和log4j.properties文件做同样的事情。

I did some digging on this site and others like this one ( log4j redirect stdout to DailyRollingFileAppender) and this one ( http://sysgears.com/articles/how-to-redirect-stdout-and-stderr-writing-to-a-log4j-appender#comment-749), but I am running into a few fundamental issues:

我在这个网站和其他网站上做了一些挖掘(log4j 将标准输出重定向到 DailyRollingFileAppender)和这个网站(http://sysgears.com/articles/how-to-redirect-stdout-and-stderr-writing-to-a -log4j-appender#comment-749),但我遇到了一些基本问题:

  1. How do I compile any of these classes? What should be the classpath? I tried compiling the class from the sysgears link but it returned 7 errors such as unable to find symbol Class Logger and symbol Class OutputStream.

  2. After compiling, specifically from the sysgears link, how can I use the class? It states to use the class, just write:

    System.setErr(new PrintStream(new LoggingOutputStream( System.err, Logger.getLogger("outLog"), Level.ERROR)));

  1. 我如何编译这些类中的任何一个?类路径应该是什么?我尝试从 sysgears 链接编译该类,但它返回了 7 个错误,例如无法找到符号类记录器和符号类输出流。

  2. 编译后,特别是从 sysgears 链接,我如何使用该类?它声明要使用该类,只需编写:

    System.setErr(new PrintStream(new LoggingOutputStream(System.err, Logger.getLogger("outLog"), Level.ERROR)));

Great, but where do I write it? In a separate file? Tomcat Properties?

很好,但我在哪里写呢?在单独的文件中?Tomcat 属性?

I would appreciate any help.

我将不胜感激任何帮助。

Thank you very much for your time.

非常感谢您的宝贵时间。

Update:

更新:

Here is my full log4j.properties file:

这是我的完整 log4j.properties 文件:

log4j.rootLogger=INFO, CATALINA, LOCALHOST, MANAGER, HOST-MANAGER, ADMIN, CONSOLE

# Define all the appenders
log4j.appender.CATALINA=org.apache.log4j.appender.TimeAndSizeRollingAppender
log4j.appender.CATALINA.file=D:/Program Files (x86)/Apache Software Foundation/Tomcat-      5.5.28/logs/catalina.log
log4j.appender.CATALINA.Threshold=DEBUG
log4j.appender.CATALINA.DatePattern='.'yyyy-MM-dd-HH-mm
log4j.appender.CATALINA.layout=org.apache.log4j.PatternLayout
log4j.appender.CATALINA.MaxFileSize=20KB
log4j.appender.CATALINA.DateRollEnforced=true
log4j.appender.CATALINA.MaxRollFileCount=100
log4j.appender.CATALINA.ScavengeInterval=-1
log4j.appender.CATALINA.BufferedIO=false
log4j.appender.CATALINA.CompressionAlgorithm=ZIP
log4j.appender.CATALINA.layout.conversionPattern = %-5p %-23d{ISO8601} [%t] %x: %c{1} - %m%n

log4j.appender.LOCALHOST=org.apache.log4j.appender.TimeAndSizeRollingAppender
log4j.appender.LOCALHOST.file=D:/Program Files (x86)/Apache Software Foundation/Tomcat-5.5.28/logs/localhost.log
log4j.appender.LOCALHOST.Threshold=DEBUG
log4j.appender.LOCALHOST.DatePattern='.'yyyy-MM-dd-HH-mm
log4j.appender.LOCALHOST.layout=org.apache.log4j.PatternLayout
log4j.appender.LOCALHOST.MaxFileSize=20KB
log4j.appender.LOCALHOST.DateRollEnforced=true
log4j.appender.LOCALHOST.MaxRollFileCount=100
log4j.appender.LOCALHOST.ScavengeInterval=-1
log4j.appender.LOCALHOST.BufferedIO=false
log4j.appender.LOCALHOST.CompressionAlgorithm=ZIP
log4j.appender.LOCALHOST.layout.conversionPattern = %-5p %-23d{ISO8601} [%t] %x: %c{1} - %m%n

log4j.appender.MANAGER=org.apache.log4j.appender.TimeAndSizeRollingAppender
log4j.appender.MANAGER.file=D:/Program Files (x86)/Apache Software Foundation/Tomcat-5.5.28/logs/manager.log
log4j.appender.MANAGER.Threshold=DEBUG
log4j.appender.MANAGER.DatePattern='.'yyyy-MM-dd-HH-mm
log4j.appender.MANAGER.layout=org.apache.log4j.PatternLayout
log4j.appender.MANAGER.MaxFileSize=20KB
log4j.appender.MANAGER.DateRollEnforced=true
log4j.appender.MANAGER.MaxRollFileCount=100
log4j.appender.MANAGER.ScavengeInterval=-1
log4j.appender.MANAGER.BufferedIO=false
log4j.appender.MANAGER.CompressionAlgorithm=ZIP
log4j.appender.MANAGER.layout.conversionPattern = %-5p %-23d{ISO8601} [%t] %x: %c{1} - %m%n

log4j.appender.HOST-MANAGER=org.apache.log4j.appender.TimeAndSizeRollingAppender
log4j.appender.HOST-MANAGER.file=D:/Program Files (x86)/Apache Software Foundation/Tomcat-5.5.28/logs/host-manager.log
log4j.appender.HOST-MANAGER.Threshold=DEBUG
log4j.appender.HOST-MANAGER.DatePattern='.'yyyy-MM-dd-HH-mm
log4j.appender.HOST-MANAGER.layout=org.apache.log4j.PatternLayout
log4j.appender.HOST-MANAGER.MaxFileSize=20KB
log4j.appender.HOST-MANAGER.DateRollEnforced=true
log4j.appender.HOST-MANAGER.MaxRollFileCount=100
log4j.appender.HOST-MANAGER.ScavengeInterval=-1
log4j.appender.HOST-MANAGER.BufferedIO=false
log4j.appender.HOST-MANAGER.CompressionAlgorithm=ZIP
log4j.appender.HOST-MANAGER.layout.conversionPattern = %-5p %-23d{ISO8601} [%t] %x: %c{1} - %m%n

log4j.appender.ADMIN=org.apache.log4j.appender.TimeAndSizeRollingAppender
log4j.appender.ADMIN.file=D:/Program Files (x86)/Apache Software Foundation/Tomcat-5.5.28/logs/admin.log
log4j.appender.ADMIN.Threshold=DEBUG
log4j.appender.ADMIN.DatePattern='.'yyyy-MM-dd-HH-mm
log4j.appender.ADMIN.layout=org.apache.log4j.PatternLayout
log4j.appender.ADMIN.MaxFileSize=20KB
log4j.appender.ADMIN.DateRollEnforced=true
log4j.appender.ADMIN.MaxRollFileCount=100
log4j.appender.ADMIN.ScavengeInterval=-1
log4j.appender.ADMIN.BufferedIO=false
log4j.appender.ADMIN.CompressionAlgorithm=ZIP
log4j.appender.ADMIN.layout.conversionPattern = %-5p %-23d{ISO8601} [%t] %x: %c{1} - %m%n

# Configure which loggers log to which appenders
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost]=INFO, LOCALHOST
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager]=\
INFO, MANAGER
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager]=\
INFO, HOST-MANAGER
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/admin]=\
INFO, ADMIN

回答by sbridges

1) How do I compile any of these classes? What should be the classpath? I tried compiling the class from the sysgears link but it returned 7 errors such as unable to find symbol Class Logger and symbol Class OutputStream.

1) 如何编译这些类中的任何一个?类路径应该是什么?我尝试从 sysgears 链接编译该类,但它返回了 7 个错误,例如无法找到符号类记录器和符号类输出流。

You need to have the log4j jar on your classpath, and import the correct classes at the top of your file. Something like,

您需要在类路径上安装 log4j jar,并在文件顶部导入正确的类。就像是,

import java.io.PrintStream;
import java.io.OutputStream;
import org.apache.log4j.Logger;

回答by Christian

you can achieve this with an appender:

您可以使用附加程序实现此目的:

log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Threshold = DEBUG
log4j.appender.stdout.Target   = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = %d{ISO8601} %-5p [%F:%L] : %m%n

Then use this appender with your logger, for example:

然后将此 appender 与您的记录器一起使用,例如:

log4j.rootLogger = DEBUG, stdout

Of course you can use the Target above different, maybe with System.err

当然你可以用上面不同的Target,也许用System.err

Hope it helps.

希望能帮助到你。

Cheers, Christian

干杯,基督徒