0
# Standard Logging
1
2
Core SLF4J logging functionality providing all standard log levels with parameterized message support and automatic formatting.
3
4
## Capabilities
5
6
### Logger Creation
7
8
Get logger instances using the standard SLF4J LoggerFactory.
9
10
```java { .api }
11
/**
12
* Get a logger instance for the specified class
13
* @param clazz The class for which to create a logger
14
* @return Logger instance
15
*/
16
Logger getLogger(Class<?> clazz);
17
18
/**
19
* Get a logger instance for the specified name
20
* @param name The logger name
21
* @return Logger instance
22
*/
23
Logger getLogger(String name);
24
```
25
26
**Usage Example:**
27
28
```java
29
import org.slf4j.Logger;
30
import org.slf4j.LoggerFactory;
31
32
public class MyService {
33
// Class-based logger (recommended)
34
private static final Logger logger = LoggerFactory.getLogger(MyService.class);
35
36
// Name-based logger
37
private static final Logger namedLogger = LoggerFactory.getLogger("com.example.custom");
38
}
39
```
40
41
### Trace Level Logging
42
43
Finest granularity logging for detailed troubleshooting information.
44
45
```java { .api }
46
/**
47
* Log a message at TRACE level
48
* @param msg The message string
49
*/
50
void trace(String msg);
51
52
/**
53
* Log a message at TRACE level with one parameter
54
* @param format The format string
55
* @param arg The argument
56
*/
57
void trace(String format, Object arg);
58
59
/**
60
* Log a message at TRACE level with two parameters
61
* @param format The format string
62
* @param arg1 The first argument
63
* @param arg2 The second argument
64
*/
65
void trace(String format, Object arg1, Object arg2);
66
67
/**
68
* Log a message at TRACE level with multiple parameters
69
* @param format The format string
70
* @param arguments The arguments
71
*/
72
void trace(String format, Object... arguments);
73
74
/**
75
* Log a message at TRACE level with an exception
76
* @param msg The message string
77
* @param t The exception
78
*/
79
void trace(String msg, Throwable t);
80
81
/**
82
* Check if TRACE level is enabled
83
* @return true if TRACE level is enabled
84
*/
85
boolean isTraceEnabled();
86
```
87
88
**Usage Examples:**
89
90
```java
91
// Simple message
92
logger.trace("Entering method processData()");
93
94
// Parameterized message
95
logger.trace("Processing item {} of {}", currentIndex, totalItems);
96
97
// Multiple parameters
98
logger.trace("User {} accessing resource {} with permissions {}", userId, resourceId, permissions);
99
100
// With exception
101
try {
102
// some operation
103
} catch (Exception e) {
104
logger.trace("Failed to process data", e);
105
}
106
107
// Conditional logging
108
if (logger.isTraceEnabled()) {
109
logger.trace("Expensive operation result: {}", computeExpensiveValue());
110
}
111
```
112
113
### Debug Level Logging
114
115
Diagnostic information for development and troubleshooting.
116
117
```java { .api }
118
void debug(String msg);
119
void debug(String format, Object arg);
120
void debug(String format, Object arg1, Object arg2);
121
void debug(String format, Object... arguments);
122
void debug(String msg, Throwable t);
123
boolean isDebugEnabled();
124
```
125
126
**Usage Examples:**
127
128
```java
129
logger.debug("Database connection established");
130
logger.debug("Query executed in {} ms", executionTime);
131
logger.debug("Cache hit ratio: {}, total requests: {}", hitRatio, totalRequests);
132
```
133
134
### Info Level Logging
135
136
General informational messages about application flow.
137
138
```java { .api }
139
void info(String msg);
140
void info(String format, Object arg);
141
void info(String format, Object arg1, Object arg2);
142
void info(String format, Object... arguments);
143
void info(String msg, Throwable t);
144
boolean isInfoEnabled();
145
```
146
147
**Usage Examples:**
148
149
```java
150
logger.info("Application started successfully");
151
logger.info("User {} logged in from {}", username, ipAddress);
152
logger.info("Processed {} records in {} seconds", recordCount, duration);
153
```
154
155
### Warn Level Logging
156
157
Warning messages for potentially problematic situations.
158
159
```java { .api }
160
void warn(String msg);
161
void warn(String format, Object arg);
162
void warn(String format, Object arg1, Object arg2);
163
void warn(String format, Object... arguments);
164
void warn(String msg, Throwable t);
165
boolean isWarnEnabled();
166
```
167
168
**Usage Examples:**
169
170
```java
171
logger.warn("Configuration file not found, using defaults");
172
logger.warn("High memory usage detected: {}%", memoryUsage);
173
logger.warn("Deprecated API call from {}", callerClass);
174
```
175
176
### Error Level Logging
177
178
Error conditions that may still allow the application to continue running.
179
180
```java { .api }
181
void error(String msg);
182
void error(String format, Object arg);
183
void error(String format, Object arg1, Object arg2);
184
void error(String format, Object... arguments);
185
void error(String msg, Throwable t);
186
boolean isErrorEnabled();
187
```
188
189
**Usage Examples:**
190
191
```java
192
logger.error("Failed to save user data");
193
logger.error("Database connection failed after {} attempts", retryCount);
194
logger.error("Critical error in payment processing", exception);
195
```
196
197
### Location-Aware Logging
198
199
Advanced logging method for frameworks requiring precise caller location information.
200
201
```java { .api }
202
/**
203
* Log with location awareness (LocationAwareLogger interface)
204
* @param marker The marker (can be null)
205
* @param fqcn Fully qualified class name of the caller
206
* @param level The log level as integer
207
* @param message The message
208
* @param params Message parameters
209
* @param throwable Exception (can be null)
210
*/
211
void log(Marker marker, String fqcn, int level, String message, Object[] params, Throwable throwable);
212
```
213
214
**Usage Example:**
215
216
```java
217
import org.slf4j.spi.LocationAwareLogger;
218
219
LocationAwareLogger locationLogger = (LocationAwareLogger) logger;
220
locationLogger.log(null, MyClass.class.getName(),
221
LocationAwareLogger.INFO_INT, "Custom location message", null, null);
222
```
223
224
### Logger Information
225
226
Access logger metadata and configuration.
227
228
```java { .api }
229
/**
230
* Get the logger name
231
* @return The logger name
232
*/
233
String getName();
234
235
/**
236
* Check if a specific level is enabled
237
* @param level The SLF4J level to check
238
* @return true if the level is enabled
239
*/
240
boolean isEnabledForLevel(org.slf4j.event.Level level);
241
```
242
243
**Usage Examples:**
244
245
```java
246
String loggerName = logger.getName();
247
logger.info("Logger name: {}", loggerName);
248
249
// Check specific level
250
if (logger.isEnabledForLevel(org.slf4j.event.Level.DEBUG)) {
251
// Perform debug-specific operations
252
}
253
```
254
255
## Message Formatting
256
257
All logging methods support SLF4J's parameterized message format using `{}` placeholders:
258
259
```java
260
// Single parameter
261
logger.info("User {} logged in", username);
262
263
// Multiple parameters
264
logger.info("Transfer of ${} from {} to {} completed", amount, fromAccount, toAccount);
265
266
// Arrays are automatically converted
267
String[] items = {"apple", "banana", "orange"};
268
logger.debug("Processing items: {}", items);
269
```
270
271
## Performance Considerations
272
273
- Use parameterized messages instead of string concatenation
274
- Check level enablement for expensive operations
275
- Avoid boxing primitives unnecessarily
276
277
```java
278
// Efficient
279
if (logger.isDebugEnabled()) {
280
logger.debug("Complex calculation result: {}", expensiveCalculation());
281
}
282
283
// Less efficient - calculation always performed
284
logger.debug("Complex calculation result: {}", expensiveCalculation());
285
```