0
# Log4j-over-SLF4J
1
2
Log4j-over-SLF4J is a compatibility layer that implements the Apache Log4j 1.x API over SLF4J (Simple Logging Facade for Java). It allows applications originally written to use Log4j 1.x to seamlessly work with SLF4J-based logging implementations without requiring code changes. The library provides drop-in replacement classes for all major Log4j components, delegating their functionality to corresponding SLF4J components.
3
4
## Package Information
5
6
- **Package Name**: log4j-over-slf4j
7
- **Package Type**: maven
8
- **Language**: Java
9
- **Group ID**: org.slf4j
10
- **Artifact ID**: log4j-over-slf4j
11
- **Installation**: Add to Maven dependencies:
12
```xml
13
<dependency>
14
<groupId>org.slf4j</groupId>
15
<artifactId>log4j-over-slf4j</artifactId>
16
<version>2.0.17</version>
17
</dependency>
18
```
19
20
## Core Imports
21
22
```java
23
import org.apache.log4j.Logger;
24
import org.apache.log4j.LogManager;
25
import org.apache.log4j.Level;
26
```
27
28
For diagnostic contexts:
29
```java
30
import org.apache.log4j.MDC;
31
import org.apache.log4j.NDC;
32
```
33
34
## Basic Usage
35
36
```java
37
import org.apache.log4j.Logger;
38
import org.apache.log4j.Level;
39
40
// Get a logger instance
41
Logger logger = Logger.getLogger(MyClass.class);
42
43
// Or get by name
44
Logger namedLogger = Logger.getLogger("com.example.MyLogger");
45
46
// Log at different levels
47
logger.debug("Debug message");
48
logger.info("Info message");
49
logger.warn("Warning message");
50
logger.error("Error message");
51
logger.fatal("Fatal message");
52
53
// Log with exceptions
54
try {
55
// some operation
56
} catch (Exception e) {
57
logger.error("Operation failed", e);
58
}
59
60
// Check if level is enabled
61
if (logger.isDebugEnabled()) {
62
logger.debug("Expensive debug operation: " + computeExpensiveData());
63
}
64
```
65
66
## Architecture
67
68
Log4j-over-SLF4J is designed as a bridge between Log4j 1.x API and SLF4J implementations:
69
70
- **API Compatibility**: Provides complete Log4j 1.x API compatibility without changing application code
71
- **SLF4J Delegation**: All logging operations delegate to underlying SLF4J logger instances
72
- **Configuration Passthrough**: Configuration is handled by the SLF4J implementation (e.g., Logback)
73
- **No-op Implementations**: Configuration classes like `PropertyConfigurator` are no-op since configuration is handled by SLF4J
74
- **Context Bridging**: MDC and NDC implementations use SLF4J's MDC mechanism
75
76
## Capabilities
77
78
### Core Logging
79
80
Primary logging functionality including logger creation, level checking, and message logging at all standard Log4j levels (TRACE, DEBUG, INFO, WARN, ERROR, FATAL).
81
82
```java { .api }
83
// Logger creation
84
public static Logger getLogger(String name);
85
public static Logger getLogger(Class<?> clazz);
86
public static Logger getRootLogger();
87
88
// Level checking
89
public boolean isDebugEnabled();
90
public boolean isInfoEnabled();
91
public boolean isWarnEnabled();
92
public boolean isErrorEnabled();
93
94
// Basic logging methods
95
public void debug(Object message);
96
public void info(Object message);
97
public void warn(Object message);
98
public void error(Object message);
99
public void fatal(Object message);
100
```
101
102
[Core Logging](./core-logging.md)
103
104
### Level Management
105
106
Level and priority system providing standard Log4j levels (OFF, FATAL, ERROR, WARN, INFO, DEBUG, TRACE, ALL) with conversion utilities and level comparison methods.
107
108
```java { .api }
109
// Standard levels
110
public static final Level OFF;
111
public static final Level FATAL;
112
public static final Level ERROR;
113
public static final Level WARN;
114
public static final Level INFO;
115
public static final Level DEBUG;
116
public static final Level TRACE;
117
public static final Level ALL;
118
119
// Level conversion
120
public static Level toLevel(String sArg);
121
public static Level toLevel(int val);
122
public static Level toLevel(String sArg, Level defaultLevel);
123
```
124
125
[Level Management](./level-management.md)
126
127
### Diagnostic Contexts
128
129
Mapped Diagnostic Context (MDC) and Nested Diagnostic Context (NDC) for adding contextual information to log messages across thread boundaries.
130
131
```java { .api }
132
// MDC operations
133
public static void put(String key, String value);
134
public static Object get(String key);
135
public static void remove(String key);
136
public static void clear();
137
138
// NDC operations
139
public static void push(String message);
140
public static String pop();
141
public static String peek();
142
public static void clear();
143
public static int getDepth();
144
```
145
146
[Diagnostic Contexts](./diagnostic-contexts.md)
147
148
### Configuration
149
150
Configuration utilities providing Log4j 1.x API compatibility for basic and property-based configuration (implemented as no-op since configuration is handled by SLF4J).
151
152
```java { .api }
153
// Basic configuration
154
public static void configure();
155
public static void configure(Appender appender);
156
157
// Property configuration
158
public static void configure(Properties properties);
159
public static void configure(String configFilename);
160
public static void configureAndWatch(String configFilename);
161
```
162
163
[Configuration](./configuration.md)
164
165
### Appenders and Layouts
166
167
Appender interfaces and skeleton implementations for output destinations, plus layout classes for message formatting (mostly no-op implementations since output handling is managed by SLF4J).
168
169
```java { .api }
170
// Appender interface
171
public interface Appender {
172
void doAppend(LoggingEvent event);
173
String getName();
174
void setName(String name);
175
boolean requiresLayout();
176
void close();
177
}
178
179
// Common appenders
180
public class ConsoleAppender extends WriterAppender;
181
public class FileAppender extends WriterAppender;
182
public class RollingFileAppender extends FileAppender;
183
```
184
185
[Appenders and Layouts](./appenders-layouts.md)
186
187
## Types
188
189
```java { .api }
190
// Core logger hierarchy
191
public class Logger extends Category {
192
protected Logger(String name);
193
}
194
195
public class Category {
196
protected final String name;
197
protected org.slf4j.Logger slf4jLogger;
198
}
199
200
// Level and Priority
201
public class Level extends Priority implements Serializable {
202
protected Level(int level, String levelStr, int syslogEquivalent);
203
}
204
205
public class Priority {
206
transient int level;
207
transient String levelStr;
208
transient int syslogEquivalent;
209
}
210
211
// Service Provider Interfaces
212
public interface LoggerFactory {
213
public Logger makeNewLoggerInstance(String name);
214
}
215
216
public interface Appender {
217
void doAppend(LoggingEvent event);
218
String getName();
219
void setName(String name);
220
Layout getLayout();
221
void setLayout(Layout layout);
222
boolean requiresLayout();
223
void close();
224
}
225
```