01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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,
34: boolean debug) {
35: this .printWriter = printWriter;
36: this .name = name;
37: this .debug = debug;
38: }
39:
40: public LoggerFacade createLogger(String newName) {
41: return new PrintWriterLogger(this .printWriter, newName,
42: this .debug);
43: }
44:
45: public void logInfo(String message) {
46: log("INFO", message);
47: }
48:
49: public void logFine(String message) {
50: if (debug)
51: log("FINE", message);
52: }
53:
54: public boolean isFineEnabled() {
55: return debug;
56: }
57:
58: public void logFiner(String message) {
59: if (debug)
60: log("FINER", message);
61: }
62:
63: public boolean isFinerEnabled() {
64: return debug;
65: }
66:
67: public void logFinest(String message) {
68: if (debug)
69: log("FINEST", message);
70: }
71:
72: public boolean isFinestEnabled() {
73: return debug;
74: }
75:
76: public void logWarning(String message) {
77: log("WARNING", message);
78: }
79:
80: public void logWarning(String message, Throwable t) {
81: log("WARNING", message);
82: t.printStackTrace(printWriter);
83: }
84:
85: public void logSevere(String message) {
86: log("SEVERE", message);
87: }
88:
89: public void logSevere(String message, Throwable t) {
90: log("SEVERE", message);
91: t.printStackTrace(printWriter);
92: }
93:
94: protected void log(String level, String message) {
95: printWriter.write(name + "(" + level + ":" + message);
96: printWriter.flush();
97: }
98: }
|