1 25 26 package org.objectweb.easybeans.log; 27 28 import java.io.OutputStreamWriter ; 29 import java.io.Writer ; 30 import java.util.logging.ErrorManager ; 31 import java.util.logging.Handler ; 32 import java.util.logging.Level ; 33 import java.util.logging.LogRecord ; 34 35 41 public class JDKConsoleHandler extends Handler { 42 43 46 private Writer infoOutputWriter; 47 48 51 private Writer errOutputWriter; 52 53 56 public JDKConsoleHandler() { 57 super(); 58 infoOutputWriter = new OutputStreamWriter (System.out); 59 errOutputWriter = new OutputStreamWriter (System.err); 60 } 61 62 66 @Override 67 public void publish(final LogRecord logRecord) { 68 if (!isLoggable(logRecord)) { 70 return; 71 } 72 73 String msg = null; 75 try { 76 msg = getFormatter().format(logRecord); 77 } catch (Exception ex) { 78 reportError(null, ex, ErrorManager.FORMAT_FAILURE); 79 return; 80 } 81 82 Writer writer = null; 84 if (logRecord.getLevel().intValue() >= Level.WARNING.intValue()) { 85 writer = errOutputWriter; 86 } else { 87 writer = infoOutputWriter; 88 } 89 90 try { 92 writer.write(msg); 93 } catch (Exception ex) { 94 reportError(null, ex, ErrorManager.WRITE_FAILURE); 95 } 96 97 flush(writer); 99 } 100 101 105 private void flush(final Writer writer) { 106 try { 107 writer.flush(); 108 } catch (Exception ex) { 109 reportError(null, ex, ErrorManager.FLUSH_FAILURE); 110 } 111 } 112 113 116 @Override 117 public void flush() { 118 flush(errOutputWriter); 119 flush(infoOutputWriter); 120 } 121 122 125 @Override 126 public void close() { 127 flush(); 128 } 129 } 130 | Popular Tags |