-
Notifications
You must be signed in to change notification settings - Fork 371
Expand file tree
/
Copy pathLogger.java
More file actions
433 lines (388 loc) · 15.5 KB
/
Logger.java
File metadata and controls
433 lines (388 loc) · 15.5 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/**
* OWASP Enterprise Security API (ESAPI)
*
* This file is part of the Open Web Application Security Project (OWASP)
* Enterprise Security API (ESAPI) project. For details, please see
* <a href="http://www.owasp.org/index.php/ESAPI">http://www.owasp.org/index.php/ESAPI</a>.
*
* Copyright (c) 2007-2019 - The OWASP Foundation
*
* The ESAPI is published by OWASP under the BSD license. You should read and accept the
* LICENSE before you use, modify, and/or redistribute this software.
*
* @author Jeff Williams <a href="http://www.aspectsecurity.com">Aspect Security</a>
* @created 2007
*/
package org.owasp.esapi;
/**
* The {@code Logger} interface defines a set of methods that can be used to log
* security events. It supports a hierarchy of logging levels which can be configured at runtime to determine
* the severity of events that are logged, and those below the current threshold that are discarded.
* Implementors should use a well established logging library
* as it is quite difficult to create a high-performance logger.
* <p>
* The logging levels defined by this interface (in descending order) are:
* <ul>
* <li>fatal (highest value)</li>
* <li>error</li>
* <li>warning</li>
* <li>info</li>
* <li>debug</li>
* <li>trace (lowest value)</li>
* </ul>
* There are also several variations of {@code always()} methods that will <i>always</i>
* log a message regardless of the log level.
* </p><p>
* ESAPI also allows for the definition of the type of log event that is being generated.
* The {@code Logger} interface predefines 6 types of Log events:
* <ul>
* <li>SECURITY_SUCCESS</li>
* <li>SECURITY_FAILURE</li>
* <li>SECURITY_AUDIT</li>
* <li>EVENT_SUCCESS</li>
* <li>EVENT_FAILURE</li>
* <li>EVENT_UNSPECIFIED</li>
* </ul>
* </p><p>
* Your custom implementation can extend or change this list if desired.
* </p><p>
* This {@code Logger} allows callers to determine which logging levels are enabled, and to submit events
* at different severity levels.<br>
* <br>Implementors of this interface should:
*
* <ol>
* <li>Provide a mechanism for setting the logging level threshold that is currently enabled. This usually works by logging all
* events at and above that severity level, and discarding all events below that level.
* This is usually done via configuration, but can also be made accessible programmatically.</li>
* <li>Ensure that dangerous HTML characters are encoded before they are logged to defend against malicious injection into logs
* that might be viewed in an HTML based log viewer.</li>
* <li>Encode any CRLF characters included in log data in order to prevent log injection attacks.</li>
* <li>Avoid logging the user's session ID. Rather, they should log something equivalent like a
* generated logging session ID, or a hashed value of the session ID so they can track session specific
* events without risking the exposure of a live session's ID.</li>
* <li>Record the following information with each event:</li>
* <ol type="a">
* <li>Identity of the user that caused the event.</li>
* <li>A description of the event (supplied by the caller).</li>
* <li>Whether the event succeeded or failed (indicated by the caller).</li>
* <li>Severity level of the event (indicated by the caller).</li>
* <li>That this is a security relevant event (indicated by the caller).</li>
* <li>Hostname or IP where the event occurred (and ideally the user's source IP as well).</li>
* <li>A date/time stamp.</li>
* </ol>
* </ol>
*
* Custom logger implementations might also:
* <ol start="6">
* <li>Filter out any sensitive data specific to the current application or organization, such as credit cards,
* social security numbers, etc.</li>
* </ol>
*
* There are both SLF4J and native Java Logging (i.e., {@code java.util.logging}, aka JUL) implementations
* of the ESAPI logger with JUL being our default logger for our stock <b>ESAPI.properties</b> file that
* is delivered along with ESAPI releases in a separate <b>esapi-configuration</b> jar available from the
* releases mentioned on
* <a href="https://github.com/ESAPI/esapi-java-legacy/releases/">ESAPI's GitHub Releases page</a>.
* </p><p>
* The {@code org.owasp.esapi.logging.java.JavaLogger} class uses the {@code java.util.logging} package as
* the basis for its logging implementation. Both provided implementations implement requirements #1 through #5 above.
* </p><p>
* <i>Customization</i>: It is expected that most organizations may wish to implement their own custom {@code Logger} class in
* order to integrate ESAPI logging with their specific logging infrastructure. The ESAPI reference implementations
* can serve as a useful starting point to intended to provide a simple functional example of an implementation, but
* they are also largely usable out-of-the-box with some additional minimal log configuration.
*
* @author Jeff Williams (jeff.williams .at. aspectsecurity.com) <a
* href="http://www.aspectsecurity.com">Aspect Security</a>
* @since June 1, 2007
*/
public interface Logger {
// All implied static final as this is an interface
/**
* A security type of log event that has succeeded. This is one of 6 predefined
* ESAPI logging events. New events can be added.
*/
EventType SECURITY_SUCCESS = new EventType( "SECURITY SUCCESS", true);
/**
* A security type of log event that has failed. This is one of 6 predefined
* ESAPI logging events. New events can be added.
*/
EventType SECURITY_FAILURE = new EventType( "SECURITY FAILURE", false);
/**
* A security type of log event that is associated with an audit trail of some type,
* but the log event is not specifically something that has either succeeded or failed
* or that is irrelevant in the case of this logged message.
*/
// CHECKME: Should the Boolean for this be 'null' or 'true'? See EVENT_UNSPECIFIED.
EventType SECURITY_AUDIT = new EventType( "SECURITY AUDIT", null);
/**
* A non-security type of log event that has succeeded. This is one of 6 predefined
* ESAPI logging events. New events can be added.
*/
EventType EVENT_SUCCESS = new EventType( "EVENT SUCCESS", true);
/**
* A non-security type of log event that has failed. This is one of 6 predefined
* ESAPI logging events. New events can be added.
*/
EventType EVENT_FAILURE = new EventType( "EVENT FAILURE", false);
/**
* A non-security type of log event that is unspecified. This is one of 6 predefined
* ESAPI logging events. New events can be added.
*/
EventType EVENT_UNSPECIFIED = new EventType( "EVENT UNSPECIFIED", null);
/**
* Defines the type of log event that is being generated. The Logger interface defines 6 types of Log events:
* SECURITY_SUCCESS, SECURITY_FAILURE, EVENT_SUCCESS, EVENT_FAILURE, EVENT_UNSPECIFIED.
* Your implementation can extend or change this list if desired.
*/
class EventType {
private String type;
private Boolean success = null;
public EventType (String name, Boolean newSuccess) {
this.type = name;
this.success = newSuccess;
}
public Boolean isSuccess() {
return success;
}
/**
* Convert the {@code EventType} to a string.
* @return The event type name.
*/
@Override
public String toString() {
return this.type;
}
}
/*
* The Logger interface defines 6 logging levels: FATAL, ERROR, WARNING, INFO, DEBUG, TRACE. It also
* supports ALL, which logs all events, and OFF, which disables all logging.
* Your implementation can extend or change this list if desired.
*/
/** OFF indicates that no messages should be logged. This level is initialized to Integer.MAX_VALUE. */
int OFF = Integer.MAX_VALUE;
/** FATAL indicates that only FATAL messages should be logged. This level is initialized to 1000. */
int FATAL = 1000;
/** ERROR indicates that ERROR messages and above should be logged.
* This level is initialized to 800. */
int ERROR = 800;
/** WARNING indicates that WARNING messages and above should be logged.
* This level is initialized to 600. */
int WARNING = 600;
/** INFO indicates that INFO messages and above should be logged.
* This level is initialized to 400. */
int INFO = 400;
/** DEBUG indicates that DEBUG messages and above should be logged.
* This level is initialized to 200. */
int DEBUG = 200;
/** TRACE indicates that TRACE messages and above should be logged.
* This level is initialized to 100. */
int TRACE = 100;
/** ALL indicates that all messages should be logged. This level is initialized to Integer.MIN_VALUE. */
int ALL = Integer.MIN_VALUE;
/**
* Dynamically set the ESAPI logging severity level. All events of this level and higher will be logged from
* this point forward for all logs. All events below this level will be discarded.
*
* @param level The level to set the logging level to.
*/
void setLevel(int level);
/** Retrieve the current ESAPI logging level for this logger.
*
* @return The current logging level.
*/
int getESAPILevel();
/**
* Log a fatal event if 'fatal' level logging is enabled.
*
* @param type
* the type of event
* @param message
* the message to log
*/
void fatal(EventType type, String message);
/**
* Log a fatal level security event if 'fatal' level logging is enabled
* and also record the stack trace associated with the event.
*
* @param type
* the type of event
* @param message
* the message to log
* @param throwable
* the exception to be logged
*/
void fatal(EventType type, String message, Throwable throwable);
/**
* Allows the caller to determine if messages logged at this level
* will be discarded, to avoid performing expensive processing.
*
* @return true if fatal level messages will be output to the log
*/
boolean isFatalEnabled();
/**
* Log an error level security event if 'error' level logging is enabled.
*
* @param type
* the type of event
* @param message
* the message to log
*/
void error(EventType type, String message);
/**
* Log an error level security event if 'error' level logging is enabled
* and also record the stack trace associated with the event.
*
* @param type
* the type of event
* @param message
* the message to log
* @param throwable
* the exception to be logged
*/
void error(EventType type, String message, Throwable throwable);
/**
* Allows the caller to determine if messages logged at this level
* will be discarded, to avoid performing expensive processing.
*
* @return true if error level messages will be output to the log
*/
boolean isErrorEnabled();
/**
* Log a warning level security event if 'warning' level logging is enabled.
*
* @param type
* the type of event
* @param message
* the message to log
*/
void warning(EventType type, String message);
/**
* Log a warning level security event if 'warning' level logging is enabled
* and also record the stack trace associated with the event.
*
* @param type
* the type of event
* @param message
* the message to log
* @param throwable
* the exception to be logged
*/
void warning(EventType type, String message, Throwable throwable);
/**
* Allows the caller to determine if messages logged at this level
* will be discarded, to avoid performing expensive processing.
*
* @return true if warning level messages will be output to the log
*/
boolean isWarningEnabled();
/**
* Log an info level security event if 'info' level logging is enabled.
*
* @param type
* the type of event
* @param message
* the message to log
*/
void info(EventType type, String message);
/**
* Log an info level security event if 'info' level logging is enabled
* and also record the stack trace associated with the event.
*
* @param type
* the type of event
* @param message
* the message to log
* @param throwable
* the exception to be logged
*/
void info(EventType type, String message, Throwable throwable);
/**
* Allows the caller to determine if messages logged at this level
* will be discarded, to avoid performing expensive processing.
*
* @return true if info level messages will be output to the log
*/
boolean isInfoEnabled();
/**
* Log a debug level security event if 'debug' level logging is enabled.
*
* @param type
* the type of event
* @param message
* the message to log
*/
void debug(EventType type, String message);
/**
* Log a debug level security event if 'debug' level logging is enabled
* and also record the stack trace associated with the event.
*
* @param type
* the type of event
* @param message
* the message to log
* @param throwable
* the exception to be logged
*/
void debug(EventType type, String message, Throwable throwable);
/**
* Allows the caller to determine if messages logged at this level
* will be discarded, to avoid performing expensive processing.
*
* @return true if debug level messages will be output to the log
*/
boolean isDebugEnabled();
/**
* Log a trace level security event if 'trace' level logging is enabled.
*
* @param type
* the type of event
* @param message
* the message to log
*/
void trace(EventType type, String message);
/**
* Log a trace level security event if 'trace' level logging is enabled
* and also record the stack trace associated with the event.
*
* @param type
* the type of event
* @param message
* the message to log
* @param throwable
* the exception to be logged
*/
void trace(EventType type, String message, Throwable throwable);
/**
* Allows the caller to determine if messages logged at this level
* will be discarded, to avoid performing expensive processing.
*
* @return true if trace level messages will be output to the log
*/
boolean isTraceEnabled();
/**
* Log an event regardless of what logging level is enabled.
* <br>
* Note that logging will not occur if the underlying logging implementation has logging disabled.
*
* @param type
* the type of event
* @param message
* the message to log
*/
void always(EventType type, String message);
/**
* Log an event regardless of what logging level is enabled
* and also record the stack trace associated with the event.
* <br>
* Note that logging will not occur if the underlying logging implementation has logging disabled.
*
* @param type
* the type of event
* @param message
* the message to log
* @param throwable
* the exception to be logged
*/
void always(EventType type, String message, Throwable throwable);
}