0
# Appenders and Layouts
1
2
Appender interfaces and skeleton implementations for output destinations, plus layout classes for message formatting. Most implementations are minimal or no-op since actual output handling is managed by the underlying SLF4J implementation.
3
4
## Capabilities
5
6
### Appender Interface
7
8
Base interface defining the contract for log output destinations. All methods provide compatibility with Log4j 1.x API.
9
10
```java { .api }
11
/**
12
* Base interface for log output destinations
13
*/
14
public interface Appender {
15
16
/**
17
* Add filter to the appender
18
* @param newFilter Filter to add
19
*/
20
void addFilter(Filter newFilter);
21
22
/**
23
* Get the head filter in the filter chain
24
* @return Head filter or null if no filters
25
*/
26
Filter getFilter();
27
28
/**
29
* Clear all filters from the appender
30
*/
31
void clearFilters();
32
33
/**
34
* Close the appender and release resources
35
*/
36
void close();
37
38
/**
39
* Log event to appender destination
40
* @param event LoggingEvent to append
41
*/
42
void doAppend(LoggingEvent event);
43
44
/**
45
* Get appender name
46
* @return Name of the appender
47
*/
48
String getName();
49
50
/**
51
* Set appender name
52
* @param name Name for the appender
53
*/
54
void setName(String name);
55
56
/**
57
* Get error handler for this appender
58
* @return ErrorHandler instance
59
*/
60
ErrorHandler getErrorHandler();
61
62
/**
63
* Set error handler for this appender
64
* @param errorHandler ErrorHandler to set
65
*/
66
void setErrorHandler(ErrorHandler errorHandler);
67
68
/**
69
* Get layout for this appender
70
* @return Layout instance
71
*/
72
Layout getLayout();
73
74
/**
75
* Set layout for this appender
76
* @param layout Layout to set
77
*/
78
void setLayout(Layout layout);
79
80
/**
81
* Check if this appender requires a layout
82
* @return true if layout is required
83
*/
84
boolean requiresLayout();
85
}
86
```
87
88
### AppenderSkeleton
89
90
Minimal implementation class implementing OptionHandler interface. Provides skeleton methods for appender functionality.
91
92
```java { .api }
93
/**
94
* Skeleton implementation for appender functionality
95
*/
96
public class AppenderSkeleton implements OptionHandler {
97
98
/**
99
* Set layout for the appender (no-op implementation)
100
* @param layout Layout to set
101
*/
102
public void setLayout(Layout layout);
103
104
/**
105
* Set name for the appender (no-op implementation)
106
* @param name Name to set
107
*/
108
public void setName(String name);
109
110
/**
111
* Activate options (no-op implementation)
112
*/
113
public void activateOptions();
114
115
/**
116
* Set threshold priority (no-op implementation)
117
* @param threshold Priority threshold to set
118
*/
119
public void setThreshold(Priority threshold);
120
}
121
```
122
123
**Usage Examples:**
124
125
```java
126
// AppenderSkeleton provides minimal compatibility
127
AppenderSkeleton appender = new AppenderSkeleton() {};
128
appender.setName("MyAppender");
129
appender.setLayout(new SimpleLayout());
130
appender.activateOptions(); // No-op
131
```
132
133
### WriterAppender
134
135
Minimal appender class extending AppenderSkeleton. Empty implementation providing inheritance hierarchy compatibility.
136
137
```java { .api }
138
/**
139
* Minimal writer-based appender extending AppenderSkeleton
140
*/
141
public class WriterAppender extends AppenderSkeleton {
142
// Empty implementation - all functionality inherited from AppenderSkeleton
143
}
144
```
145
146
### Console Appender
147
148
Skeleton implementation for console output. Extends WriterAppender with console-specific constructors.
149
150
```java { .api }
151
/**
152
* Console output appender (skeleton implementation)
153
*/
154
public class ConsoleAppender extends WriterAppender {
155
156
/**
157
* Default constructor
158
*/
159
public ConsoleAppender();
160
161
/**
162
* Constructor with layout
163
* @param layout Layout for formatting messages
164
*/
165
public ConsoleAppender(Layout layout);
166
167
/**
168
* Constructor with layout and target
169
* @param layout Layout for formatting messages
170
* @param target Target stream ("System.out" or "System.err")
171
*/
172
public ConsoleAppender(Layout layout, String target);
173
}
174
```
175
176
**Usage Examples:**
177
178
```java
179
import org.apache.log4j.ConsoleAppender;
180
import org.apache.log4j.SimpleLayout;
181
import org.apache.log4j.PatternLayout;
182
183
// Create console appender with simple layout
184
ConsoleAppender consoleAppender = new ConsoleAppender(new SimpleLayout());
185
186
// Create with pattern layout
187
PatternLayout pattern = new PatternLayout("%d [%t] %-5p %c - %m%n");
188
ConsoleAppender patternAppender = new ConsoleAppender(pattern);
189
190
// Create with target specification
191
ConsoleAppender errorAppender = new ConsoleAppender(pattern, "System.err");
192
193
// Note: These are skeleton implementations - actual output
194
// is handled by SLF4J implementation
195
```
196
197
### FileAppender
198
199
File output appender with minimal implementation. Extends WriterAppender.
200
201
```java { .api }
202
/**
203
* File output appender (minimal implementation)
204
*/
205
public class FileAppender extends WriterAppender {
206
// Minimal implementation - inherits from WriterAppender
207
}
208
```
209
210
### RollingFileAppender
211
212
Rolling file appender with minimal implementation. Does not extend FileAppender in this implementation.
213
214
```java { .api }
215
/**
216
* Rolling file appender (minimal implementation)
217
*/
218
public class RollingFileAppender {
219
220
/**
221
* Default constructor
222
*/
223
public RollingFileAppender();
224
225
/**
226
* Constructor with layout and filename
227
* @param layout Layout for formatting
228
* @param filename Output file name
229
* @throws IOException If file operations fail
230
*/
231
public RollingFileAppender(Layout layout, String filename) throws IOException;
232
233
/**
234
* Constructor with layout, filename and append mode
235
* @param layout Layout for formatting
236
* @param filename Output file name
237
* @param append Whether to append to existing file
238
* @throws IOException If file operations fail
239
*/
240
public RollingFileAppender(Layout layout, String filename, boolean append) throws IOException;
241
242
/**
243
* Set maximum number of backup files (no-op implementation)
244
* @param maxBackups Maximum backup files
245
*/
246
public void setMaxBackupIndex(int maxBackups);
247
248
/**
249
* Set maximum file size before rolling (no-op implementation)
250
* @param maxFileSize Maximum file size in bytes
251
*/
252
public void setMaximumFileSize(long maxFileSize);
253
}
254
```
255
256
**Usage Examples:**
257
258
```java
259
import org.apache.log4j.RollingFileAppender;
260
import org.apache.log4j.PatternLayout;
261
import java.io.IOException;
262
263
try {
264
// Create rolling file appender
265
RollingFileAppender rollingAppender = new RollingFileAppender(
266
new PatternLayout("%d %-5p [%c] - %m%n"),
267
"app.log"
268
);
269
270
// Configure rolling parameters (no-op)
271
rollingAppender.setMaxBackupIndex(5);
272
rollingAppender.setMaximumFileSize(10 * 1024 * 1024); // 10MB
273
274
} catch (IOException e) {
275
// Handle file creation errors
276
}
277
278
// Note: This is a minimal implementation for API compatibility
279
```
280
281
### Layout Classes
282
283
Layout classes for formatting log messages. Minimal implementations providing basic API compatibility.
284
285
```java { .api }
286
/**
287
* Base class for message layouts (minimal implementation)
288
*/
289
public class Layout {
290
// Minimal implementation - actual formatting handled by SLF4J
291
}
292
293
/**
294
* Simple layout extending base Layout
295
*/
296
public class SimpleLayout extends Layout {
297
// Minimal implementation extending Layout
298
}
299
300
/**
301
* Pattern-based layout with minimal implementation
302
*/
303
public class PatternLayout extends Layout {
304
305
/**
306
* Default constructor
307
*/
308
public PatternLayout();
309
310
/**
311
* Constructor with pattern
312
* @param pattern Conversion pattern (stored but not used)
313
*/
314
public PatternLayout(String pattern);
315
}
316
```
317
318
**Usage Examples:**
319
320
```java
321
import org.apache.log4j.SimpleLayout;
322
import org.apache.log4j.PatternLayout;
323
324
// Simple layout
325
SimpleLayout simple = new SimpleLayout();
326
327
// Pattern layouts (pattern parameter accepted but not processed)
328
PatternLayout basic = new PatternLayout();
329
PatternLayout detailed = new PatternLayout("%d [%t] %-5p %c - %m%n");
330
331
// Note: Actual message formatting is handled by SLF4J implementation
332
// These layouts provide API compatibility only
333
```
334
335
### Service Provider Interfaces
336
337
SPI interfaces providing minimal implementations for Log4j compatibility.
338
339
```java { .api }
340
/**
341
* Option handler interface for activation
342
*/
343
public interface OptionHandler {
344
345
/**
346
* Activate configured options
347
*/
348
void activateOptions();
349
}
350
351
/**
352
* Filter interface (minimal implementation)
353
*/
354
public abstract class Filter {
355
// Minimal implementation - filtering handled by SLF4J
356
}
357
358
/**
359
* Error handler interface (minimal implementation)
360
*/
361
public interface ErrorHandler {
362
// Minimal interface - error handling managed by SLF4J
363
}
364
365
/**
366
* Logging event class (minimal implementation)
367
*/
368
public class LoggingEvent {
369
// Minimal implementation - event data handled by SLF4J
370
}
371
```
372
373
**Usage Examples:**
374
375
```java
376
// These SPI classes provide API compatibility but minimal functionality
377
// Actual filtering, error handling, and event processing is managed by SLF4J
378
379
// Custom option handler
380
public class CustomAppender implements OptionHandler {
381
@Override
382
public void activateOptions() {
383
// Configuration activation logic
384
}
385
}
386
```
387
388
## Implementation Notes
389
390
**Important:** All appender and layout classes in log4j-over-slf4j are minimal or skeleton implementations. They provide API compatibility with Log4j 1.x but do not perform actual logging operations. Instead:
391
392
1. **Actual logging output** is handled by the underlying SLF4J implementation
393
2. **Configuration** should be done through SLF4J configuration files (logback.xml, etc.)
394
3. **Formatting** is controlled by the SLF4J logging framework configuration
395
4. **Appender behavior** is managed by SLF4J appenders, not these compatibility classes
396
397
**Migration Strategy:**
398
399
When migrating from Log4j 1.x to log4j-over-slf4j:
400
401
1. Keep existing Log4j appender configuration code for compatibility
402
2. Configure actual logging behavior through SLF4J configuration files
403
3. Remove or simplify programmatic appender configuration
404
4. Test that logging output matches expectations through SLF4J backend
405
406
**Example SLF4J Configuration (logback.xml):**
407
408
```xml
409
<configuration>
410
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
411
<encoder>
412
<pattern>%d [%thread] %-5level %logger{36} - %msg%n</pattern>
413
</encoder>
414
</appender>
415
416
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
417
<file>app.log</file>
418
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
419
<fileNamePattern>app.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
420
<maxFileSize>10MB</maxFileSize>
421
<maxHistory>30</maxHistory>
422
</rollingPolicy>
423
<encoder>
424
<pattern>%d %-5level [%logger{36}] - %msg%n</pattern>
425
</encoder>
426
</appender>
427
428
<root level="INFO">
429
<appender-ref ref="CONSOLE"/>
430
<appender-ref ref="FILE"/>
431
</root>
432
</configuration>
433
```