View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.transaction.util;
18  
19  import java.io.PrintWriter;
20  
21  /**
22   * Logger implementation that logs into a pring writer like the one
23   * passed in JCA.
24   *
25   * @version $Id: PrintWriterLogger.java 493628 2007-01-07 01:42:48Z joerg $
26   */
27  public class PrintWriterLogger implements LoggerFacade {
28  
29      protected PrintWriter printWriter;
30      protected String name;
31      protected boolean debug;
32  
33      public PrintWriterLogger(PrintWriter printWriter, String name, boolean debug) {
34          this.printWriter = printWriter;
35          this.name = name;
36          this.debug = debug;
37      }
38  
39      public LoggerFacade createLogger(String newName) {
40          return new PrintWriterLogger(this.printWriter, newName, this.debug);
41      }
42  
43      public void logInfo(String message) {
44          log("INFO", message);
45      }
46  
47      public void logFine(String message) {
48          if (debug)
49          log("FINE", message);
50      }
51  
52      public boolean isFineEnabled() {
53          return debug;
54      }
55  
56      public void logFiner(String message) {
57          if (debug)
58              log("FINER", message);
59      }
60  
61      public boolean isFinerEnabled() {
62          return debug;
63      }
64  
65      public void logFinest(String message) {
66          if (debug)
67              log("FINEST", message);
68      }
69  
70      public boolean isFinestEnabled() {
71          return debug;
72      }
73  
74      public void logWarning(String message) {
75          log("WARNING", message);
76      }
77  
78      public void logWarning(String message, Throwable t) {
79          log("WARNING", message);
80          t.printStackTrace(printWriter);
81      }
82  
83      public void logSevere(String message) {
84          log("SEVERE", message);
85      }
86  
87      public void logSevere(String message, Throwable t) {
88          log("SEVERE", message);
89          t.printStackTrace(printWriter);
90      }
91  
92      protected void log(String level, String message) {
93          printWriter.write(name + "(" + level + ":" + message);
94          printWriter.flush();
95      }
96  }