or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

fluent-logging.mdindex.mdmarker-logging.mdmarker-management.mdmdc-support.mdstandard-logging.md

index.mddocs/

0

# Log4j SLF4J2 Implementation

1

2

The Log4j SLF4J2 Implementation is a provider (binding) that bridges SLF4J 2.x logging calls to the Apache Log4j API. It enables applications using SLF4J 2.x to leverage Log4j 2 as the underlying logging implementation, providing access to advanced SLF4J 2 features like fluent logging API, structured data, and caller information detection while benefiting from Log4j 2's high-performance architecture and rich configuration capabilities.

3

4

## Package Information

5

6

- **Package Name**: org.apache.logging.log4j:log4j-slf4j2-impl

7

- **Package Type**: maven

8

- **Language**: Java

9

- **Installation**: Add to Maven dependencies:

10

11

```xml

12

<dependency>

13

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

14

<artifactId>log4j-slf4j2-impl</artifactId>

15

<version>2.25.1</version>

16

</dependency>

17

```

18

19

Gradle:

20

```gradle

21

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

22

```

23

24

## Core Imports

25

26

```java

27

import org.slf4j.Logger;

28

import org.slf4j.LoggerFactory;

29

import org.slf4j.Marker;

30

import org.slf4j.MarkerFactory;

31

import org.slf4j.MDC;

32

import org.slf4j.spi.LoggingEventBuilder;

33

```

34

35

## Basic Usage

36

37

```java

38

import org.slf4j.Logger;

39

import org.slf4j.LoggerFactory;

40

import org.slf4j.Marker;

41

import org.slf4j.MarkerFactory;

42

43

public class MyApplication {

44

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

45

46

public void doWork() {

47

// Basic logging

48

logger.info("Application starting");

49

logger.debug("Debug information: {}", someValue);

50

51

// Marker-based logging

52

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

53

logger.warn(important, "Something significant happened");

54

55

// Fluent logging API (SLF4J 2.x)

56

logger.atInfo()

57

.addKeyValue("userId", userId)

58

.addKeyValue("operation", "login")

59

.log("User authentication successful");

60

}

61

}

62

```

63

64

## Architecture

65

66

The implementation consists of several key components:

67

68

- **Service Provider**: `SLF4JServiceProvider` automatically registers with SLF4J through Java ServiceLoader mechanism

69

- **Logger Factory**: `Log4jLoggerFactory` creates and manages logger instances backed by Log4j

70

- **Logger Implementation**: `Log4jLogger` provides complete SLF4J Logger interface with Log4j backend

71

- **Marker System**: `Log4jMarkerFactory` and `Log4jMarker` provide SLF4J marker support using Log4j markers

72

- **MDC Support**: `Log4jMDCAdapter` implements SLF4J MDC using Log4j's ThreadContext

73

- **Fluent API**: `Log4jEventBuilder` provides SLF4J 2.x fluent logging interface

74

- **Message Processing**: `ThrowableConsumingMessageFactory` handles advanced message formatting with throwable extraction

75

76

## Capabilities

77

78

### Standard Logging

79

80

Core SLF4J logging functionality with all standard log levels (TRACE, DEBUG, INFO, WARN, ERROR) and parameterized messages.

81

82

```java { .api }

83

// Standard logging methods for all levels

84

void trace(String msg);

85

void trace(String format, Object arg);

86

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

87

void trace(String format, Object... arguments);

88

void trace(String msg, Throwable t);

89

90

// Level checking methods

91

boolean isTraceEnabled();

92

boolean isDebugEnabled();

93

boolean isInfoEnabled();

94

boolean isWarnEnabled();

95

boolean isErrorEnabled();

96

```

97

98

[Standard Logging](./standard-logging.md)

99

100

### Marker-Based Logging

101

102

Enhanced logging with markers for categorization and filtering of log events.

103

104

```java { .api }

105

// Marker-based logging methods (available for all levels)

106

void info(Marker marker, String msg);

107

void info(Marker marker, String format, Object arg);

108

void info(Marker marker, String format, Object arg1, Object arg2);

109

void info(Marker marker, String format, Object... arguments);

110

void info(Marker marker, String msg, Throwable t);

111

112

// Level checking with markers

113

boolean isInfoEnabled(Marker marker);

114

```

115

116

[Marker-Based Logging](./marker-logging.md)

117

118

### Fluent Logging API

119

120

SLF4J 2.x fluent interface for building complex log events with structured data and lazy evaluation.

121

122

```java { .api }

123

LoggingEventBuilder atTrace();

124

LoggingEventBuilder atDebug();

125

LoggingEventBuilder atInfo();

126

LoggingEventBuilder atWarn();

127

LoggingEventBuilder atError();

128

129

interface LoggingEventBuilder {

130

LoggingEventBuilder setCause(Throwable cause);

131

LoggingEventBuilder addMarker(Marker marker);

132

LoggingEventBuilder addArgument(Object p);

133

LoggingEventBuilder addArgument(Supplier<?> objectSupplier);

134

LoggingEventBuilder addKeyValue(String key, Object value);

135

LoggingEventBuilder addKeyValue(String key, Supplier<Object> valueSupplier);

136

LoggingEventBuilder setMessage(String message);

137

LoggingEventBuilder setMessage(Supplier<String> messageSupplier);

138

void log();

139

void log(String message);

140

void log(String message, Object arg);

141

void log(String message, Object arg0, Object arg1);

142

void log(String message, Object... args);

143

void log(Supplier<String> messageSupplier);

144

}

145

```

146

147

[Fluent Logging API](./fluent-logging.md)

148

149

### MDC (Mapped Diagnostic Context)

150

151

Thread-local context management for adding contextual information to log events.

152

153

```java { .api }

154

// Basic MDC operations

155

void put(String key, String val);

156

String get(String key);

157

void remove(String key);

158

void clear();

159

Map<String, String> getCopyOfContextMap();

160

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

161

162

// Stack-based MDC operations

163

void pushByKey(String key, String value);

164

String popByKey(String key);

165

Deque<String> getCopyOfDequeByKey(String key);

166

void clearDequeByKey(String key);

167

```

168

169

[MDC Support](./mdc-support.md)

170

171

### Marker Management

172

173

Creation and management of hierarchical markers for log event categorization.

174

175

```java { .api }

176

Marker getMarker(String name);

177

boolean exists(String name);

178

boolean detachMarker(String name);

179

Marker getDetachedMarker(String name);

180

181

interface Marker {

182

String getName();

183

void add(Marker reference);

184

boolean remove(Marker reference);

185

boolean hasChildren();

186

boolean hasReferences();

187

Iterator<Marker> iterator();

188

boolean contains(Marker other);

189

boolean contains(String name);

190

}

191

```

192

193

[Marker Management](./marker-management.md)

194

195

## Exception Types

196

197

```java { .api }

198

class SLF4JLoggingException extends RuntimeException {

199

SLF4JLoggingException(String msg);

200

SLF4JLoggingException(String msg, Exception ex);

201

SLF4JLoggingException(Exception ex);

202

}

203

```

204

205

## Service Provider Integration

206

207

This implementation automatically integrates with SLF4J through the Java ServiceLoader mechanism. The `SLF4JServiceProvider` is automatically discovered and provides all required SLF4J factories and adapters.

208

209

```java { .api }

210

class SLF4JServiceProvider implements org.slf4j.spi.SLF4JServiceProvider {

211

String REQUESTED_API_VERSION = "2.0.99";

212

213

ILoggerFactory getLoggerFactory();

214

IMarkerFactory getMarkerFactory();

215

MDCAdapter getMDCAdapter();

216

String getRequestedApiVersion();

217

void initialize();

218

}

219

```

220

221

## Advanced Message Processing

222

223

The implementation includes specialized message processing for compatibility with different logging frameworks:

224

225

```java { .api }

226

class ThrowableConsumingMessageFactory implements MessageFactory2 {

227

Message newMessage(Object message);

228

Message newMessage(String message);

229

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

230

Message newMessage(String message, Object p0);

231

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

232

// ... additional overloads for up to 10 parameters

233

}

234

```

235

236

This factory implements Logback's algorithm for determining when the last parameter should be treated as a throwable rather than a message parameter.