Java tutorial
/******************************************************************************* * Copyright 2013 Marius Giepz * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. ******************************************************************************/ package org.saiku.reporting.backend.util; import java.io.OutputStream; import org.apache.commons.logging.Log; import org.saiku.reporting.backend.service.SaikuProperties; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * This class logs all bytes written to it as output stream with a specified logging level. * * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a> * @version 1.0 */ public class LogOutputStream extends OutputStream { /** The logger where to log the written bytes. */ private Logger logger; /** The internal memory for the written bytes. */ private String mem; /** * Creates a new log output stream which logs bytes to the specified logger with the specified * level. * * @param logger the logger where to log the written bytes * @param level the level */ public LogOutputStream(Logger logger) { setLogger(logger); mem = ""; } /** * Sets the logger where to log the bytes. * * @param logger the logger */ public void setLogger(Logger logger) { this.logger = logger; } /** * Returns the logger. * * @return DOCUMENT ME! */ public Logger getLogger() { return logger; } /** * Writes a byte to the output stream. This method flushes automatically at the end of a line. * * @param b DOCUMENT ME! */ public void write(int b) { byte[] bytes = new byte[1]; bytes[0] = (byte) (b & 0xff); mem = mem + new String(bytes); if (mem.endsWith("\n")) { mem = mem.substring(0, mem.length() - 1); flush(); } } /** * Flushes the output stream. */ public void flush() { logger.info(mem); mem = ""; } }