001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  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.apache.commons.transaction.util;
018
019import java.io.PrintWriter;
020
021/**
022 * Logger implementation that logs into a pring writer like the one
023 * passed in JCA.
024 *
025 * @version $Id: PrintWriterLogger.java 493628 2007-01-07 01:42:48Z joerg $
026 */
027public class PrintWriterLogger implements LoggerFacade {
028
029    protected PrintWriter printWriter;
030    protected String name;
031    protected boolean debug;
032
033    public PrintWriterLogger(PrintWriter printWriter, String name, boolean debug) {
034        this.printWriter = printWriter;
035        this.name = name;
036        this.debug = debug;
037    }
038
039    public LoggerFacade createLogger(String newName) {
040        return new PrintWriterLogger(this.printWriter, newName, this.debug);
041    }
042
043    public void logInfo(String message) {
044        log("INFO", message);
045    }
046
047    public void logFine(String message) {
048        if (debug)
049        log("FINE", message);
050    }
051
052    public boolean isFineEnabled() {
053        return debug;
054    }
055
056    public void logFiner(String message) {
057        if (debug)
058            log("FINER", message);
059    }
060
061    public boolean isFinerEnabled() {
062        return debug;
063    }
064
065    public void logFinest(String message) {
066        if (debug)
067            log("FINEST", message);
068    }
069
070    public boolean isFinestEnabled() {
071        return debug;
072    }
073
074    public void logWarning(String message) {
075        log("WARNING", message);
076    }
077
078    public void logWarning(String message, Throwable t) {
079        log("WARNING", message);
080        t.printStackTrace(printWriter);
081    }
082
083    public void logSevere(String message) {
084        log("SEVERE", message);
085    }
086
087    public void logSevere(String message, Throwable t) {
088        log("SEVERE", message);
089        t.printStackTrace(printWriter);
090    }
091
092    protected void log(String level, String message) {
093        printWriter.write(name + "(" + level + ":" + message);
094        printWriter.flush();
095    }
096}