1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package org.slf4j.ext;
26
27 import org.slf4j.Logger;
28 import org.slf4j.Marker;
29 import org.slf4j.MarkerFactory;
30 import org.slf4j.helpers.FormattingTuple;
31 import org.slf4j.helpers.MessageFormatter;
32 import org.slf4j.spi.LocationAwareLogger;
33
34
35
36
37
38
39
40
41 public class XLogger extends LoggerWrapper implements Logger {
42
43 private static final String FQCN = XLogger.class.getName();
44 static Marker FLOW_MARKER = MarkerFactory.getMarker("FLOW");
45 static Marker ENTRY_MARKER = MarkerFactory.getMarker("ENTRY");
46 static Marker EXIT_MARKER = MarkerFactory.getMarker("EXIT");
47
48 static Marker EXCEPTION_MARKER = MarkerFactory.getMarker("EXCEPTION");
49 static Marker THROWING_MARKER = MarkerFactory.getMarker("THROWING");
50 static Marker CATCHING_MARKER = MarkerFactory.getMarker("CATCHING");
51
52 static String EXIT_MESSAGE_0 = "exit";
53 static String EXIT_MESSAGE_1 = "exit with ({})";
54
55 static String ENTRY_MESSAGE_0 = "entry";
56 static String ENTRY_MESSAGE_1 = "entry with ({})";
57 static String ENTRY_MESSAGE_2 = "entry with ({}, {})";
58 static String ENTRY_MESSAGE_3 = "entry with ({}, {}, {})";
59 static String ENTRY_MESSAGE_4 = "entry with ({}, {}, {}, {})";
60 static int ENTRY_MESSAGE_ARRAY_LEN = 5;
61 static String[] ENTRY_MESSAGE_ARRAY = new String[ENTRY_MESSAGE_ARRAY_LEN];
62 static {
63 ENTRY_MARKER.add(FLOW_MARKER);
64 EXIT_MARKER.add(FLOW_MARKER);
65 THROWING_MARKER.add(EXCEPTION_MARKER);
66 CATCHING_MARKER.add(EXCEPTION_MARKER);
67
68 ENTRY_MESSAGE_ARRAY[0] = ENTRY_MESSAGE_0;
69 ENTRY_MESSAGE_ARRAY[1] = ENTRY_MESSAGE_1;
70 ENTRY_MESSAGE_ARRAY[2] = ENTRY_MESSAGE_2;
71 ENTRY_MESSAGE_ARRAY[3] = ENTRY_MESSAGE_3;
72 ENTRY_MESSAGE_ARRAY[4] = ENTRY_MESSAGE_4;
73 }
74
75 public enum Level {
76 TRACE("TRACE", LocationAwareLogger.TRACE_INT), DEBUG("DEBUG", LocationAwareLogger.DEBUG_INT), INFO("INFO", LocationAwareLogger.INFO_INT), WARN("WARN",
77 LocationAwareLogger.WARN_INT), ERROR("ERROR", LocationAwareLogger.ERROR_INT);
78
79 private final String name;
80 private final int level;
81
82 public String toString() {
83 return this.name;
84 }
85
86 public int intValue() {
87 return this.level;
88 }
89
90 private Level(String name, int level) {
91 this.name = name;
92 this.level = level;
93 }
94 }
95
96
97
98
99
100
101 public XLogger(Logger logger) {
102
103
104
105 super(logger, LoggerWrapper.class.getName());
106 }
107
108
109
110
111
112
113 public void entry(Object... argArray) {
114 if (instanceofLAL && logger.isTraceEnabled(ENTRY_MARKER)) {
115 String messagePattern = null;
116 if (argArray.length < ENTRY_MESSAGE_ARRAY_LEN) {
117 messagePattern = ENTRY_MESSAGE_ARRAY[argArray.length];
118 } else {
119 messagePattern = buildMessagePattern(argArray.length);
120 }
121 FormattingTuple tp = MessageFormatter.arrayFormat(messagePattern, argArray);
122 ((LocationAwareLogger) logger).log(ENTRY_MARKER, FQCN, LocationAwareLogger.TRACE_INT, tp.getMessage(), argArray, tp.getThrowable());
123 }
124 }
125
126
127
128
129 public void exit() {
130 if (instanceofLAL && logger.isTraceEnabled(ENTRY_MARKER)) {
131 ((LocationAwareLogger) logger).log(EXIT_MARKER, FQCN, LocationAwareLogger.TRACE_INT, EXIT_MESSAGE_0, null, null);
132 }
133 }
134
135
136
137
138
139
140 public <T> T exit(T result) {
141 if (instanceofLAL && logger.isTraceEnabled(ENTRY_MARKER)) {
142 FormattingTuple tp = MessageFormatter.format(EXIT_MESSAGE_1, result);
143 ((LocationAwareLogger) logger).log(EXIT_MARKER, FQCN, LocationAwareLogger.TRACE_INT, tp.getMessage(), new Object[] { result }, tp.getThrowable());
144 }
145 return result;
146 }
147
148
149
150
151
152
153 public <T extends Throwable> T throwing(T throwable) {
154 if (instanceofLAL) {
155 ((LocationAwareLogger) logger).log(THROWING_MARKER, FQCN, LocationAwareLogger.ERROR_INT, "throwing", null, throwable);
156 }
157 return throwable;
158 }
159
160
161
162
163
164
165
166 public <T extends Throwable> T throwing(Level level, T throwable) {
167 if (instanceofLAL) {
168 ((LocationAwareLogger) logger).log(THROWING_MARKER, FQCN, level.level, "throwing", null, throwable);
169 }
170 return throwable;
171 }
172
173
174
175
176
177
178 public void catching(Throwable throwable) {
179 if (instanceofLAL) {
180 ((LocationAwareLogger) logger).log(CATCHING_MARKER, FQCN, LocationAwareLogger.ERROR_INT, "catching", null, throwable);
181 }
182 }
183
184
185
186
187
188
189
190 public void catching(Level level, Throwable throwable) {
191 if (instanceofLAL) {
192 ((LocationAwareLogger) logger).log(CATCHING_MARKER, FQCN, level.level, "catching", null, throwable);
193 }
194 }
195
196 private static String buildMessagePattern(int len) {
197 StringBuilder sb = new StringBuilder();
198 sb.append(" entry with (");
199 for (int i = 0; i < len; i++) {
200 sb.append("{}");
201 if (i != len - 1)
202 sb.append(", ");
203 }
204 sb.append(')');
205 return sb.toString();
206 }
207 }