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