स्रोत: http://sysgears.com/articles/how-to-redirect-stdout-and-stderr-writing-to-a-log4j-appender/
Blockquote
log4j बॉक्स से बाहर stdout और stderr संदेशों को पकड़ने के लिए अनुमति नहीं है। हालांकि, यदि आप तृतीय पक्ष घटकों का उपयोग कर रहे हैं और उन्हें लॉग ऑन करना है जो वे स्ट्रीम में फ़्लश करते हैं, तो आप लॉगिंग का समर्थन करने वाले कस्टम आउटपुट स्ट्रीम को थोड़ा सा चाल और कार्यान्वित कर सकते हैं।
यह जिम मूर द्वारा पहले ही किया जा चुका है (लॉग 4j स्रोत कोड में लॉगिंगऑटपुटस्ट्रीम देखें)। एकमात्र मुद्दा यह है कि जिममूर के लॉगिंगऑटपुटस्ट्रीम को org.apache.log4j.Category और org.apache.log4j की आवश्यकता होती है। प्राथमिकता जिसे अब आंशिक रूप से बहिष्कृत किया गया है।
यहाँ संशोधित किया गया है LoggingOutputStream बहिष्कृत तरीकों से बचा जाता है:
public class LoggingOutputStream extends OutputStream {
/**
* Default number of bytes in the buffer.
*/
private static final int DEFAULT_BUFFER_LENGTH = 2048;
/**
* Indicates stream state.
*/
private boolean hasBeenClosed = false;
/**
* Internal buffer where data is stored.
*/
private byte[] buf;
/**
* The number of valid bytes in the buffer.
*/
private int count;
/**
* Remembers the size of the buffer.
*/
private int curBufLength;
/**
* The logger to write to.
*/
private Logger log;
/**
* The log level.
*/
private Level level;
/**
* Creates the Logging instance to flush to the given logger.
*
* @param log the Logger to write to
* @param level the log level
* @throws IllegalArgumentException in case if one of arguments
* is null.
*/
public LoggingOutputStream(final Logger log,
final Level level)
throws IllegalArgumentException {
if (log == null || level == null) {
throw new IllegalArgumentException(
"Logger or log level must be not null");
}
this.log = log;
this.level = level;
curBufLength = DEFAULT_BUFFER_LENGTH;
buf = new byte[curBufLength];
count = 0;
}
/**
* Writes the specified byte to this output stream.
*
* @param b the byte to write
* @throws IOException if an I/O error occurs.
*/
public void write(final int b) throws IOException {
if (hasBeenClosed) {
throw new IOException("The stream has been closed.");
}
// don't log nulls
if (b == 0) {
return;
}
// would this be writing past the buffer?
if (count == curBufLength) {
// grow the buffer
final int newBufLength = curBufLength +
DEFAULT_BUFFER_LENGTH;
final byte[] newBuf = new byte[newBufLength];
System.arraycopy(buf, 0, newBuf, 0, curBufLength);
buf = newBuf;
curBufLength = newBufLength;
}
buf[count] = (byte) b;
count++;
}
/**
* Flushes this output stream and forces any buffered output
* bytes to be written out.
*/
public void flush() {
if (count == 0) {
return;
}
final byte[] bytes = new byte[count];
System.arraycopy(buf, 0, bytes, 0, count);
String str = new String(bytes);
log.log(level, str);
count = 0;
}
/**
* Closes this output stream and releases any system resources
* associated with this stream.
*/
public void close() {
flush();
hasBeenClosed = true;
}
}
अब आप उन संदेशों को stderr को प्लावित या निम्नलिखित तरीके से stdout रहे हैं पकड़ कर सकते हैं:
System.setErr(new PrintStream(new LoggingOutputStream(
Logger.getLogger("outLog"), Level.ERROR)));
log4j.properties विन्यास:
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
दिमित्री पावलेंको,,210 SysGears
Blockquote
तुम सिर्फ 'सौंपने लिखने आपका अपना OutputStream को लागू नहीं किया जा सका (...) लॉगर के लिए? – Rekin
@ रीकिन बिल्कुल मैंने क्या किया! –
Log4J कहने से आपका क्या मतलब है System.err? –