0
# Core Context Management
1
2
The LoggerContext is the central anchor for the Log4j Core logging system, managing all loggers, configuration, and lifecycle operations. It provides the primary interface for obtaining loggers and managing configuration changes dynamically.
3
4
## Capabilities
5
6
### LoggerContext Management
7
8
Central management class for the entire logging system.
9
10
```java { .api }
11
/**
12
* Get the current LoggerContext for the calling thread
13
* @return Current LoggerContext instance
14
*/
15
public static LoggerContext getContext();
16
17
/**
18
* Get LoggerContext with current context flag
19
* @param currentContext If true, returns context for current classloader
20
* @return LoggerContext instance
21
*/
22
public static LoggerContext getContext(boolean currentContext);
23
24
/**
25
* Get LoggerContext with specific parameters
26
* @param loader ClassLoader to use for context selection
27
* @param currentContext Whether to use current context
28
* @param configLocation URI of configuration file
29
* @return LoggerContext instance
30
*/
31
public static LoggerContext getContext(ClassLoader loader, boolean currentContext, URI configLocation);
32
33
/**
34
* Start the LoggerContext with current configuration
35
*/
36
public void start();
37
38
/**
39
* Start the LoggerContext with specific configuration
40
* @param config Configuration to use
41
*/
42
public void start(Configuration config);
43
44
/**
45
* Stop the LoggerContext with timeout
46
* @param timeout Maximum time to wait
47
* @param timeUnit Time unit for timeout
48
* @return true if stopped within timeout
49
*/
50
public boolean stop(long timeout, TimeUnit timeUnit);
51
```
52
53
**Usage Examples:**
54
55
```java
56
import org.apache.logging.log4j.core.LoggerContext;
57
import java.net.URI;
58
import java.util.concurrent.TimeUnit;
59
60
// Get default context
61
LoggerContext context = LoggerContext.getContext();
62
63
// Get context with specific configuration
64
URI configURI = new URI("file:///path/to/log4j2.xml");
65
LoggerContext customContext = LoggerContext.getContext(
66
MyClass.class.getClassLoader(),
67
false,
68
configURI
69
);
70
71
// Start and stop context
72
context.start();
73
boolean stopped = context.stop(30, TimeUnit.SECONDS);
74
```
75
76
### Logger Management
77
78
Managing individual loggers within the context.
79
80
```java { .api }
81
/**
82
* Get logger by name
83
* @param name Logger name
84
* @return Logger instance
85
*/
86
public Logger getLogger(String name);
87
88
/**
89
* Get logger with custom message factory
90
* @param name Logger name
91
* @param messageFactory Message factory for the logger
92
* @return Logger instance
93
*/
94
public Logger getLogger(String name, MessageFactory messageFactory);
95
96
/**
97
* Get all loggers in this context
98
* @return Collection of all loggers
99
*/
100
public Collection<Logger> getLoggers();
101
```
102
103
**Usage Examples:**
104
105
```java
106
// Get specific loggers
107
Logger rootLogger = context.getLogger("");
108
Logger appLogger = context.getLogger("com.example.MyApp");
109
Logger serviceLogger = context.getLogger("com.example.service.MyService");
110
111
// Get all loggers
112
Collection<Logger> allLoggers = context.getLoggers();
113
System.out.println("Total loggers: " + allLoggers.size());
114
```
115
116
### Configuration Management
117
118
Dynamic configuration management within the context.
119
120
```java { .api }
121
/**
122
* Get current configuration
123
* @return Current Configuration instance
124
*/
125
public Configuration getConfiguration();
126
127
/**
128
* Set new configuration
129
* @param config New configuration to set
130
* @return Previous configuration
131
*/
132
public Configuration setConfiguration(Configuration config);
133
134
/**
135
* Reconfigure using existing configuration location
136
*/
137
public void reconfigure();
138
139
/**
140
* Reconfigure with specific configuration
141
* @param configuration Configuration to use for reconfiguration
142
*/
143
public void reconfigure(Configuration configuration);
144
```
145
146
**Usage Examples:**
147
148
```java
149
// Get current configuration
150
Configuration currentConfig = context.getConfiguration();
151
System.out.println("Current config: " + currentConfig.getName());
152
153
// Reconfigure from file
154
context.reconfigure();
155
156
// Set new configuration
157
Configuration newConfig = new DefaultConfiguration();
158
Configuration oldConfig = context.setConfiguration(newConfig);
159
```
160
161
### Filter Management
162
163
Global filter management for the context.
164
165
```java { .api }
166
/**
167
* Add a global filter to the context
168
* @param filter Filter to add
169
*/
170
public void addFilter(Filter filter);
171
172
/**
173
* Remove a global filter from the context
174
* @param filter Filter to remove
175
*/
176
public void removeFilter(Filter filter);
177
```
178
179
**Usage Examples:**
180
181
```java
182
import org.apache.logging.log4j.core.filter.ThresholdFilter;
183
import org.apache.logging.log4j.Level;
184
185
// Add global threshold filter
186
Filter thresholdFilter = ThresholdFilter.createFilter(Level.WARN, Filter.Result.ACCEPT, Filter.Result.DENY);
187
context.addFilter(thresholdFilter);
188
189
// Remove filter later
190
context.removeFilter(thresholdFilter);
191
```
192
193
### Context State Management
194
195
Managing the lifecycle state of the context.
196
197
```java { .api }
198
/**
199
* Check if context is started
200
* @return true if context is started
201
*/
202
public boolean isStarted();
203
204
/**
205
* Check if context is stopped
206
* @return true if context is stopped
207
*/
208
public boolean isStopped();
209
210
/**
211
* Get current lifecycle state
212
* @return Current LifeCycle.State
213
*/
214
public LifeCycle.State getState();
215
```
216
217
## Configurator Utility
218
219
Static utility class for common configuration operations.
220
221
```java { .api }
222
/**
223
* Initialize logger context with parameters
224
* @param name Context name
225
* @param loader ClassLoader to use
226
* @param configLocation Configuration file location
227
* @return Initialized LoggerContext
228
*/
229
public static LoggerContext initialize(String name, ClassLoader loader, String configLocation);
230
231
/**
232
* Shutdown a logger context
233
* @param context LoggerContext to shutdown
234
*/
235
public static void shutdown(LoggerContext context);
236
237
/**
238
* Set level for specific logger
239
* @param loggerName Logger name
240
* @param level Level to set
241
*/
242
public static void setLevel(String loggerName, Level level);
243
244
/**
245
* Set root logger level
246
* @param level Level to set for root logger
247
*/
248
public static void setRootLevel(Level level);
249
250
/**
251
* Set level for logger and all descendants
252
* @param loggerName Logger name
253
* @param level Level to set
254
*/
255
public static void setAllLevels(String loggerName, Level level);
256
```
257
258
**Usage Examples:**
259
260
```java
261
import org.apache.logging.log4j.core.config.Configurator;
262
import org.apache.logging.log4j.Level;
263
264
// Initialize with custom configuration
265
LoggerContext ctx = Configurator.initialize("MyApp", null, "log4j2-custom.xml");
266
267
// Set levels programmatically
268
Configurator.setRootLevel(Level.INFO);
269
Configurator.setLevel("com.example", Level.DEBUG);
270
Configurator.setLevel("com.example.verbose", Level.TRACE);
271
272
// Shutdown when done
273
Configurator.shutdown(ctx);
274
```