8

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

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

But I want to control this through log4j.

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

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

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:

  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)));

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

I would appreciate any help.

Thank you very much for your time.

Update:

Here is my full 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
Community
  • 1
  • 1
user717236
  • 4,959
  • 19
  • 66
  • 102
  • Just to validate my understanding of the question, you have already configured Log4J with Tomcat as stated in the documentation (http://tomcat.apache.org/tomcat-5.5-doc/logging.html). It is just that you are not getting STDOUT and STDERR in the logs, right? – Edwin Dalorzo Apr 20 '11 at 14:08
  • Yes, that is correct. I posted my full log4j.properties file in my original post. I just want to log STDOUT and STDERR to log files using log4j, however that can be done. Thank you. – user717236 Apr 20 '11 at 14:34

2 Answers2

3

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.

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

import java.io.PrintStream;
import java.io.OutputStream;
import org.apache.log4j.Logger;
sbridges
  • 24,960
  • 4
  • 64
  • 71
  • Okay, thank you. I did as you said, imported those classes at the top of the file and specified the classpath directly. Here's what I typed (note, the command line prompt is relative to the folder for the classpath) --> "javac -cp log4j-1.2.16.jar LoggingOutputStream.java" (without quotes). I got 4 errors, all of which point to cannot find symbol Class Level and Class IOException. My full import looks like this: import java.io.PrintStream; import java.io.OutputStream; import java.io.IOException;import java.util.logging.Level; import java.util.logging.Logger; – user717236 Apr 20 '11 at 15:18
  • Find all the missing imports, and import them. IOException is java.io.IOException, Level is org.apache.log4j.Level – sbridges Apr 20 '11 at 15:23
  • Thank you, I found the missing imports and it compiled successfully. You're excellent, thank you. The only other tidbit that I'm lost on is how to use the class (see my edited original post above), in conjunction with the log4j.properties file. In the sysgears link, it mentions to write a couple of lines of code to use their class, but it doesn't state where to write it to? I don't know if they are implying to use Tomcat or write it a separate .java file and compile it, or something else. I have no idea. Any help would be appreciated, as always, thank you. – user717236 Apr 20 '11 at 15:40
  • 1
    @user717236: If you find his post helpful, at least have the courtesy to up vote it as I've done. – Hovercraft Full Of Eels Apr 24 '11 at 06:08
  • Up vote? I don't understand. I'm the one who posted the question and I found all the answers helpful, thank you. – user717236 Apr 25 '11 at 16:43
  • If you post a question, you can accept an answer. You can also upvote an answer once you get a reputation > 15. – sbridges Apr 26 '11 at 00:50
3

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:

log4j.rootLogger = DEBUG, stdout

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

Hope it helps.

Cheers, Christian

Christian
  • 7,062
  • 5
  • 36
  • 39
  • Hello, thank you for your help. Unfortunately, this doesn't provide the solution for which I'm looking. With ConsoleAppender, where does it save the file? I'd like to use rollover on stdout and stderr to control its size and backups. I'm getting stuck on the sysgear class, insomuch how to use it in conjunction with log4j.properties. Thank you. – user717236 Apr 21 '11 at 13:09