简体   繁体   中英

Need help with running a customized class/jar for log4j in Tomcat5 on Windows (Java, NetBeans)

I am trying to implement a custom class written for log4j and use it in conjunction with Tomcat5 on Windows. Now, please keep in mind that tomcat5 and log4j both are installed and configured properly and work fine. However, adding this class does not produce the expected results, which is the point of this post. I discovered this class from the following link: http://sysgears.com/articles/how-to-redirect-stdout-and-stderr-writing-to-a-log4j-appender#comment-749 ).

I wrote the class and put the following above the class declaration:

package LoggingOutputStream;

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

Then, I compiled it successfully. The link then states to use the class, write:

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

Here's what I wrote:

import java.io.PrintStream;
import LoggingOutputStream.LoggingOutputStream;
import org.apache.log4j.Logger;
import org.apache.log4j.Level;

public class UseLoggingOutputStream {

  public void SetErr() {
    System.setErr(new PrintStream(new LoggingOutputStream(Logger.getLogger("outLog"), Level.ERROR)));
  }
}

An important detail is the LoggingOutPutstream constructor takes 2 inputs, yet the author from the link called it using 3 inputs, which produces an error. I removed the "System.err" parameter and the file compiled successfully.

I then jar'd the two classes and copied the jar file to the tomcat5/common/lib folder. Lastly, I modified the log4j.properties file with what was specified from the link:

log4j.logger.outLog=error, out_log

log4j.appender.out_log=org.apache.log4j.RollingFileAppender
log4j.appender.out_log.file=/logs/error.log
log4j.appender.out_log.MaxFileSize=10MB
log4j.appender.out_log.threshold=error

I started the Apache Tomcat5 service and I did not see an error.log in the logs folder. Am I missing something? Has anyone else gotten this to work successfully?

I would sincerely appreciate any advice. Thank you.

A little outdated but could still be helpful for some others :)

I had this problem as well and couldn't seem to figure out why it wasn't writing anything at all. I even tried to write directly to the console as well but there wasn't any output. In the end I looked up the PrintStream.class and found that you had to pass a boolean for autoflushing the bytes like this:

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

This got it working for me. Hope it helps!

Problem: you are using an absolute path /logs/error.log

You need to locate your Tomcat directory. As simple as follow: log4j.appender.out_log.file=${catalina.home}/logs/error.log

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM