001/*-------------------------------------------------------------------------+ 002| | 003| Copyright 2005-2011 The ConQAT Project | 004| | 005| Licensed under the Apache License, Version 2.0 (the "License"); | 006| you may not use this file except in compliance with the License. | 007| You may obtain a copy of the License at | 008| | 009| http://www.apache.org/licenses/LICENSE-2.0 | 010| | 011| Unless required by applicable law or agreed to in writing, software | 012| distributed under the License is distributed on an "AS IS" BASIS, | 013| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 014| See the License for the specific language governing permissions and | 015| limitations under the License. | 016+-------------------------------------------------------------------------*/ 017package org.conqat.lib.commons.logging; 018 019import static org.conqat.lib.commons.string.StringUtils.obtainStackTrace; 020 021import java.io.OutputStream; 022import java.io.PrintWriter; 023 024/** 025 * Simple logger that writes all messages to a stream or print writer. 026 * 027 * @author deissenb 028 */ 029public class SimpleLogger implements ILogger { 030 031 /** The writer used for output. */ 032 private final PrintWriter writer; 033 034 /** Create logger that logs to {@link System#out}. */ 035 public SimpleLogger() { 036 this(System.out); 037 } 038 039 /** Create logger that logs to a stream. */ 040 public SimpleLogger(OutputStream stream) { 041 writer = new PrintWriter(stream, true); 042 } 043 044 /** Create logger that logs to a writer. */ 045 public SimpleLogger(PrintWriter writer) { 046 this.writer = writer; 047 } 048 049 /** Logs a message to the print writer. */ 050 protected void log(String prefix, Object message, Throwable throwable) { 051 writer.print(prefix + ": " + message); 052 if (throwable != null) { 053 writer.print(": " + obtainStackTrace(throwable)); 054 } 055 writer.println(); 056 } 057 058 /** {@inheritDoc} */ 059 @Override 060 public void debug(Object message) { 061 log("DEBUG", message, null); 062 } 063 064 /** {@inheritDoc} */ 065 @Override 066 public void debug(Object message, Throwable throwable) { 067 log("DEBUG", message, throwable); 068 } 069 070 /** {@inheritDoc} */ 071 @Override 072 public void error(Object message) { 073 log("ERROR", message, null); 074 } 075 076 /** {@inheritDoc} */ 077 @Override 078 public void error(Object message, Throwable throwable) { 079 log("ERROR", message, throwable); 080 } 081 082 /** {@inheritDoc} */ 083 @Override 084 public void info(Object message) { 085 log("INFO", message, null); 086 } 087 088 /** {@inheritDoc} */ 089 @Override 090 public void info(Object message, Throwable throwable) { 091 log("INFO", message, throwable); 092 } 093 094 /** {@inheritDoc} */ 095 @Override 096 public void warn(Object message) { 097 log("WARN", message, null); 098 } 099 100 /** {@inheritDoc} */ 101 @Override 102 public void warn(Object message, Throwable throwable) { 103 log("WARN", message, throwable); 104 } 105 106}