0
# Core Logging
1
2
Primary logging functionality providing logger creation, level checking, and message logging at all standard Log4j levels. All logging operations delegate to the underlying SLF4J implementation.
3
4
## Capabilities
5
6
### Logger Creation
7
8
Factory methods for creating logger instances by name, class, or accessing the root logger.
9
10
```java { .api }
11
/**
12
* Gets a logger instance by name
13
* @param name The logger name
14
* @return Logger instance
15
*/
16
public static Logger getLogger(String name);
17
18
/**
19
* Gets a logger instance by class name
20
* @param clazz The class to create logger for
21
* @return Logger instance
22
*/
23
public static Logger getLogger(Class<?> clazz);
24
25
/**
26
* Gets a logger instance with custom factory
27
* @param name The logger name
28
* @param loggerFactory Custom logger factory
29
* @return Logger instance
30
*/
31
public static Logger getLogger(String name, LoggerFactory loggerFactory);
32
33
/**
34
* Gets the root logger instance
35
* @return Root logger
36
*/
37
public static Logger getRootLogger();
38
```
39
40
**Usage Examples:**
41
42
```java
43
import org.apache.log4j.Logger;
44
import org.apache.log4j.LogManager;
45
46
// Get logger by class
47
Logger logger = Logger.getLogger(MyClass.class);
48
49
// Get logger by name
50
Logger namedLogger = Logger.getLogger("com.example.MyLogger");
51
52
// Get logger via LogManager (equivalent)
53
Logger managerLogger = LogManager.getLogger(MyClass.class);
54
55
// Get root logger
56
Logger rootLogger = Logger.getRootLogger();
57
```
58
59
### Level Checking
60
61
Methods to check if logging is enabled at specific levels before performing expensive operations.
62
63
```java { .api }
64
/**
65
* Check if TRACE level is enabled
66
* @return true if TRACE enabled
67
*/
68
public boolean isTraceEnabled();
69
70
/**
71
* Check if DEBUG level is enabled
72
* @return true if DEBUG enabled
73
*/
74
public boolean isDebugEnabled();
75
76
/**
77
* Check if INFO level is enabled
78
* @return true if INFO enabled
79
*/
80
public boolean isInfoEnabled();
81
82
/**
83
* Check if WARN level is enabled
84
* @return true if WARN enabled
85
*/
86
public boolean isWarnEnabled();
87
88
/**
89
* Check if ERROR level is enabled
90
* @return true if ERROR enabled
91
*/
92
public boolean isErrorEnabled();
93
94
/**
95
* Check if specific priority is enabled
96
* @param p Priority to check
97
* @return true if enabled for priority
98
*/
99
public boolean isEnabledFor(Priority p);
100
```
101
102
**Usage Examples:**
103
104
```java
105
// Check before expensive operations
106
if (logger.isDebugEnabled()) {
107
logger.debug("Complex data: " + computeExpensiveDebugInfo());
108
}
109
110
// Check different levels
111
if (logger.isInfoEnabled()) {
112
logger.info("Processing item " + item.getId());
113
}
114
115
// Check with Priority
116
if (logger.isEnabledFor(Level.WARN)) {
117
logger.warn("Warning condition detected");
118
}
119
```
120
121
### Basic Logging Methods
122
123
Core logging methods for each standard Log4j level, accepting both objects and throwables.
124
125
```java { .api }
126
/**
127
* Log message at TRACE level
128
* @param message Message to log
129
*/
130
public void trace(Object message);
131
132
/**
133
* Log message at TRACE level with throwable
134
* @param message Message to log
135
* @param t Throwable to log
136
*/
137
public void trace(Object message, Throwable t);
138
139
/**
140
* Log message at DEBUG level
141
* @param message Message to log
142
*/
143
public void debug(Object message);
144
145
/**
146
* Log message at DEBUG level with throwable
147
* @param message Message to log
148
* @param t Throwable to log
149
*/
150
public void debug(Object message, Throwable t);
151
152
/**
153
* Log message at INFO level
154
* @param message Message to log
155
*/
156
public void info(Object message);
157
158
/**
159
* Log message at INFO level with throwable
160
* @param message Message to log
161
* @param t Throwable to log
162
*/
163
public void info(Object message, Throwable t);
164
165
/**
166
* Log message at WARN level
167
* @param message Message to log
168
*/
169
public void warn(Object message);
170
171
/**
172
* Log message at WARN level with throwable
173
* @param message Message to log
174
* @param t Throwable to log
175
*/
176
public void warn(Object message, Throwable t);
177
178
/**
179
* Log message at ERROR level
180
* @param message Message to log
181
*/
182
public void error(Object message);
183
184
/**
185
* Log message at ERROR level with throwable
186
* @param message Message to log
187
* @param t Throwable to log
188
*/
189
public void error(Object message, Throwable t);
190
191
/**
192
* Log message at TRACE level
193
* @param message Message to log
194
*/
195
public void trace(Object message);
196
197
/**
198
* Log message at TRACE level with throwable
199
* @param message Message to log
200
* @param t Throwable to log
201
*/
202
public void trace(Object message, Throwable t);
203
204
/**
205
* Log message at FATAL level (mapped to ERROR in SLF4J with FATAL marker)
206
* @param message Message to log
207
*/
208
public void fatal(Object message);
209
210
/**
211
* Log message at FATAL level with throwable
212
* @param message Message to log
213
* @param t Throwable to log
214
*/
215
public void fatal(Object message, Throwable t);
216
```
217
218
**Usage Examples:**
219
220
```java
221
Logger logger = Logger.getLogger(MyClass.class);
222
223
// Basic logging at different levels
224
logger.trace("Entering method processData()");
225
logger.debug("Processing " + items.size() + " items");
226
logger.info("Successfully processed batch");
227
logger.warn("Low memory condition detected");
228
logger.error("Failed to process item");
229
logger.fatal("System is shutting down");
230
231
// Logging with exceptions
232
try {
233
processData();
234
} catch (DataProcessingException e) {
235
logger.error("Data processing failed", e);
236
} catch (CriticalSystemException e) {
237
logger.fatal("Critical system failure", e);
238
}
239
```
240
241
### Generic Logging Methods
242
243
Generic logging methods that accept Priority/Level parameters for dynamic level selection.
244
245
```java { .api }
246
/**
247
* Log message at specified priority
248
* @param p Priority level
249
* @param message Message to log
250
*/
251
public void log(Priority p, Object message);
252
253
/**
254
* Log message at specified priority with throwable
255
* @param p Priority level
256
* @param message Message to log
257
* @param t Throwable to log
258
*/
259
public void log(Priority p, Object message, Throwable t);
260
261
/**
262
* Log message with FQCN at specified priority
263
* @param FQCN Fully qualified class name
264
* @param p Priority level
265
* @param message Message to log
266
* @param t Throwable to log
267
*/
268
public void log(String FQCN, Priority p, Object message, Throwable t);
269
```
270
271
**Usage Examples:**
272
273
```java
274
// Dynamic level logging
275
Priority level = determineLogLevel();
276
logger.log(level, "Dynamic message at runtime level");
277
278
// With exception
279
try {
280
operation();
281
} catch (Exception e) {
282
logger.log(Level.ERROR, "Operation failed", e);
283
}
284
```
285
286
### Logger Hierarchy Methods
287
288
Methods for accessing logger hierarchy information and properties.
289
290
```java { .api }
291
/**
292
* Get logger name
293
* @return Logger name
294
*/
295
public String getName();
296
297
/**
298
* Get parent logger (always returns null in this implementation)
299
* @return Parent logger or null
300
*/
301
public final Category getParent();
302
303
/**
304
* Get effective level based on SLF4J logger state
305
* @return Effective Level
306
*/
307
public Level getEffectiveLevel();
308
309
/**
310
* Get assigned level (always returns null in this implementation)
311
* @return Assigned level or null
312
*/
313
public final Level getLevel();
314
315
/**
316
* Get appender by name (always returns null in this implementation)
317
* @param name Appender name
318
* @return Appender or null
319
*/
320
public Appender getAppender(String name);
321
322
/**
323
* Get all appenders (always returns empty enumeration in this implementation)
324
* @return Empty enumeration
325
*/
326
public Enumeration getAllAppenders();
327
328
/**
329
* Get additivity flag (always returns false in this implementation)
330
* @return Additivity flag
331
*/
332
public boolean getAdditivity();
333
```
334
335
**Usage Examples:**
336
337
```java
338
Logger logger = Logger.getLogger("com.example.MyClass");
339
340
// Get logger information
341
String name = logger.getName(); // "com.example.MyClass"
342
Level effectiveLevel = logger.getEffectiveLevel(); // Current effective level
343
Category parent = logger.getParent(); // null
344
```
345
346
### Conditional Logging
347
348
Utility method for assertion-based logging.
349
350
```java { .api }
351
/**
352
* Log error message if assertion fails
353
* @param assertion Boolean assertion to check
354
* @param msg Error message to log if assertion fails
355
*/
356
public void assertLog(boolean assertion, String msg);
357
```
358
359
**Usage Examples:**
360
361
```java
362
// Assertion-based logging
363
logger.assertLog(data != null, "Data cannot be null");
364
logger.assertLog(connection.isValid(), "Database connection is invalid");
365
```
366
367
### Appender Management
368
369
Methods for managing appenders attached to loggers (compatibility methods that return null or empty collections).
370
371
```java { .api }
372
/**
373
* Get appender by name (always returns null in this implementation)
374
* @param name Appender name to lookup
375
* @return Appender instance or null
376
*/
377
public Appender getAppender(String name);
378
379
/**
380
* Get all appenders (always returns empty enumeration)
381
* @return Enumeration of appenders (empty)
382
*/
383
@SuppressWarnings("rawtypes")
384
public Enumeration getAllAppenders();
385
386
/**
387
* Get priority (deprecated method, always returns null)
388
* @return Priority or null
389
* @deprecated Use getLevel() instead
390
*/
391
@Deprecated
392
public final Level getPriority();
393
```
394
395
**Usage Examples:**
396
397
```java
398
Logger logger = Logger.getLogger(MyClass.class);
399
400
// Appender management (compatibility methods)
401
Appender consoleAppender = logger.getAppender("console"); // Always null
402
Enumeration<?> appenders = logger.getAllAppenders(); // Always empty
403
404
// Deprecated priority method
405
Level priority = logger.getPriority(); // Always null, use getLevel() instead
406
```