0
# Configuration
1
2
Configuration utilities providing Log4j 1.x API compatibility for basic and property-based configuration. All configuration classes are implemented as no-op since actual configuration is handled by the underlying SLF4J implementation (e.g., Logback).
3
4
## Capabilities
5
6
### Basic Configuration
7
8
Simple configuration utilities for basic logging setup. These methods maintain API compatibility but perform no actual configuration.
9
10
```java { .api }
11
/**
12
* Basic configuration setup (no-op implementation)
13
*/
14
public static void configure();
15
16
/**
17
* Configure with a specific appender (no-op implementation)
18
* @param appender Appender to configure with
19
*/
20
public static void configure(Appender appender);
21
22
/**
23
* Reset logging configuration (no-op implementation)
24
*/
25
public static void resetConfiguration();
26
```
27
28
**Usage Examples:**
29
30
```java
31
import org.apache.log4j.BasicConfigurator;
32
import org.apache.log4j.ConsoleAppender;
33
import org.apache.log4j.SimpleLayout;
34
35
// Basic configuration (no-op, but maintains API compatibility)
36
BasicConfigurator.configure();
37
38
// Configure with console appender (no-op)
39
ConsoleAppender appender = new ConsoleAppender(new SimpleLayout());
40
BasicConfigurator.configure(appender);
41
42
// Reset configuration (no-op)
43
BasicConfigurator.resetConfiguration();
44
45
// Note: Actual configuration must be done through SLF4J implementation
46
// For example, with Logback, use logback.xml or logback-spring.xml
47
```
48
49
### Property Configuration
50
51
Property-based configuration system implementing the Configurator interface. All methods are no-op implementations.
52
53
```java { .api }
54
/**
55
* PropertyConfigurator implementing Configurator interface
56
*/
57
public class PropertyConfigurator implements Configurator {
58
59
/**
60
* Configure from Properties object (no-op)
61
* @param properties Properties for configuration
62
*/
63
public static void configure(Properties properties);
64
65
/**
66
* Configure from properties file (no-op)
67
* @param configFilename Path to properties file
68
*/
69
public static void configure(String configFilename);
70
71
/**
72
* Configure from URL (no-op)
73
* @param configURL URL to properties file
74
*/
75
public static void configure(URL configURL);
76
77
/**
78
* Configure and watch file for changes (no-op)
79
* @param configFilename Path to properties file
80
*/
81
public static void configureAndWatch(String configFilename);
82
83
/**
84
* Configure and watch file for changes with delay (no-op)
85
* @param configFilename Path to properties file
86
* @param delay Watch delay in milliseconds
87
*/
88
public static void configureAndWatch(String configFilename, long delay);
89
90
/**
91
* Configure from Properties with logger repository (no-op)
92
* @param properties Properties for configuration
93
* @param hierarchy Logger repository
94
*/
95
public void doConfigure(Properties properties, LoggerRepository hierarchy);
96
97
/**
98
* Configure from file with logger repository (no-op)
99
* @param configFileName Path to properties file
100
* @param hierarchy Logger repository
101
*/
102
public void doConfigure(String configFileName, LoggerRepository hierarchy);
103
104
/**
105
* Configure from URL with logger repository (no-op)
106
* @param configURL URL to properties file
107
* @param hierarchy Logger repository
108
*/
109
public void doConfigure(URL configURL, LoggerRepository hierarchy);
110
}
111
```
112
113
**Usage Examples:**
114
115
```java
116
import org.apache.log4j.PropertyConfigurator;
117
import java.util.Properties;
118
import java.net.URL;
119
120
// Configure from properties file (no-op)
121
PropertyConfigurator.configure("log4j.properties");
122
123
// Configure from Properties object (no-op)
124
Properties props = new Properties();
125
props.setProperty("log4j.rootLogger", "INFO, console");
126
PropertyConfigurator.configure(props);
127
128
// Configure from URL (no-op)
129
URL configURL = new URL("http://example.com/log4j.properties");
130
PropertyConfigurator.configure(configURL);
131
132
// Configure and watch for changes (no-op)
133
PropertyConfigurator.configureAndWatch("log4j.properties");
134
PropertyConfigurator.configureAndWatch("log4j.properties", 60000); // Check every minute
135
136
// Instance-based configuration (no-op)
137
PropertyConfigurator configurator = new PropertyConfigurator();
138
configurator.doConfigure("log4j.properties", null);
139
```
140
141
### XML Configuration
142
143
XML-based configuration using DOM parser. Implemented as no-op.
144
145
```java { .api }
146
/**
147
* XML-based configuration using DOM parser (no-op implementation)
148
*/
149
public class DOMConfigurator {
150
// All methods are no-op implementations
151
}
152
```
153
154
**Usage Examples:**
155
156
```java
157
import org.apache.log4j.xml.DOMConfigurator;
158
159
// XML configuration would be no-op
160
// DOMConfigurator.configure("log4j.xml"); // No-op
161
```
162
163
### LogManager Configuration
164
165
Configuration methods available through LogManager. These methods are also no-op implementations.
166
167
```java { .api }
168
/**
169
* Get current loggers (returns empty enumeration)
170
* @return Empty enumeration
171
*/
172
public static Enumeration<?> getCurrentLoggers();
173
174
/**
175
* Shutdown logging system (no-op)
176
*/
177
public static void shutdown();
178
179
/**
180
* Reset logging configuration (no-op)
181
*/
182
public static void resetConfiguration();
183
```
184
185
**Usage Examples:**
186
187
```java
188
import org.apache.log4j.LogManager;
189
import java.util.Enumeration;
190
191
// Get current loggers (returns empty enumeration)
192
Enumeration<?> loggers = LogManager.getCurrentLoggers();
193
194
// Shutdown logging (no-op)
195
LogManager.shutdown();
196
197
// Reset configuration (no-op)
198
LogManager.resetConfiguration();
199
```
200
201
### SLF4J Configuration Guide
202
203
Since log4j-over-slf4j delegates to SLF4J, actual configuration must be done through the SLF4J implementation.
204
205
**Logback Configuration Example:**
206
207
Create `logback.xml` in your classpath:
208
209
```xml
210
<?xml version="1.0" encoding="UTF-8"?>
211
<configuration>
212
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
213
<encoder>
214
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
215
</encoder>
216
</appender>
217
218
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
219
<file>application.log</file>
220
<encoder>
221
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
222
</encoder>
223
</appender>
224
225
<logger name="com.example" level="DEBUG"/>
226
<logger name="org.springframework" level="INFO"/>
227
228
<root level="INFO">
229
<appender-ref ref="CONSOLE"/>
230
<appender-ref ref="FILE"/>
231
</root>
232
</configuration>
233
```
234
235
**SLF4J Simple Configuration:**
236
237
For development, you can use slf4j-simple with `simplelogger.properties`:
238
239
```properties
240
org.slf4j.simpleLogger.defaultLogLevel=info
241
org.slf4j.simpleLogger.log.com.example=debug
242
org.slf4j.simpleLogger.showDateTime=true
243
org.slf4j.simpleLogger.dateTimeFormat=yyyy-MM-dd HH:mm:ss:SSS
244
org.slf4j.simpleLogger.showThreadName=true
245
org.slf4j.simpleLogger.showLogName=true
246
org.slf4j.simpleLogger.showShortLogName=false
247
```
248
249
**Migration Notes:**
250
251
```java
252
// Old Log4j 1.x configuration pattern
253
public class OldApp {
254
static {
255
// This would configure Log4j directly
256
PropertyConfigurator.configure("log4j.properties");
257
}
258
}
259
260
// New pattern with log4j-over-slf4j
261
public class NewApp {
262
static {
263
// Configuration is done through SLF4J implementation
264
// No programmatic configuration needed - use logback.xml, etc.
265
266
// These calls are no-op but maintain compatibility
267
PropertyConfigurator.configure("log4j.properties"); // No-op
268
BasicConfigurator.configure(); // No-op
269
}
270
}
271
```
272
273
### Configuration Migration
274
275
Guidelines for migrating from Log4j 1.x configuration to SLF4J-based configuration.
276
277
**Log4j Properties to Logback XML:**
278
279
```properties
280
# Old log4j.properties
281
log4j.rootLogger=INFO, console, file
282
log4j.appender.console=org.apache.log4j.ConsoleAppender
283
log4j.appender.console.layout=org.apache.log4j.PatternLayout
284
log4j.appender.console.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
285
```
286
287
Equivalent Logback configuration:
288
289
```xml
290
<configuration>
291
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
292
<encoder>
293
<pattern>%d [%t] %-5p %c - %m%n</pattern>
294
</encoder>
295
</appender>
296
297
<root level="INFO">
298
<appender-ref ref="console"/>
299
</root>
300
</configuration>
301
```