or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

tessl/maven-org-apache-logging-log4j--log4j-slf4j-impl

SLF4J 1 binding that forwards SLF4J logging calls to the Log4j 2 API

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
mavenpkg:maven/org.apache.logging.log4j/log4j-slf4j-impl@2.25.x

To install, run

npx @tessl/cli install tessl/maven-org-apache-logging-log4j--log4j-slf4j-impl@2.25.0

0

# Log4j SLF4J Implementation

1

2

Log4j SLF4J Implementation is a binding library that enables applications using the SLF4J 1.x logging facade to seamlessly integrate with Apache Log4j 2's logging implementation. It acts as a bridge between the SLF4J API and Log4j 2's core functionality, forwarding all SLF4J logging calls to the Log4j 2 API while preserving logging levels, markers, and MDC (Mapped Diagnostic Context) information.

3

4

## Package Information

5

6

- **Package Name**: log4j-slf4j-impl

7

- **Package Type**: Maven

8

- **Group ID**: org.apache.logging.log4j

9

- **Artifact ID**: log4j-slf4j-impl

10

- **Language**: Java

11

- **Installation**: Add to Maven `pom.xml`:

12

13

```xml

14

<dependency>

15

<groupId>org.apache.logging.log4j</groupId>

16

<artifactId>log4j-slf4j-impl</artifactId>

17

<version>2.25.1</version>

18

</dependency>

19

```

20

21

For Gradle:

22

23

```gradle

24

implementation 'org.apache.logging.log4j:log4j-slf4j-impl:2.25.1'

25

```

26

27

## Core Imports

28

29

```java

30

import org.slf4j.Logger;

31

import org.slf4j.LoggerFactory;

32

import org.slf4j.Marker;

33

import org.slf4j.MarkerFactory;

34

import org.slf4j.MDC;

35

```

36

37

## Basic Usage

38

39

```java

40

import org.slf4j.Logger;

41

import org.slf4j.LoggerFactory;

42

import org.slf4j.Marker;

43

import org.slf4j.MarkerFactory;

44

import org.slf4j.MDC;

45

46

public class Example {

47

private static final Logger logger = LoggerFactory.getLogger(Example.class);

48

49

public void demonstrateLogging() {

50

// Basic logging

51

logger.info("Application started");

52

logger.debug("Debug message with parameter: {}", "value");

53

logger.error("Error occurred", new RuntimeException("Example exception"));

54

55

// Marker-based logging

56

Marker importantMarker = MarkerFactory.getMarker("IMPORTANT");

57

logger.warn(importantMarker, "Important warning message");

58

59

// MDC (Mapped Diagnostic Context) usage

60

MDC.put("userId", "12345");

61

MDC.put("sessionId", "abc-def-ghi");

62

logger.info("User action performed"); // Will include MDC context

63

MDC.clear();

64

}

65

}

66

```

67

68

## Architecture

69

70

The log4j-slf4j-impl library implements the SLF4J Service Provider Interface (SPI) pattern, providing three key binding components that SLF4J automatically discovers at runtime:

71

72

- **StaticLoggerBinder**: Entry point for logger factory binding, manages logger creation through Log4j

73

- **StaticMarkerBinder**: Entry point for marker factory binding, handles marker creation and management

74

- **StaticMDCBinder**: Entry point for MDC adapter binding, delegates MDC operations to Log4j's ThreadContext

75

76

The implementation classes provide full SLF4J compatibility while leveraging Log4j 2's advanced features like asynchronous logging, flexible configuration, and high performance. This design ensures applications can migrate from other logging frameworks to Log4j 2 without requiring code changes at the application layer.

77

78

## Capabilities

79

80

### SLF4J Logger Factory Binding

81

82

The binding entry point that enables SLF4J to use Log4j 2 as the underlying logging implementation.

83

84

```java { .api }

85

public final class StaticLoggerBinder implements LoggerFactoryBinder {

86

public static String REQUESTED_API_VERSION = "1.6";

87

public static StaticLoggerBinder getSingleton();

88

public ILoggerFactory getLoggerFactory();

89

public String getLoggerFactoryClassStr();

90

}

91

```

92

93

### SLF4J Marker Factory Binding

94

95

The binding entry point for marker operations, delegating to Log4j's marker system.

96

97

```java { .api }

98

public class StaticMarkerBinder implements MarkerFactoryBinder {

99

public static final StaticMarkerBinder SINGLETON;

100

public static StaticMarkerBinder getSingleton();

101

public IMarkerFactory getMarkerFactory();

102

public String getMarkerFactoryClassStr();

103

}

104

```

105

106

### SLF4J MDC Binding

107

108

The binding entry point for Mapped Diagnostic Context operations, delegating to Log4j's ThreadContext.

109

110

```java { .api }

111

public final class StaticMDCBinder {

112

public static final StaticMDCBinder SINGLETON;

113

public static StaticMDCBinder getSingleton();

114

public MDCAdapter getMDCA();

115

public String getMDCAdapterClassStr();

116

}

117

```

118

119

### Log4j Logger Factory Implementation

120

121

Log4j implementation of SLF4J's ILoggerFactory interface, responsible for creating and managing logger instances.

122

123

```java { .api }

124

public class Log4jLoggerFactory extends AbstractLoggerAdapter<Logger> implements ILoggerFactory {

125

public Log4jLoggerFactory(Log4jMarkerFactory markerFactory);

126

Log4jMarkerFactory getMarkerFactory();

127

// Inherited from ILoggerFactory:

128

// public Logger getLogger(String name);

129

}

130

```

131

132

### Log4j Logger Implementation

133

134

SLF4J logger implementation that forwards all logging calls to Log4j's ExtendedLogger.

135

136

```java { .api }

137

public class Log4jLogger implements LocationAwareLogger, Serializable {

138

public static final String FQCN = "org.apache.logging.slf4j.Log4jLogger";

139

140

// Trace level logging

141

public void trace(String format);

142

public void trace(String format, Object arg);

143

public void trace(String format, Object arg1, Object arg2);

144

public void trace(String format, Object... args);

145

public void trace(String format, Throwable t);

146

public boolean isTraceEnabled();

147

public boolean isTraceEnabled(Marker marker);

148

public void trace(Marker marker, String s);

149

public void trace(Marker marker, String s, Object o);

150

public void trace(Marker marker, String s, Object o, Object o1);

151

public void trace(Marker marker, String s, Object... objects);

152

public void trace(Marker marker, String s, Throwable throwable);

153

154

// Debug level logging

155

public void debug(String format);

156

public void debug(String format, Object arg);

157

public void debug(String format, Object arg1, Object arg2);

158

public void debug(String format, Object... args);

159

public void debug(String format, Throwable t);

160

public boolean isDebugEnabled();

161

public boolean isDebugEnabled(Marker marker);

162

public void debug(Marker marker, String s);

163

public void debug(Marker marker, String s, Object o);

164

public void debug(Marker marker, String s, Object o, Object o1);

165

public void debug(Marker marker, String s, Object... objects);

166

public void debug(Marker marker, String s, Throwable throwable);

167

168

// Info level logging

169

public void info(String format);

170

public void info(String format, Object arg);

171

public void info(String format, Object arg1, Object arg2);

172

public void info(String format, Object... args);

173

public void info(String format, Throwable t);

174

public boolean isInfoEnabled();

175

public boolean isInfoEnabled(Marker marker);

176

public void info(Marker marker, String s);

177

public void info(Marker marker, String s, Object o);

178

public void info(Marker marker, String s, Object o, Object o1);

179

public void info(Marker marker, String s, Object... objects);

180

public void info(Marker marker, String s, Throwable throwable);

181

182

// Warn level logging

183

public void warn(String format);

184

public void warn(String format, Object arg);

185

public void warn(String format, Object arg1, Object arg2);

186

public void warn(String format, Object... args);

187

public void warn(String format, Throwable t);

188

public boolean isWarnEnabled();

189

public boolean isWarnEnabled(Marker marker);

190

public void warn(Marker marker, String s);

191

public void warn(Marker marker, String s, Object o);

192

public void warn(Marker marker, String s, Object o, Object o1);

193

public void warn(Marker marker, String s, Object... objects);

194

public void warn(Marker marker, String s, Throwable throwable);

195

196

// Error level logging

197

public void error(String format);

198

public void error(String format, Object arg);

199

public void error(String format, Object arg1, Object arg2);

200

public void error(String format, Object... args);

201

public void error(String format, Throwable t);

202

public boolean isErrorEnabled();

203

public boolean isErrorEnabled(Marker marker);

204

public void error(Marker marker, String s);

205

public void error(Marker marker, String s, Object o);

206

public void error(Marker marker, String s, Object o, Object o1);

207

public void error(Marker marker, String s, Object... objects);

208

public void error(Marker marker, String s, Throwable throwable);

209

210

// LocationAwareLogger interface

211

public void log(Marker marker, String fqcn, int level, String message, Object[] params, Throwable throwable);

212

213

// Logger interface

214

public String getName();

215

}

216

```

217

218

### Log4j Marker Factory Implementation

219

220

Log4j/SLF4J bridge for creating and managing SLF4J Markers based on Log4j markers.

221

222

```java { .api }

223

public class Log4jMarkerFactory implements IMarkerFactory {

224

public Marker getMarker(String name);

225

public Marker getMarker(Marker marker);

226

public boolean exists(String name);

227

public boolean detachMarker(String name); // Always returns false

228

public Marker getDetachedMarker(String name); // Returns regular marker

229

}

230

```

231

232

### Log4j Marker Implementation

233

234

SLF4J Marker implementation that bridges to Log4j's marker system.

235

236

```java { .api }

237

public class Log4jMarker implements Marker {

238

public static final long serialVersionUID = 1590472L;

239

240

public void add(Marker marker);

241

public boolean contains(Marker marker);

242

public boolean contains(String s);

243

public String getName();

244

public boolean hasChildren();

245

public boolean hasReferences();

246

public Iterator<Marker> iterator();

247

public boolean remove(Marker marker);

248

public boolean equals(Object obj);

249

public int hashCode();

250

}

251

```

252

253

### Log4j MDC Adapter Implementation

254

255

MDC adapter that delegates all operations to Log4j's ThreadContext.

256

257

```java { .api }

258

public class Log4jMDCAdapter implements MDCAdapter {

259

public void put(String key, String val);

260

public String get(String key);

261

public void remove(String key);

262

public void clear();

263

public Map<String, String> getCopyOfContextMap();

264

public void setContextMap(Map<String, String> map);

265

}

266

```

267

268

### SLF4J Logging Exception

269

270

Exception thrown when the SLF4J adapter encounters problems.

271

272

```java { .api }

273

public class SLF4JLoggingException extends RuntimeException {

274

public SLF4JLoggingException(String msg);

275

public SLF4JLoggingException(String msg, Exception ex);

276

public SLF4JLoggingException(Exception ex);

277

}

278

```

279

280

### Throwable Consuming Message Factory

281

282

Message factory that eagerly removes trailing throwable arguments following Logback's algorithm.

283

284

```java { .api }

285

public final class ThrowableConsumingMessageFactory implements MessageFactory2 {

286

public Message newMessage(Object message);

287

public Message newMessage(String message);

288

public Message newMessage(String message, Object... params);

289

public Message newMessage(CharSequence charSequence);

290

public Message newMessage(String message, Object p0);

291

public Message newMessage(String message, Object p0, Object p1);

292

public Message newMessage(String message, Object p0, Object p1, Object p2);

293

public Message newMessage(String message, Object p0, Object p1, Object p2, Object p3);

294

public Message newMessage(String message, Object p0, Object p1, Object p2, Object p3, Object p4);

295

public Message newMessage(String message, Object p0, Object p1, Object p2, Object p3, Object p4, Object p5);

296

public Message newMessage(String message, Object p0, Object p1, Object p2, Object p3, Object p4, Object p5, Object p6);

297

public Message newMessage(String message, Object p0, Object p1, Object p2, Object p3, Object p4, Object p5, Object p6, Object p7);

298

public Message newMessage(String message, Object p0, Object p1, Object p2, Object p3, Object p4, Object p5, Object p6, Object p7, Object p8);

299

public Message newMessage(String message, Object p0, Object p1, Object p2, Object p3, Object p4, Object p5, Object p6, Object p7, Object p8, Object p9);

300

}

301

```

302

303

## Types

304

305

### SLF4J Standard Types

306

307

These are the standard SLF4J interfaces that the binding implementations provide:

308

309

```java { .api }

310

// From SLF4J API

311

interface Logger {

312

String ROOT_LOGGER_NAME = "ROOT";

313

String getName();

314

// Logging methods (implemented by Log4jLogger)

315

}

316

317

interface LocationAwareLogger extends Logger {

318

int TRACE_INT = 00;

319

int DEBUG_INT = 10;

320

int INFO_INT = 20;

321

int WARN_INT = 30;

322

int ERROR_INT = 40;

323

324

void log(Marker marker, String fqcn, int level, String message, Object[] argArray, Throwable t);

325

}

326

327

interface Marker extends Serializable {

328

String getName();

329

void add(Marker reference);

330

boolean remove(Marker reference);

331

boolean hasChildren();

332

boolean hasReferences();

333

Iterator<Marker> iterator();

334

boolean contains(Marker other);

335

boolean contains(String name);

336

boolean equals(Object o);

337

int hashCode();

338

}

339

340

interface ILoggerFactory {

341

Logger getLogger(String name);

342

}

343

344

interface IMarkerFactory {

345

Marker getMarker(String name);

346

boolean exists(String name);

347

boolean detachMarker(String name);

348

Marker getDetachedMarker(String name);

349

}

350

351

interface MDCAdapter {

352

void put(String key, String val);

353

String get(String key);

354

void remove(String key);

355

void clear();

356

Map<String, String> getCopyOfContextMap();

357

void setContextMap(Map<String, String> contextMap);

358

}

359

```

360

361

### Log4j 2 Core Types

362

363

These are key Log4j 2 types that the implementation uses internally:

364

365

```java { .api }

366

// From Log4j 2 API (used internally)

367

interface ExtendedLogger {

368

void logIfEnabled(String fqcn, Level level, org.apache.logging.log4j.Marker marker,

369

String message, Object... params);

370

boolean isEnabled(Level level, org.apache.logging.log4j.Marker marker, String message);

371

void logMessage(String fqcn, Level level, org.apache.logging.log4j.Marker marker,

372

Message message, Throwable t);

373

}

374

375

interface MessageFactory2 {

376

Message newMessage(Object message);

377

Message newMessage(String message);

378

Message newMessage(String message, Object... params);

379

// Additional overloads...

380

}

381

382

interface Message {

383

String getFormattedMessage();

384

String getFormat();

385

Object[] getParameters();

386

Throwable getThrowable();

387

}

388

```

389

390

## Important Notes

391

392

- **Mutual Exclusivity**: This library cannot coexist with `log4j-to-slf4j` (the reverse bridge) in the same classpath as they serve opposite purposes

393

- **Automatic Discovery**: SLF4J automatically discovers and uses this binding when it's present on the classpath

394

- **MDC Integration**: All MDC operations are transparently forwarded to Log4j's ThreadContext

395

- **Marker Compatibility**: SLF4J markers are converted to Log4j markers preserving parent-child relationships

396

- **Serialization Support**: Log4jLogger supports Java serialization for distributed logging scenarios

397

- **Location Awareness**: The binding preserves caller location information for accurate log source reporting

398

- **Performance**: Leverages Log4j 2's high-performance logging implementation including asynchronous logging capabilities