or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

index.md

index.mddocs/

0

# SLF4J Reload4j Binding

1

2

SLF4J Reload4j Binding provides a bridge between the SLF4J (Simple Logging Facade for Java) API and the Reload4j logging framework (successor to Log4j 1.2.x). This binding allows applications using SLF4J to delegate logging operations to Reload4j as the underlying logging implementation.

3

4

## Package Information

5

6

- **Package Name**: org.slf4j:slf4j-reload4j

7

- **Package Type**: maven

8

- **Language**: Java

9

- **Installation**: `<dependency><groupId>org.slf4j</groupId><artifactId>slf4j-reload4j</artifactId><version>1.7.36</version></dependency>`

10

11

## Core Imports

12

13

```java

14

import org.slf4j.Logger;

15

import org.slf4j.LoggerFactory;

16

import org.slf4j.MDC;

17

import org.slf4j.Marker;

18

import org.slf4j.ILoggerFactory;

19

import org.slf4j.spi.MDCAdapter;

20

import org.slf4j.spi.LocationAwareLogger;

21

import org.slf4j.event.LoggingEvent;

22

```

23

24

## Basic Usage

25

26

```java

27

import org.slf4j.Logger;

28

import org.slf4j.LoggerFactory;

29

import org.slf4j.MDC;

30

31

public class Example {

32

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

33

34

public void demonstrateLogging() {

35

// Basic logging at different levels

36

logger.trace("Trace level message");

37

logger.debug("Debug level message");

38

logger.info("Info level message");

39

logger.warn("Warning message");

40

logger.error("Error message");

41

42

// Parameterized logging (avoids string concatenation)

43

String username = "alice";

44

int userId = 123;

45

logger.info("User {} logged in with ID {}", username, userId);

46

47

// Exception logging

48

try {

49

// some operation

50

} catch (Exception e) {

51

logger.error("Operation failed for user {}", username, e);

52

}

53

54

// MDC (Mapped Diagnostic Context) usage

55

MDC.put("requestId", "req-456");

56

logger.info("Processing request");

57

MDC.remove("requestId");

58

}

59

}

60

```

61

62

## Architecture

63

64

The SLF4J Reload4j Binding operates through several key components:

65

66

- **Service Provider Interface**: Implements SLF4J's service provider pattern through static binder classes

67

- **Logger Adapter**: Wraps Reload4j loggers to provide SLF4J interface compatibility

68

- **Factory Pattern**: Manages logger instances with caching for performance

69

- **MDC Integration**: Bridges SLF4J's MDC with Reload4j's native MDC implementation

70

- **Location-Aware Logging**: Supports advanced logging features like caller location information

71

72

## Capabilities

73

74

### Logger Factory Management

75

76

Central factory for creating and managing logger instances with efficient caching.

77

78

```java { .api }

79

public class StaticLoggerBinder implements LoggerFactoryBinder {

80

/**

81

* Returns the singleton instance of the logger binder.

82

* @return StaticLoggerBinder singleton

83

*/

84

public static final StaticLoggerBinder getSingleton();

85

86

/**

87

* SLF4J API version this binding was compiled against.

88

*/

89

public static String REQUESTED_API_VERSION;

90

91

/**

92

* Returns the logger factory instance.

93

* @return ILoggerFactory implementation

94

*/

95

public ILoggerFactory getLoggerFactory();

96

97

/**

98

* Returns the class name of the logger factory.

99

* @return String representation of factory class

100

*/

101

public String getLoggerFactoryClassStr();

102

}

103

104

public class Reload4jLoggerFactory implements ILoggerFactory {

105

/**

106

* Returns a logger instance for the given name.

107

* @param name Logger name (typically class name)

108

* @return Logger instance

109

*/

110

public Logger getLogger(String name);

111

}

112

```

113

114

### Core Logging Operations

115

116

Complete logging interface supporting all standard levels with parameterized messages and exception handling.

117

118

```java { .api }

119

public final class Reload4jLoggerAdapter extends MarkerIgnoringBase

120

implements LocationAwareLogger, Serializable {

121

122

// Level checking methods

123

public boolean isTraceEnabled();

124

public boolean isDebugEnabled();

125

public boolean isInfoEnabled();

126

public boolean isWarnEnabled();

127

public boolean isErrorEnabled();

128

129

// TRACE level logging

130

public void trace(String msg);

131

public void trace(String format, Object arg);

132

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

133

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

134

public void trace(String msg, Throwable t);

135

136

// DEBUG level logging

137

public void debug(String msg);

138

public void debug(String format, Object arg);

139

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

140

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

141

public void debug(String msg, Throwable t);

142

143

// INFO level logging

144

public void info(String msg);

145

public void info(String format, Object arg);

146

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

147

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

148

public void info(String msg, Throwable t);

149

150

// WARN level logging

151

public void warn(String msg);

152

public void warn(String format, Object arg);

153

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

154

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

155

public void warn(String msg, Throwable t);

156

157

// ERROR level logging

158

public void error(String msg);

159

public void error(String format, Object arg);

160

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

161

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

162

public void error(String msg, Throwable t);

163

164

// Location-aware logging

165

public void log(Marker marker, String callerFQCN, int level, String msg, Object[] argArray, Throwable t);

166

public void log(LoggingEvent event);

167

}

168

```

169

170

### Mapped Diagnostic Context (MDC)

171

172

Thread-local storage for contextual information that appears in log messages.

173

174

```java { .api }

175

public class StaticMDCBinder {

176

/**

177

* Singleton instance for MDC binding.

178

*/

179

public static final StaticMDCBinder SINGLETON;

180

181

/**

182

* Returns the singleton instance.

183

* @return StaticMDCBinder singleton

184

*/

185

public static final StaticMDCBinder getSingleton();

186

187

/**

188

* Returns the MDC adapter instance.

189

* @return MDCAdapter implementation

190

*/

191

public MDCAdapter getMDCA();

192

193

/**

194

* Returns the class name of the MDC adapter.

195

* @return String representation of MDC adapter class

196

*/

197

public String getMDCAdapterClassStr();

198

}

199

200

public class Reload4jMDCAdapter implements MDCAdapter {

201

/**

202

* Clear all entries in the MDC for the current thread.

203

*/

204

public void clear();

205

206

/**

207

* Get the context value for the given key.

208

* @param key Context key

209

* @return Context value or null if not found

210

*/

211

public String get(String key);

212

213

/**

214

* Put a context value for the given key.

215

* @param key Context key (cannot be null)

216

* @param val Context value (cannot be null for Log4j)

217

*/

218

public void put(String key, String val);

219

220

/**

221

* Remove the context value for the given key.

222

* @param key Context key to remove

223

*/

224

public void remove(String key);

225

226

/**

227

* Get a copy of the current thread's context map.

228

* @return Map containing all context entries, or null if empty

229

*/

230

public Map getCopyOfContextMap();

231

232

/**

233

* Set the current thread's context map.

234

* @param contextMap Map of context entries to set

235

*/

236

public void setContextMap(Map contextMap);

237

}

238

```

239

240

### Marker Support

241

242

Marker factory binding for structured logging markers.

243

244

```java { .api }

245

public class StaticMarkerBinder implements MarkerFactoryBinder {

246

/**

247

* Singleton instance for marker binding.

248

*/

249

public static final StaticMarkerBinder SINGLETON;

250

251

/**

252

* Returns the singleton instance.

253

* @return StaticMarkerBinder singleton

254

*/

255

public static StaticMarkerBinder getSingleton();

256

257

/**

258

* Returns the marker factory instance.

259

* @return IMarkerFactory implementation (BasicMarkerFactory)

260

*/

261

public IMarkerFactory getMarkerFactory();

262

263

/**

264

* Returns the class name of the marker factory.

265

* @return String representation of marker factory class

266

*/

267

public String getMarkerFactoryClassStr();

268

}

269

```

270

271

## Types

272

273

Core type definitions referenced in the API:

274

275

```java { .api }

276

// From SLF4J API

277

interface ILoggerFactory {

278

Logger getLogger(String name);

279

}

280

281

interface MDCAdapter {

282

void clear();

283

String get(String key);

284

void put(String key, String val);

285

void remove(String key);

286

Map getCopyOfContextMap();

287

void setContextMap(Map contextMap);

288

}

289

290

interface LoggerFactoryBinder {

291

ILoggerFactory getLoggerFactory();

292

String getLoggerFactoryClassStr();

293

}

294

295

interface MarkerFactoryBinder {

296

IMarkerFactory getMarkerFactory();

297

String getMarkerFactoryClassStr();

298

}

299

300

interface LocationAwareLogger extends Logger {

301

int TRACE_INT = 0;

302

int DEBUG_INT = 10;

303

int INFO_INT = 20;

304

int WARN_INT = 30;

305

int ERROR_INT = 40;

306

307

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

308

}

309

310

interface LoggingEvent {

311

Level getLevel();

312

Marker getMarker();

313

String getLoggerName();

314

String getMessage();

315

String getThreadName();

316

Object[] getArgumentArray();

317

long getTimeStamp();

318

Throwable getThrowable();

319

}

320

321

enum Level {

322

ERROR, WARN, INFO, DEBUG, TRACE;

323

324

int toInt();

325

String toString();

326

}

327

328

// Standard Java types used

329

import java.util.Map;

330

import java.io.Serializable;

331

```

332

333

## Usage Examples

334

335

### Performance-Optimized Logging

336

337

```java

338

// Efficient parameterized logging (recommended)

339

logger.debug("Processing user {} with {} items", userId, itemCount);

340

341

// Avoid string concatenation (inefficient)

342

// logger.debug("Processing user " + userId + " with " + itemCount + " items");

343

344

// Level checking for expensive operations

345

if (logger.isDebugEnabled()) {

346

String expensiveDebugInfo = computeExpensiveDebugInfo();

347

logger.debug("Debug info: {}", expensiveDebugInfo);

348

}

349

```

350

351

### Exception Logging

352

353

```java

354

try {

355

performDatabaseOperation();

356

} catch (SQLException e) {

357

// Log exception with context

358

logger.error("Database operation failed for user {}", userId, e);

359

}

360

```

361

362

### MDC Usage

363

364

```java

365

// Set context information

366

MDC.put("userId", String.valueOf(userId));

367

MDC.put("sessionId", sessionId);

368

369

try {

370

// All log messages in this thread will include MDC context

371

logger.info("Starting user operation");

372

performUserOperation();

373

logger.info("User operation completed successfully");

374

} finally {

375

// Clean up MDC to prevent memory leaks

376

MDC.clear();

377

}

378

```

379

380

## Dependencies

381

382

This binding requires the following dependencies:

383

384

- **org.slf4j:slf4j-api** - SLF4J API interfaces

385

- **ch.qos.reload4j:reload4j** - Reload4j logging framework implementation

386

387

## OSGi Support

388

389

The binding includes OSGi metadata and functions as a fragment bundle:

390

391

- **Bundle-SymbolicName**: slf4j.reload4j

392

- **Export-Package**: org.slf4j.impl

393

- **Fragment-Host**: slf4j.api

394

395

## Configuration

396

397

This binding delegates all configuration to the underlying Reload4j framework. Configure logging behavior through standard Reload4j configuration files (log4j.properties or log4j.xml).