001: /**
002: * Copyright (C) 2004 France Telecom R&D
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */package org.objectweb.util.monolog.wrapper.javaLog;
018:
019: import org.objectweb.util.monolog.wrapper.common.OutputStreamSwitcher;
020: import org.objectweb.util.monolog.api.BasicLevel;
021:
022: import java.io.OutputStream;
023: import java.io.Writer;
024: import java.io.OutputStreamWriter;
025: import java.io.UnsupportedEncodingException;
026: import java.util.logging.LogRecord;
027: import java.util.logging.ErrorManager;
028:
029: /**
030: * The aim of this class is to permit to specify the output for the console
031: * handler of the java.util.logging system.
032: *
033: * This console handler is also able to choose the right ouput (System.err
034: * or System.out) depending on the message level.
035: *
036: * @author S.Chassande-Barrioz
037: */
038: public class ConsoleHandler extends java.util.logging.Handler {
039:
040: protected OutputStreamSwitcher oss;
041: private Writer writer;
042:
043: public ConsoleHandler() {
044: super ();
045: }
046:
047: public void desactivateSwitching(OutputStream newOut) {
048: if (oss != null) {
049: oss = null;
050: setOutput(newOut);
051: }
052: }
053:
054: public void activateSwitching() {
055: if (oss == null) {
056: oss = new OutputStreamSwitcher();
057: setOutput(oss);
058: }
059: }
060:
061: /**
062: * Assign the Outputstream by calling a protected method from the super
063: * class.
064: */
065: public void setOutput(OutputStream out) throws SecurityException {
066: if (out == null) {
067: throw new NullPointerException();
068: }
069: flush();
070: String encoding = getEncoding();
071: if (encoding == null) {
072: writer = new OutputStreamWriter(out);
073: } else {
074: try {
075: writer = new OutputStreamWriter(out, encoding);
076: } catch (UnsupportedEncodingException ex) {
077: // This shouldn't happen. The setEncoding method
078: // should have validated that the encoding is OK.
079: throw new Error("Unexpected exception " + ex);
080: }
081: }
082: }
083:
084: public void publish(LogRecord record) {
085: if (!isLoggable(record)) {
086: return;
087: }
088: String msg;
089: try {
090: msg = getFormatter().format(record);
091: } catch (Exception ex) {
092: // We don't want to throw an exception here, but we
093: // report the exception to any registered ErrorManager.
094: reportError(null, ex, ErrorManager.FORMAT_FAILURE);
095: return;
096: }
097:
098: if (oss != null) {
099: if (record.getLevel().intValue() >= BasicLevel.WARN) {
100: oss.switchOutput(System.err);
101: } else {
102: oss.switchOutput(System.out);
103: }
104: }
105: try {
106: writer.write(msg);
107: } catch (Exception ex) {
108: // We don't want to throw an exception here, but we
109: // report the exception to any registered ErrorManager.
110: reportError(null, ex, ErrorManager.WRITE_FAILURE);
111: }
112: flush();
113: }
114:
115: public void flush() {
116: if (writer != null) {
117: try {
118: writer.flush();
119: } catch (Exception ex) {
120: // We don't want to throw an exception here, but we
121: // report the exception to any registered ErrorManager.
122: reportError(null, ex, ErrorManager.FLUSH_FAILURE);
123: }
124: }
125: }
126:
127: public void close() throws SecurityException {
128: flush();
129: }
130: }
|