001: /*
002: * Jacareto Copyright (c) 2002-2005
003: * Applied Computer Science Research Group, Darmstadt University of
004: * Technology, Institute of Mathematics & Computer Science,
005: * Ludwigsburg University of Education, and Computer Based
006: * Learning Research Group, Aachen University. All rights reserved.
007: *
008: * Jacareto is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * Jacareto is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public
019: * License along with Jacareto; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: *
022: */
023:
024: package jacareto.toolkit.log4j;
025:
026: import org.apache.log4j.Level;
027: import org.apache.log4j.Logger;
028:
029: import java.io.OutputStream;
030:
031: /**
032: * This class logs all bytes written to it as output stream with a specified logging level.
033: *
034: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
035: * @version 1.0
036: */
037: public class LogOutputStream extends OutputStream {
038: /** The logger where to log the written bytes. */
039: private Logger logger;
040:
041: /** The level. */
042: private Level level;
043:
044: /** The internal memory for the written bytes. */
045: private String mem;
046:
047: /**
048: * Creates a new log output stream which logs bytes to the specified logger with the specified
049: * level.
050: *
051: * @param logger the logger where to log the written bytes
052: * @param level the level
053: */
054: public LogOutputStream(Logger logger, Level level) {
055: setLogger(logger);
056: setLevel(level);
057: mem = "";
058: }
059:
060: /**
061: * Sets the logger where to log the bytes.
062: *
063: * @param logger the logger
064: */
065: public void setLogger(Logger logger) {
066: this .logger = logger;
067: }
068:
069: /**
070: * Returns the logger.
071: *
072: * @return DOCUMENT ME!
073: */
074: public Logger getLogger() {
075: return logger;
076: }
077:
078: /**
079: * Sets the logging level.
080: *
081: * @param level DOCUMENT ME!
082: */
083: public void setLevel(Level level) {
084: this .level = level;
085: }
086:
087: /**
088: * Returns the logging level.
089: *
090: * @return DOCUMENT ME!
091: */
092: public Level getLevel() {
093: return level;
094: }
095:
096: /**
097: * Writes a byte to the output stream. This method flushes automatically at the end of a line.
098: *
099: * @param b DOCUMENT ME!
100: */
101: public void write(int b) {
102: byte[] bytes = new byte[1];
103: bytes[0] = (byte) (b & 0xff);
104: mem = mem + new String(bytes);
105:
106: if (mem.endsWith("\n")) {
107: mem = mem.substring(0, mem.length() - 1);
108: flush();
109: }
110: }
111:
112: /**
113: * Flushes the output stream.
114: */
115: public void flush() {
116: logger.log(level, mem);
117: mem = "";
118: }
119: }
|