or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

basic-logging.mdfluent-logging.mdindex.mdmarkers.mdmdc.mdservice-providers.md

basic-logging.mddocs/

0

# Basic Logging

1

2

Traditional SLF4J logging interface providing methods for all standard log levels. This API has been stable since SLF4J 1.0 and remains the foundation of SLF4J logging.

3

4

## Capabilities

5

6

### Logger Factory

7

8

Static factory for obtaining Logger instances by name or class.

9

10

```java { .api }

11

/**

12

* Static factory class for obtaining Logger instances

13

*/

14

public final class LoggerFactory {

15

/**

16

* Return a logger named according to the name parameter

17

* @param name The name of the logger

18

* @return logger instance

19

*/

20

public static Logger getLogger(String name);

21

22

/**

23

* Return a logger named corresponding to the class passed as parameter

24

* @param clazz The returned logger will be named after clazz

25

* @return logger instance

26

*/

27

public static Logger getLogger(Class<?> clazz);

28

29

/**

30

* Return the ILoggerFactory instance in use

31

* @return the ILoggerFactory instance in use

32

*/

33

public static ILoggerFactory getILoggerFactory();

34

}

35

```

36

37

**Usage Examples:**

38

39

```java

40

import org.slf4j.Logger;

41

import org.slf4j.LoggerFactory;

42

43

public class MyService {

44

// Get logger by class - recommended approach

45

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

46

47

// Get logger by name

48

private static final Logger namedLogger = LoggerFactory.getLogger("com.example.custom");

49

50

public void processData() {

51

logger.info("Processing started");

52

// ... processing logic

53

logger.info("Processing completed");

54

}

55

}

56

```

57

58

### Logger Interface

59

60

Main logging interface with methods for all log levels.

61

62

```java { .api }

63

/**

64

* The main user entry point of SLF4J API

65

*/

66

public interface Logger {

67

/**

68

* Return the name of this Logger instance

69

* @return name of this logger instance

70

*/

71

String getName();

72

73

// Level checking methods

74

boolean isTraceEnabled();

75

boolean isDebugEnabled();

76

boolean isInfoEnabled();

77

boolean isWarnEnabled();

78

boolean isErrorEnabled();

79

}

80

```

81

82

### TRACE Level Logging

83

84

Finest level of logging, typically used for detailed diagnostic information.

85

86

```java { .api }

87

/**

88

* Is the logger instance enabled for the TRACE level?

89

* @return True if this Logger is enabled for the TRACE level, false otherwise

90

*/

91

boolean isTraceEnabled();

92

93

/**

94

* Log a message at the TRACE level

95

* @param msg the message string to be logged

96

*/

97

void trace(String msg);

98

99

/**

100

* Log a message at the TRACE level according to the specified format and argument

101

* @param format the format string

102

* @param arg the argument

103

*/

104

void trace(String format, Object arg);

105

106

/**

107

* Log a message at the TRACE level according to the specified format and arguments

108

* @param format the format string

109

* @param arg1 the first argument

110

* @param arg2 the second argument

111

*/

112

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

113

114

/**

115

* Log a message at the TRACE level according to the specified format and arguments

116

* @param format the format string

117

* @param arguments a list of 3 or more arguments

118

*/

119

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

120

121

/**

122

* Log an exception (throwable) at the TRACE level with an accompanying message

123

* @param msg the message accompanying the exception

124

* @param t the exception (throwable) to log

125

*/

126

void trace(String msg, Throwable t);

127

```

128

129

### DEBUG Level Logging

130

131

Detailed information for diagnosing problems, typically used during development.

132

133

```java { .api }

134

/**

135

* Is the logger instance enabled for the DEBUG level?

136

* @return True if this Logger is enabled for the DEBUG level, false otherwise

137

*/

138

boolean isDebugEnabled();

139

140

/**

141

* Log a message at the DEBUG level

142

* @param msg the message string to be logged

143

*/

144

void debug(String msg);

145

146

/**

147

* Log a message at the DEBUG level according to the specified format and argument

148

* @param format the format string

149

* @param arg the argument

150

*/

151

void debug(String format, Object arg);

152

153

/**

154

* Log a message at the DEBUG level according to the specified format and arguments

155

* @param format the format string

156

* @param arg1 the first argument

157

* @param arg2 the second argument

158

*/

159

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

160

161

/**

162

* Log a message at the DEBUG level according to the specified format and arguments

163

* @param format the format string

164

* @param arguments a list of 3 or more arguments

165

*/

166

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

167

168

/**

169

* Log an exception (throwable) at the DEBUG level with an accompanying message

170

* @param msg the message accompanying the exception

171

* @param t the exception (throwable) to log

172

*/

173

void debug(String msg, Throwable t);

174

```

175

176

### INFO Level Logging

177

178

General informational messages about application flow.

179

180

```java { .api }

181

/**

182

* Is the logger instance enabled for the INFO level?

183

* @return True if this Logger is enabled for the INFO level, false otherwise

184

*/

185

boolean isInfoEnabled();

186

187

/**

188

* Log a message at the INFO level

189

* @param msg the message string to be logged

190

*/

191

void info(String msg);

192

193

/**

194

* Log a message at the INFO level according to the specified format and argument

195

* @param format the format string

196

* @param arg the argument

197

*/

198

void info(String format, Object arg);

199

200

/**

201

* Log a message at the INFO level according to the specified format and arguments

202

* @param format the format string

203

* @param arg1 the first argument

204

* @param arg2 the second argument

205

*/

206

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

207

208

/**

209

* Log a message at the INFO level according to the specified format and arguments

210

* @param format the format string

211

* @param arguments a list of 3 or more arguments

212

*/

213

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

214

215

/**

216

* Log an exception (throwable) at the INFO level with an accompanying message

217

* @param msg the message accompanying the exception

218

* @param t the exception (throwable) to log

219

*/

220

void info(String msg, Throwable t);

221

```

222

223

### WARN Level Logging

224

225

Potentially harmful situations or deprecated API usage.

226

227

```java { .api }

228

/**

229

* Is the logger instance enabled for the WARN level?

230

* @return True if this Logger is enabled for the WARN level, false otherwise

231

*/

232

boolean isWarnEnabled();

233

234

/**

235

* Log a message at the WARN level

236

* @param msg the message string to be logged

237

*/

238

void warn(String msg);

239

240

/**

241

* Log a message at the WARN level according to the specified format and argument

242

* @param format the format string

243

* @param arg the argument

244

*/

245

void warn(String format, Object arg);

246

247

/**

248

* Log a message at the WARN level according to the specified format and arguments

249

* @param format the format string

250

* @param arg1 the first argument

251

* @param arg2 the second argument

252

*/

253

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

254

255

/**

256

* Log a message at the WARN level according to the specified format and arguments

257

* @param format the format string

258

* @param arguments a list of 3 or more arguments

259

*/

260

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

261

262

/**

263

* Log an exception (throwable) at the WARN level with an accompanying message

264

* @param msg the message accompanying the exception

265

* @param t the exception (throwable) to log

266

*/

267

void warn(String msg, Throwable t);

268

```

269

270

### ERROR Level Logging

271

272

Error events that might still allow the application to continue running.

273

274

```java { .api }

275

/**

276

* Is the logger instance enabled for the ERROR level?

277

* @return True if this Logger is enabled for the ERROR level, false otherwise

278

*/

279

boolean isErrorEnabled();

280

281

/**

282

* Log a message at the ERROR level

283

* @param msg the message string to be logged

284

*/

285

void error(String msg);

286

287

/**

288

* Log a message at the ERROR level according to the specified format and argument

289

* @param format the format string

290

* @param arg the argument

291

*/

292

void error(String format, Object arg);

293

294

/**

295

* Log a message at the ERROR level according to the specified format and arguments

296

* @param format the format string

297

* @param arg1 the first argument

298

* @param arg2 the second argument

299

*/

300

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

301

302

/**

303

* Log a message at the ERROR level according to the specified format and arguments

304

* @param format the format string

305

* @param arguments a list of 3 or more arguments

306

*/

307

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

308

309

/**

310

* Log an exception (throwable) at the ERROR level with an accompanying message

311

* @param msg the message accompanying the exception

312

* @param t the exception (throwable) to log

313

*/

314

void error(String msg, Throwable t);

315

```

316

317

**Usage Examples:**

318

319

```java

320

import org.slf4j.Logger;

321

import org.slf4j.LoggerFactory;

322

323

public class UserService {

324

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

325

326

public User findUser(String userId) {

327

// Check if debug logging is enabled before expensive operations

328

if (logger.isDebugEnabled()) {

329

logger.debug("Searching for user with ID: {}", userId);

330

}

331

332

try {

333

User user = userRepository.findById(userId);

334

if (user == null) {

335

logger.warn("User not found: {}", userId);

336

return null;

337

}

338

339

logger.info("User retrieved successfully: {}", user.getName());

340

return user;

341

342

} catch (DatabaseException e) {

343

logger.error("Database error while retrieving user {}", userId, e);

344

throw new ServiceException("Failed to retrieve user", e);

345

}

346

}

347

348

public void processUsers(List<String> userIds) {

349

logger.info("Processing {} users", userIds.size());

350

351

for (int i = 0; i < userIds.size(); i++) {

352

String userId = userIds.get(i);

353

logger.trace("Processing user {} of {}: {}", i + 1, userIds.size(), userId);

354

// ... process user

355

}

356

357

logger.info("User processing completed");

358

}

359

}

360

```

361

362

### Parameter Substitution

363

364

SLF4J uses `{}` as placeholder for parameters, which is safer and more efficient than string concatenation:

365

366

```java

367

// Efficient - parameters only evaluated if logging level is enabled

368

logger.debug("User {} has {} active sessions", user.getName(), sessionCount);

369

370

// Inefficient - string concatenation always performed

371

logger.debug("User " + user.getName() + " has " + sessionCount + " active sessions");

372

373

// Multiple parameters

374

logger.info("Transaction {} processed: amount={}, status={}",

375

transaction.getId(),

376

transaction.getAmount(),

377

transaction.getStatus());

378

379

// Exception logging - throwable should be the last parameter

380

logger.error("Failed to process transaction {}", transactionId, exception);

381

```

382

383

## Level Hierarchy

384

385

SLF4J log levels follow this hierarchy (from most severe to least severe):

386

- **ERROR**: Error events that might still allow the application to continue

387

- **WARN**: Potentially harmful situations

388

- **INFO**: Informational messages that highlight application progress

389

- **DEBUG**: Fine-grained informational events most useful for debugging

390

- **TRACE**: Finer-grained informational events than DEBUG

391

392

When a logger is configured for a specific level, it will log messages at that level and all higher levels. For example, a logger configured for INFO level will log ERROR, WARN, and INFO messages, but not DEBUG or TRACE messages.