Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
StringBufferLoggerImpl |
|
| 3.25;3.25 |
1 | package org.apache.ojb.broker.util.logging; | |
2 | ||
3 | /* Copyright 2002-2005 The Apache Software Foundation | |
4 | * | |
5 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
6 | * you may not use this file except in compliance with the License. | |
7 | * 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 | ||
18 | import org.apache.commons.lang.SystemUtils; | |
19 | import org.apache.commons.lang.exception.ExceptionUtils; | |
20 | ||
21 | /** | |
22 | * This class is a {@link Logger} implementation based on a {@link StringBuffer}. All | |
23 | * logging method calls are written in the buffer. With method {@link #flushLogBuffer()} | |
24 | * the buffer will be cleared and the buffered log statements returned as string. | |
25 | * | |
26 | * @version $Id: StringBufferLoggerImpl.java,v 1.1 2007-08-24 22:17:32 ewestfal Exp $ | |
27 | */ | |
28 | public class StringBufferLoggerImpl extends PoorMansLoggerImpl | |
29 | { | |
30 | protected String EOL = SystemUtils.LINE_SEPARATOR; | |
31 | private StringBuffer buffer; | |
32 | private boolean errorLog; | |
33 | ||
34 | public StringBufferLoggerImpl(String name) | |
35 | { | |
36 | super(name); | |
37 | buffer = new StringBuffer(1000); | |
38 | } | |
39 | ||
40 | /** | |
41 | * Log all statements in a {@link StringBuffer}. | |
42 | */ | |
43 | protected void log(String aLevel, Object obj, Throwable t) | |
44 | { | |
45 | buffer.append(BRAKE_OPEN).append(getName()).append(BRAKE_CLOSE).append(aLevel); | |
46 | if (obj != null && obj instanceof Throwable) | |
47 | { | |
48 | try | |
49 | { | |
50 | buffer.append(((Throwable) obj).getMessage()).append(EOL); | |
51 | ((Throwable) obj).printStackTrace(); | |
52 | } | |
53 | catch (Throwable ignored) | |
54 | { | |
55 | /*logging should be failsafe*/ | |
56 | } | |
57 | } | |
58 | else | |
59 | { | |
60 | try | |
61 | { | |
62 | buffer.append(obj).append(EOL); | |
63 | } | |
64 | catch(Exception e) | |
65 | { | |
66 | // ignore | |
67 | } | |
68 | } | |
69 | ||
70 | if (t != null) | |
71 | { | |
72 | try | |
73 | { | |
74 | buffer.append(t.getMessage()).append(EOL); | |
75 | buffer.append(ExceptionUtils.getFullStackTrace(t)).append(EOL); | |
76 | } | |
77 | catch (Throwable ignored) | |
78 | { | |
79 | /*logging should be failsafe*/ | |
80 | } | |
81 | } | |
82 | if(!errorLog && (aLevel.equals(STR_ERROR) || aLevel.equals(STR_FATAL))) | |
83 | { | |
84 | errorLog = true; | |
85 | } | |
86 | } | |
87 | ||
88 | /** | |
89 | * Returns <em>true</em> if one or more error/fatal log method calls | |
90 | * have been logged in the buffer. | |
91 | */ | |
92 | public boolean isErrorLog() | |
93 | { | |
94 | return errorLog; | |
95 | } | |
96 | ||
97 | /** | |
98 | * Returns all buffered log statements as string and clear the buffer. | |
99 | */ | |
100 | public String flushLogBuffer() | |
101 | { | |
102 | String result = buffer.toString(); | |
103 | buffer = new StringBuffer(1000); | |
104 | return result; | |
105 | } | |
106 | } |