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

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

STDOUT and STDERR can be automatically registered through the Apache Tomcat properties through the Logging → Redirect Stdout and Redirect Stderror tab.

But I want to control this through log4j.

I am trying to use ConsoleAppender and the TimeAndSizeRollingAppender class for rollover, which is usually controlled by the native Apache Tomcat protocol.

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

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

  • How to compile any of these classes? What should be the class path? I tried to compile the class from the sysgears link, but it returned 7 errors, such as the inability to find the Class Logger character and the Class OutputStream character.

  • After compiling, especially from the sysgears link, how can I use the class? He states that he should use the class, just write:

    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?

I would be grateful for any help.

Thanks so much for your time.

Update:

Here is my complete log4j.properties file:

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 
+8
java tomcat stdout stderr log4j
source share
2 answers

1) How to compile any of these classes? What should be the class path? I tried to compile the class from the sysgears link, but it returned 7 errors, such as the inability to find the Class Logger character and the Class OutputStream character.

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

 import java.io.PrintStream; import java.io.OutputStream; import org.apache.log4j.Logger; 
+3
source share

you can achieve this with the application:

 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 registrar, for example:

 log4j.rootLogger = DEBUG, stdout 

Of course, you can use Target above another, possibly with System.err

Hope this helps.

Cheers, Christian

+3
source share

All Articles