0
# Core Logging
1
2
The core logging functionality provides the primary interfaces for application logging through Logger, LogManager, Level, and Category classes.
3
4
## Primary Classes
5
6
### Logger
7
8
```java { .api }
9
public class Logger extends Category {
10
// Static factory methods
11
public static Logger getLogger(Class clazz);
12
public static Logger getLogger(String name);
13
public static Logger getRootLogger();
14
15
// Level checking methods
16
public boolean isTraceEnabled();
17
public boolean isDebugEnabled();
18
public boolean isInfoEnabled();
19
20
// Trace logging methods
21
public void trace(Object message);
22
public void trace(Object message, Throwable t);
23
}
24
```
25
26
**Parameters:**
27
- `clazz` - Class object used to determine logger name
28
- `name` - String name of the logger
29
- `message` - Log message object (toString() will be called)
30
- `t` - Throwable for stack trace logging
31
32
**Returns:**
33
- `Logger` instance for the specified class or name
34
- `boolean` indicating if the specified level is enabled
35
36
### LogManager
37
38
```java { .api }
39
public final class LogManager {
40
// Logger retrieval
41
public static Logger getLogger(Class clazz);
42
public static Logger getLogger(String name);
43
public static Logger getRootLogger();
44
public static Logger exists(String name);
45
46
// Logger enumeration
47
public static Enumeration getCurrentLoggers();
48
49
// Configuration management
50
public static void resetConfiguration();
51
public static void shutdown();
52
53
// Configuration constants
54
public static final String DEFAULT_CONFIGURATION_FILE;
55
public static final String DEFAULT_CONFIGURATION_KEY;
56
}
57
```
58
59
**Parameters:**
60
- `clazz` - Class object for logger name derivation
61
- `name` - String name of the logger to retrieve or check
62
63
**Returns:**
64
- `Logger` instance or null if not found (exists method)
65
- `Enumeration` of current logger instances
66
67
### Category (Legacy)
68
69
```java { .api }
70
public class Category implements AppenderAttachable {
71
// Static factory methods
72
public static Category getInstance(String name);
73
public static Category getRoot();
74
75
// Level methods
76
public Level getLevel();
77
public void setLevel(Level level);
78
public Priority getPriority();
79
public void setPriority(Priority priority);
80
81
// Logging methods
82
public void debug(Object message);
83
public void debug(Object message, Throwable t);
84
public void info(Object message);
85
public void info(Object message, Throwable t);
86
public void warn(Object message);
87
public void warn(Object message, Throwable t);
88
public void error(Object message);
89
public void error(Object message, Throwable t);
90
public void fatal(Object message);
91
public void fatal(Object message, Throwable t);
92
public void log(Priority priority, Object message);
93
public void log(Priority priority, Object message, Throwable t);
94
95
// Level checking
96
public boolean isDebugEnabled();
97
public boolean isInfoEnabled();
98
public boolean isEnabledFor(Priority level);
99
100
// Appender management
101
public void addAppender(Appender appender);
102
public void removeAppender(Appender appender);
103
public void removeAllAppenders();
104
public Enumeration getAllAppenders();
105
public Appender getAppender(String name);
106
public boolean isAttached(Appender appender);
107
}
108
```
109
110
**Parameters:**
111
- `name` - String name for the category
112
- `level` - Level object to set
113
- `priority` - Priority object to set
114
- `message` - Log message object
115
- `t` - Throwable for exception logging
116
- `appender` - Appender to add/remove
117
118
**Returns:**
119
- `Category` instance
120
- `Level` or `Priority` object
121
- `boolean` for level checks and appender operations
122
- `Enumeration` of appenders
123
- `Appender` instance or null
124
125
## Log Levels
126
127
### Level
128
129
```java { .api }
130
public class Level extends Priority implements Serializable {
131
// Standard levels
132
public static final Level OFF;
133
public static final Level FATAL;
134
public static final Level ERROR;
135
public static final Level WARN;
136
public static final Level INFO;
137
public static final Level DEBUG;
138
public static final Level TRACE;
139
public static final Level ALL;
140
141
// Level constants
142
public static final int TRACE_INT = 5000;
143
144
// Conversion methods
145
public static Level toLevel(String sArg);
146
public static Level toLevel(int val);
147
public static Level toLevel(String sArg, Level defaultLevel);
148
public static Level toLevel(int val, Level defaultLevel);
149
}
150
```
151
152
**Parameters:**
153
- `sArg` - String representation of level (e.g., "DEBUG", "INFO")
154
- `val` - Integer value of level
155
- `defaultLevel` - Default level to return if conversion fails
156
157
**Returns:**
158
- `Level` object corresponding to the input
159
- Default level if conversion fails
160
161
### Priority (Legacy)
162
163
```java { .api }
164
public class Priority {
165
// Priority constants
166
public static final int OFF_INT;
167
public static final int FATAL_INT;
168
public static final int ERROR_INT;
169
public static final int WARN_INT;
170
public static final int INFO_INT;
171
public static final int DEBUG_INT;
172
public static final int ALL_INT;
173
174
// Legacy priority objects (deprecated)
175
public static final Priority FATAL;
176
public static final Priority ERROR;
177
public static final Priority WARN;
178
public static final Priority INFO;
179
public static final Priority DEBUG;
180
181
// Comparison methods
182
public boolean isGreaterOrEqual(Priority r);
183
184
// Conversion methods
185
public int toInt();
186
public String toString();
187
188
// Deprecated methods
189
@Deprecated
190
public static Priority toPriority(String sArg);
191
@Deprecated
192
public static Priority toPriority(int val);
193
}
194
```
195
196
**Parameters:**
197
- `r` - Priority object to compare against
198
- `sArg` - String representation of priority
199
- `val` - Integer value of priority
200
201
**Returns:**
202
- `boolean` for comparison operations
203
- `int` priority level value
204
- `String` representation of priority
205
- `Priority` object (deprecated methods)
206
207
## Usage Examples
208
209
### Basic Logger Usage
210
```java
211
import org.apache.log4j.Logger;
212
213
public class MyService {
214
private static final Logger logger = Logger.getLogger(MyService.class);
215
216
public void processData(String data) {
217
logger.info("Processing data: " + data);
218
219
try {
220
// Process the data
221
if (logger.isDebugEnabled()) {
222
logger.debug("Data processed successfully");
223
}
224
} catch (Exception e) {
225
logger.error("Failed to process data", e);
226
throw e;
227
}
228
}
229
}
230
```
231
232
### Level Checking and Conditional Logging
233
```java
234
import org.apache.log4j.Logger;
235
import org.apache.log4j.Level;
236
237
public class PerformanceService {
238
private static final Logger logger = Logger.getLogger(PerformanceService.class);
239
240
public void expensiveOperation() {
241
if (logger.isDebugEnabled()) {
242
String debugInfo = buildExpensiveDebugString();
243
logger.debug("Starting operation with: " + debugInfo);
244
}
245
246
// Check specific levels
247
if (logger.isEnabledFor(Level.TRACE)) {
248
logger.trace("Detailed trace information");
249
}
250
}
251
}
252
```
253
254
### Category Usage (Legacy)
255
```java
256
import org.apache.log4j.Category;
257
import org.apache.log4j.Priority;
258
259
public class LegacyService {
260
private static final Category cat = Category.getInstance("LegacyService");
261
262
public void performTask() {
263
cat.log(Priority.INFO, "Task started");
264
265
try {
266
// Task implementation
267
cat.debug("Task in progress");
268
} catch (Exception e) {
269
cat.error("Task failed", e);
270
}
271
}
272
}
273
```