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:
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.
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
you can achieve this with an appender:
Then use this appender with your logger, for example:
Of course you can use the Target above different, maybe with System.err
Hope it helps.
Cheers, Christian
You need to have the log4j jar on your classpath, and import the correct classes at the top of your file. Something like,