1 | // Copyright (c) 2002 SPY internetworking <dustin@spy.net> |
2 | |
3 | package net.spy.memcached.compat.log; |
4 | |
5 | /** |
6 | * Abstract implementation of Logger providing most of the common |
7 | * framework. |
8 | */ |
9 | public abstract class AbstractLogger implements Logger { |
10 | |
11 | private final String name; |
12 | |
13 | /** |
14 | * Instantiate the abstract logger. |
15 | */ |
16 | protected AbstractLogger(String nm) { |
17 | super(); |
18 | if(nm == null) { |
19 | throw new NullPointerException("Logger name may not be null."); |
20 | } |
21 | name=nm; |
22 | } |
23 | |
24 | /** |
25 | * Get the name of this logger. |
26 | */ |
27 | public String getName() { |
28 | return(name); |
29 | } |
30 | |
31 | /** |
32 | * Get the throwable from the last element of this array if it is |
33 | * Throwable, else null. |
34 | */ |
35 | public Throwable getThrowable(Object args[]) { |
36 | Throwable rv=null; |
37 | if(args.length > 0) { |
38 | if(args[args.length-1] instanceof Throwable) { |
39 | rv=(Throwable)args[args.length-1]; |
40 | } |
41 | } |
42 | return rv; |
43 | } |
44 | |
45 | /** |
46 | * True if debug is enabled for this logger. |
47 | * Default implementation always returns false |
48 | * |
49 | * @return true if debug messages would be displayed |
50 | */ |
51 | public abstract boolean isDebugEnabled(); |
52 | |
53 | /** |
54 | * True if debug is enabled for this logger. |
55 | * Default implementation always returns false |
56 | * |
57 | * @return true if info messages would be displayed |
58 | */ |
59 | public abstract boolean isInfoEnabled(); |
60 | |
61 | /** |
62 | * Log a message at debug level. |
63 | * |
64 | * @param message the message to log |
65 | * @param exception the exception that caused the message to be generated |
66 | */ |
67 | public void debug(Object message, Throwable exception) { |
68 | log(Level.DEBUG, message, exception); |
69 | } |
70 | /** |
71 | * Log a formatted message at debug level. |
72 | * |
73 | * @param message the message to log |
74 | * @param args the arguments for that message |
75 | */ |
76 | public void debug(String message, Object... args) { |
77 | if(isDebugEnabled()) { |
78 | debug(String.format(message, args), getThrowable(args)); |
79 | } |
80 | } |
81 | |
82 | /** |
83 | * Log a message at debug level. |
84 | * |
85 | * @param message the message to log |
86 | */ |
87 | public void debug(Object message) { |
88 | debug(message, null); |
89 | } |
90 | |
91 | /** |
92 | * Log a message at info level. |
93 | * |
94 | * @param message the message to log |
95 | * @param exception the exception that caused the message to be generated |
96 | */ |
97 | public void info(Object message, Throwable exception) { |
98 | log(Level.INFO, message, exception); |
99 | } |
100 | /** |
101 | * Log a formatted message at info level. |
102 | * |
103 | * @param message the message to log |
104 | * @param args the arguments for that message |
105 | */ |
106 | public void info(String message, Object... args) { |
107 | if(isInfoEnabled()) { |
108 | info(String.format(message, args), getThrowable(args)); |
109 | } |
110 | } |
111 | |
112 | /** |
113 | * Log a message at info level. |
114 | * |
115 | * @param message the message to log |
116 | */ |
117 | public void info(Object message) { |
118 | info(message, null); |
119 | } |
120 | |
121 | /** |
122 | * Log a message at warning level. |
123 | * |
124 | * @param message the message to log |
125 | * @param exception the exception that caused the message to be generated |
126 | */ |
127 | public void warn(Object message, Throwable exception) { |
128 | log(Level.WARN, message, exception); |
129 | } |
130 | /** |
131 | * Log a formatted message at debug level. |
132 | * |
133 | * @param message the message to log |
134 | * @param args the arguments for that message |
135 | */ |
136 | public void warn(String message, Object... args) { |
137 | warn(String.format(message, args), getThrowable(args)); |
138 | } |
139 | |
140 | /** |
141 | * Log a message at warning level. |
142 | * |
143 | * @param message the message to log |
144 | */ |
145 | public void warn(Object message) { |
146 | warn(message, null); |
147 | } |
148 | |
149 | /** |
150 | * Log a message at error level. |
151 | * |
152 | * @param message the message to log |
153 | * @param exception the exception that caused the message to be generated |
154 | */ |
155 | public void error(Object message, Throwable exception) { |
156 | log(Level.ERROR, message, exception); |
157 | } |
158 | /** |
159 | * Log a formatted message at debug level. |
160 | * |
161 | * @param message the message to log |
162 | * @param args the arguments for that message |
163 | */ |
164 | public void error(String message, Object... args) { |
165 | error(String.format(message, args), getThrowable(args)); |
166 | } |
167 | |
168 | /** |
169 | * Log a message at error level. |
170 | * |
171 | * @param message the message to log |
172 | */ |
173 | public void error(Object message) { |
174 | error(message, null); |
175 | } |
176 | |
177 | /** |
178 | * Log a message at fatal level. |
179 | * |
180 | * @param message the message to log |
181 | * @param exception the exception that caused the message to be generated |
182 | */ |
183 | public void fatal(Object message, Throwable exception) { |
184 | log(Level.FATAL, message, exception); |
185 | } |
186 | /** |
187 | * Log a formatted message at debug level. |
188 | * |
189 | * @param message the message to log |
190 | * @param args the arguments for that message |
191 | */ |
192 | public void fatal(String message, Object... args) { |
193 | fatal(String.format(message, args), getThrowable(args)); |
194 | } |
195 | |
196 | /** |
197 | * Log a message at fatal level. |
198 | * |
199 | * @param message the message to log |
200 | */ |
201 | public void fatal(Object message) { |
202 | fatal(message, null); |
203 | } |
204 | |
205 | /** |
206 | * Log a message at the given level. |
207 | * |
208 | * @param level the level |
209 | * @param message the message |
210 | */ |
211 | public void log(Level level, Object message) { |
212 | log(level, message, null); |
213 | } |
214 | |
215 | /** |
216 | * Subclasses should implement this method to determine what to do when |
217 | * a client wants to log at a particular level. |
218 | * |
219 | * @param level the level to log at (see the fields of this class) |
220 | * @param message the message to log |
221 | * @param e the exception that caused the message (or null) |
222 | */ |
223 | public abstract void log(Level level, Object message, Throwable e); |
224 | |
225 | } |