KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > log4j > lf5 > util > AdapterLogRecord


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.log4j.lf5.util;
17
18 import org.apache.log4j.lf5.LogLevel;
19 import org.apache.log4j.lf5.LogRecord;
20
21 import java.io.PrintWriter JavaDoc;
22 import java.io.StringWriter JavaDoc;
23
24 /**
25  * <p>A LogRecord to be used with the LogMonitorAdapter</p>
26  *
27  * @author Richard Hurst
28  */

29
30 // Contributed by ThoughtWorks Inc.
31

32 public class AdapterLogRecord extends LogRecord {
33   //--------------------------------------------------------------------------
34
// Constants:
35
//--------------------------------------------------------------------------
36

37   //--------------------------------------------------------------------------
38
// Protected Variables:
39
//--------------------------------------------------------------------------
40

41   //--------------------------------------------------------------------------
42
// Private Variables:
43
//--------------------------------------------------------------------------
44
private static LogLevel severeLevel = null;
45
46   private static StringWriter JavaDoc sw = new StringWriter JavaDoc();
47   private static PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw);
48
49   //--------------------------------------------------------------------------
50
// Constructors:
51
//--------------------------------------------------------------------------
52
public AdapterLogRecord() {
53     super();
54   }
55
56   //--------------------------------------------------------------------------
57
// Public Methods:
58
//--------------------------------------------------------------------------
59
public void setCategory(String JavaDoc category) {
60     super.setCategory(category);
61     super.setLocation(getLocationInfo(category));
62   }
63
64   public boolean isSevereLevel() {
65     if (severeLevel == null) return false;
66     return severeLevel.equals(getLevel());
67   }
68
69   public static void setSevereLevel(LogLevel level) {
70     severeLevel = level;
71   }
72
73   public static LogLevel getSevereLevel() {
74     return severeLevel;
75   }
76
77   //--------------------------------------------------------------------------
78
// Protected Methods:
79
//--------------------------------------------------------------------------
80
protected String JavaDoc getLocationInfo(String JavaDoc category) {
81     String JavaDoc stackTrace = stackTraceToString(new Throwable JavaDoc());
82     String JavaDoc line = parseLine(stackTrace, category);
83     return line;
84   }
85
86   protected String JavaDoc stackTraceToString(Throwable JavaDoc t) {
87     String JavaDoc s = null;
88
89     synchronized (sw) {
90       t.printStackTrace(pw);
91       s = sw.toString();
92       sw.getBuffer().setLength(0);
93     }
94
95     return s;
96   }
97
98   protected String JavaDoc parseLine(String JavaDoc trace, String JavaDoc category) {
99     int index = trace.indexOf(category);
100     if (index == -1) return null;
101     trace = trace.substring(index);
102     trace = trace.substring(0, trace.indexOf(")") + 1);
103     return trace;
104   }
105   //--------------------------------------------------------------------------
106
// Private Methods:
107
//--------------------------------------------------------------------------
108

109   //--------------------------------------------------------------------------
110
// Nested Top-Level Classes or Interfaces
111
//--------------------------------------------------------------------------
112
}
113
114
Popular Tags