2010-01-22 5 views
8

javax.mail.Session setDebugOut को log4j लॉगर पर रीडायरेक्ट करने के लिए कैसे करें?javax.mail.Session setDebugOut को log4j लॉगर पर रीडायरेक्ट करने के लिए कैसे करें?

क्या केवल लॉगर को मेलसेशन डीबग आउट करना संभव है?

मेरा मतलब है, वहाँ

link text

जो reassigns सभी मानक उत्पादन log4j करने

--System.setOut जाना (नई Log4jStream())

बेस्ट सादर तरह समाधान कर रहे हैं

+1

बस इस पुरानी पोस्ट पर ठोकर खाई। प्रिंटस्ट्रीम डीबग विकल्प का उपयोग किए बिना अब जूल-> slf4j पुल 'java.util.logging.Logger.getLogger ("javax.mail") का उपयोग करने का विकल्प भी है। –

उत्तर

2

अपना खुद का आउटपुटस्ट्रीम क्लास

लिखें

और

mailSession.setDebugOut (नई PrintStream (धारा वस्तु aoutput अपने कस्टम));

LogOutputStream losStdOut = new LogOutputStream() {    
    @Override 
    protected void processLine(String line, int level) { 
     cat.debug(line); 
    } 
}; 

Session session = Session.getDefaultInstance(new Properties(), null); 
session.setDebugOut(new PrintStream(losStdOut)); 

बिल्ली जाहिर log4j श्रेणी appender है /:

10

Apache Commons Exec पुस्तकालय उपयोगी वर्ग LogOutputStream, जो आप बिल्कुल इसी उद्देश्य के लिए उपयोग कर सकते हैं।

3

मैं एक खुद filteroutputstream (आप भी SLF के बजाय org.apache.logging.Logger इस्तेमाल कर सकते हैं)

public class LogStream extends FilterOutputStream 
    { 
     private static org.slf4j.Logger    LOG = org.slf4j.LoggerFactory.getLogger(LogStream.class); 
     private static final OutputStream bos = new ByteArrayOutputStream(); 

     public LogStream(OutputStream out) 
      { 
       // initialize parent with my bytearray (which was never used) 
       super(bos); 
      } 

     @Override 
     public void flush() throws IOException 
      { 
       // this was never called in my test 
       bos.flush(); 
       if (bos.size() > 0) LOG.info(bos.toString()); 
       bos.reset(); 
      } 

     @Override 
     public void write(byte[] b) throws IOException 
      { 
       LOG.info(new String(b)); 
      } 

     @Override 
     public void write(byte[] b, int off, int len) throws IOException 
      { 
       LOG.info(new String(b, off, len)); 
      } 

     @Override 
     public void write(int b) throws IOException 
      { 
       write(new byte[] { (byte) b }); 
      } 
    } 

बनाया तो मैं अपने उत्पादन

// redirect the output to our logstream 
javax.mail.Session def = javax.mail.Session.getDefaultInstance(new Properties()); 
def.setDebugOut(new PrintStream(new LogStream(null))); 
def.setDebug(true); 

को javamail कि रीडायरेक्ट किए गए चाल है :)

संबंधित मुद्दे