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, whic开发者_如何转开发h 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.
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
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!
精彩评论