or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

appenders.mdbuilders.mdconfiguration.mdcontext.mdindex.mdlayouts.mdlogging.mdspi.md

appenders.mddocs/

0

# Appenders and Output

1

2

Appenders define where log messages are sent, including console output, files, rolling files, and custom destinations. The compatibility API provides full Log4j 1.x appender functionality.

3

4

## Core Appender Interface

5

6

### Appender Interface

7

8

```java { .api }

9

public interface Appender {

10

// Core appender methods

11

void doAppend(LoggingEvent event);

12

void close();

13

boolean requiresLayout();

14

15

// Name management

16

String getName();

17

void setName(String name);

18

19

// Layout management

20

Layout getLayout();

21

void setLayout(Layout layout);

22

23

// Error handling

24

ErrorHandler getErrorHandler();

25

void setErrorHandler(ErrorHandler eh);

26

27

// Filter management

28

void addFilter(Filter newFilter);

29

Filter getFilter();

30

void clearFilters();

31

}

32

```

33

34

**Parameters:**

35

- `event` - LoggingEvent to append

36

- `name` - String name for the appender

37

- `layout` - Layout for message formatting

38

- `eh` - ErrorHandler for error management

39

- `newFilter` - Filter to add to filter chain

40

41

**Returns:**

42

- `String` appender name

43

- `Layout` current layout

44

- `ErrorHandler` current error handler

45

- `Filter` first filter in chain

46

- `boolean` indicating if layout is required

47

48

### AppenderSkeleton Base Class

49

50

```java { .api }

51

public abstract class AppenderSkeleton implements Appender, OptionHandler {

52

// Abstract method to implement

53

protected abstract void append(LoggingEvent event);

54

55

// Configuration

56

public void activateOptions();

57

58

// Name management

59

public String getName();

60

public void setName(String name);

61

62

// Layout management

63

public Layout getLayout();

64

public void setLayout(Layout layout);

65

66

// Threshold management

67

public Priority getThreshold();

68

public void setThreshold(Priority threshold);

69

public boolean isAsSevereAsThreshold(Priority priority);

70

71

// Error handling

72

public ErrorHandler getErrorHandler();

73

public void setErrorHandler(ErrorHandler errorHandler);

74

75

// Filter management

76

public void addFilter(Filter newFilter);

77

public Filter getFilter();

78

public void clearFilters();

79

80

// Lifecycle

81

public void close();

82

public boolean isClosed();

83

}

84

```

85

86

**Parameters:**

87

- `event` - LoggingEvent to process

88

- `name` - String name for appender

89

- `layout` - Layout for formatting

90

- `threshold` - Priority threshold for filtering

91

- `priority` - Priority to check against threshold

92

- `errorHandler` - ErrorHandler for error management

93

- `newFilter` - Filter to add

94

95

**Returns:**

96

- `String`, `Layout`, `Priority`, `ErrorHandler`, `Filter` as appropriate

97

- `boolean` for threshold checks and closed status

98

99

## Console Appenders

100

101

### ConsoleAppender

102

103

```java { .api }

104

public class ConsoleAppender extends WriterAppender {

105

// Target constants

106

public static final String SYSTEM_OUT = "System.out";

107

public static final String SYSTEM_ERR = "System.err";

108

109

// Constructors

110

public ConsoleAppender();

111

public ConsoleAppender(Layout layout);

112

public ConsoleAppender(Layout layout, String target);

113

114

// Target management

115

public String getTarget();

116

public void setTarget(String value);

117

118

// Follow option

119

public boolean getFollow();

120

public void setFollow(boolean follow);

121

122

// Appender behavior

123

public boolean requiresLayout();

124

public void activateOptions();

125

}

126

```

127

128

**Parameters:**

129

- `layout` - Layout for message formatting

130

- `target` - Target stream (SYSTEM_OUT or SYSTEM_ERR)

131

- `value` - String target value to set

132

- `follow` - Boolean indicating if target should be followed

133

134

**Returns:**

135

- `String` current target

136

- `boolean` follow setting and layout requirement

137

138

## File Appenders

139

140

### FileAppender

141

142

```java { .api }

143

public class FileAppender extends WriterAppender {

144

// Constructors

145

public FileAppender();

146

public FileAppender(Layout layout, String filename) throws IOException;

147

public FileAppender(Layout layout, String filename, boolean append) throws IOException;

148

149

// File management

150

public String getFile();

151

public void setFile(String file);

152

public boolean getAppend();

153

public void setAppend(boolean flag);

154

155

// Buffering

156

public boolean getBufferedIO();

157

public void setBufferedIO(boolean bufferedIO);

158

public int getBufferSize();

159

public void setBufferSize(int bufferSize);

160

161

// Lifecycle

162

public void activateOptions();

163

protected void closeFile();

164

protected void setQWForFiles(Writer writer);

165

166

// Utility

167

protected void reset();

168

protected void writeHeader();

169

protected void writeFooter();

170

}

171

```

172

173

**Parameters:**

174

- `layout` - Layout for message formatting

175

- `filename` - String path to log file

176

- `append` - Boolean indicating if file should be appended to

177

- `flag` - Boolean append flag

178

- `bufferedIO` - Boolean indicating if IO should be buffered

179

- `bufferSize` - Integer buffer size in bytes

180

- `writer` - Writer for file output

181

182

**Returns:**

183

- `String` current filename

184

- `boolean` append and buffering settings

185

- `int` buffer size

186

187

**Throws:**

188

- `IOException` - When file operations fail

189

190

### RollingFileAppender

191

192

```java { .api }

193

public class RollingFileAppender extends FileAppender {

194

// Constructors

195

public RollingFileAppender();

196

public RollingFileAppender(Layout layout, String filename) throws IOException;

197

public RollingFileAppender(Layout layout, String filename, boolean append) throws IOException;

198

199

// Rolling configuration

200

public long getMaximumFileSize();

201

public void setMaximumFileSize(long maxFileSize);

202

public void setMaxFileSize(String value);

203

public int getMaxBackupIndex();

204

public void setMaxBackupIndex(int maxBackups);

205

206

// Rolling behavior

207

public void rollOver();

208

protected void subAppend(LoggingEvent event);

209

}

210

```

211

212

**Parameters:**

213

- `layout` - Layout for formatting

214

- `filename` - String path to log file

215

- `append` - Boolean append flag

216

- `maxFileSize` - Long maximum file size in bytes

217

- `value` - String representation of max file size (e.g., "10MB")

218

- `maxBackups` - Integer maximum number of backup files

219

- `event` - LoggingEvent to append

220

221

**Returns:**

222

- `long` maximum file size

223

- `int` maximum backup index

224

225

**Throws:**

226

- `IOException` - When file operations fail

227

228

### DailyRollingFileAppender

229

230

```java { .api }

231

public class DailyRollingFileAppender extends FileAppender {

232

// Date pattern constants

233

public static final String MONTHLY_ROLLOVER = "'.'yyyy-MM";

234

public static final String WEEKLY_ROLLOVER = "'.'yyyy-ww";

235

public static final String DAILY_ROLLOVER = "'.'yyyy-MM-dd";

236

public static final String TWICE_DAILY_ROLLOVER = "'.'yyyy-MM-dd-a";

237

public static final String HOURLY_ROLLOVER = "'.'yyyy-MM-dd-HH";

238

public static final String MINUTELY_ROLLOVER = "'.'yyyy-MM-dd-HH-mm";

239

240

// Constructors

241

public DailyRollingFileAppender();

242

public DailyRollingFileAppender(Layout layout, String filename, String datePattern) throws IOException;

243

244

// Date pattern management

245

public String getDatePattern();

246

public void setDatePattern(String pattern);

247

248

// Rolling behavior

249

public void activateOptions();

250

protected void subAppend(LoggingEvent event);

251

}

252

```

253

254

**Parameters:**

255

- `layout` - Layout for formatting

256

- `filename` - String path to log file

257

- `datePattern` - String date pattern for rolling (SimpleDateFormat pattern)

258

- `pattern` - String date pattern to set

259

- `event` - LoggingEvent to append

260

261

**Returns:**

262

- `String` current date pattern

263

264

**Throws:**

265

- `IOException` - When file operations fail

266

267

## Network Appenders

268

269

### SocketAppender

270

271

```java { .api }

272

public class SocketAppender extends AppenderSkeleton {

273

// Constructors

274

public SocketAppender();

275

public SocketAppender(InetAddress address, int port);

276

public SocketAppender(String host, int port);

277

278

// Connection configuration

279

public String getRemoteHost();

280

public void setRemoteHost(String host);

281

public int getPort();

282

public void setPort(int port);

283

public int getReconnectionDelay();

284

public void setReconnectionDelay(int delay);

285

286

// Lifecycle

287

public void activateOptions();

288

public void close();

289

public boolean requiresLayout();

290

291

// Appending

292

protected void append(LoggingEvent event);

293

}

294

```

295

296

**Parameters:**

297

- `address` - InetAddress of remote host

298

- `port` - Integer port number

299

- `host` - String hostname or IP address

300

- `delay` - Integer reconnection delay in milliseconds

301

- `event` - LoggingEvent to send

302

303

**Returns:**

304

- `String` remote host

305

- `int` port and reconnection delay

306

- `boolean` layout requirement (false)

307

308

## Custom Appenders

309

310

### WriterAppender Base Class

311

312

```java { .api }

313

public class WriterAppender extends AppenderSkeleton {

314

// Constructors

315

public WriterAppender();

316

public WriterAppender(Layout layout, Writer writer);

317

public WriterAppender(Layout layout, OutputStream os);

318

319

// Writer management

320

public void setWriter(Writer writer);

321

protected void closeWriter();

322

protected void writeHeader();

323

protected void writeFooter();

324

325

// Encoding

326

public String getEncoding();

327

public void setEncoding(String value);

328

329

// Immediate flush

330

public boolean getImmediateFlush();

331

public void setImmediateFlush(boolean value);

332

333

// Appending

334

protected void append(LoggingEvent event);

335

protected void subAppend(LoggingEvent event);

336

337

// Lifecycle

338

public void activateOptions();

339

public void close();

340

public boolean requiresLayout();

341

}

342

```

343

344

**Parameters:**

345

- `layout` - Layout for formatting

346

- `writer` - Writer for output

347

- `os` - OutputStream for output

348

- `value` - String encoding name or boolean immediate flush flag

349

- `event` - LoggingEvent to append

350

351

**Returns:**

352

- `String` current encoding

353

- `boolean` immediate flush setting and layout requirement

354

355

## Usage Examples

356

357

### Console Appender Setup

358

```java

359

import org.apache.log4j.*;

360

361

public class ConsoleAppenderExample {

362

public void setupConsoleLogging() {

363

// Create pattern layout

364

PatternLayout layout = new PatternLayout("%d{HH:mm:ss} %-5p %c{1} - %m%n");

365

366

// Create console appender

367

ConsoleAppender appender = new ConsoleAppender(layout, ConsoleAppender.SYSTEM_OUT);

368

appender.setName("console");

369

370

// Add to root logger

371

Logger rootLogger = Logger.getRootLogger();

372

rootLogger.addAppender(appender);

373

rootLogger.setLevel(Level.INFO);

374

}

375

}

376

```

377

378

### File Appender Setup

379

```java

380

import org.apache.log4j.*;

381

import java.io.IOException;

382

383

public class FileAppenderExample {

384

public void setupFileLogging() throws IOException {

385

// Create layout

386

PatternLayout layout = new PatternLayout("%d{ISO8601} [%t] %-5p %c - %m%n");

387

388

// Create file appender

389

FileAppender appender = new FileAppender(layout, "application.log", true);

390

appender.setName("file");

391

appender.setBufferedIO(true);

392

appender.setBufferSize(8192);

393

appender.activateOptions();

394

395

// Add to logger

396

Logger logger = Logger.getLogger("com.myapp");

397

logger.addAppender(appender);

398

logger.setLevel(Level.DEBUG);

399

}

400

}

401

```

402

403

### Rolling File Appender Setup

404

```java

405

import org.apache.log4j.*;

406

import java.io.IOException;

407

408

public class RollingFileExample {

409

public void setupRollingFile() throws IOException {

410

// Create layout

411

PatternLayout layout = new PatternLayout("%d %p %c - %m%n");

412

413

// Create rolling file appender

414

RollingFileAppender appender = new RollingFileAppender(layout, "app.log", true);

415

appender.setName("rolling");

416

appender.setMaxFileSize("10MB");

417

appender.setMaxBackupIndex(5);

418

appender.activateOptions();

419

420

// Add to root logger

421

Logger.getRootLogger().addAppender(appender);

422

}

423

}

424

```

425

426

### Daily Rolling File Appender

427

```java

428

import org.apache.log4j.*;

429

import java.io.IOException;

430

431

public class DailyRollingExample {

432

public void setupDailyRolling() throws IOException {

433

// Create layout

434

PatternLayout layout = new PatternLayout("%d{ISO8601} %-5p %c - %m%n");

435

436

// Create daily rolling appender

437

DailyRollingFileAppender appender = new DailyRollingFileAppender(

438

layout,

439

"daily.log",

440

DailyRollingFileAppender.DAILY_ROLLOVER

441

);

442

appender.setName("daily");

443

appender.activateOptions();

444

445

// Add to logger

446

Logger logger = Logger.getLogger("com.myapp.daily");

447

logger.addAppender(appender);

448

}

449

}

450

```

451

452

### Custom Appender Implementation

453

```java

454

import org.apache.log4j.*;

455

import org.apache.log4j.spi.LoggingEvent;

456

457

public class CustomAppender extends AppenderSkeleton {

458

private String customProperty;

459

460

public CustomAppender() {

461

super();

462

}

463

464

public String getCustomProperty() {

465

return customProperty;

466

}

467

468

public void setCustomProperty(String customProperty) {

469

this.customProperty = customProperty;

470

}

471

472

@Override

473

protected void append(LoggingEvent event) {

474

// Custom appending logic

475

String message = layout.format(event);

476

// Process message with custom logic

477

processMessage(message);

478

}

479

480

@Override

481

public void close() {

482

// Cleanup resources

483

if (this.closed) {

484

return;

485

}

486

this.closed = true;

487

}

488

489

@Override

490

public boolean requiresLayout() {

491

return true;

492

}

493

494

private void processMessage(String message) {

495

// Custom message processing

496

}

497

}

498

```

499

500

## Utility Appenders

501

502

### NullAppender

503

504

A special appender that discards all log messages. Useful for silencing specific loggers or during testing.

505

506

```java { .api }

507

public class NullAppender extends AppenderSkeleton {

508

// Constructors

509

public NullAppender();

510

511

// Factory method (preferred)

512

public static NullAppender getNullAppender();

513

514

// Deprecated factory method

515

@Deprecated

516

public NullAppender getInstance();

517

518

// Inherited methods (all no-op)

519

public void activateOptions();

520

protected void append(LoggingEvent event);

521

public void close();

522

public void doAppend(LoggingEvent event);

523

public boolean requiresLayout();

524

}

525

```

526

527

**Parameters:**

528

- All methods are no-op implementations

529

530

**Returns:**

531

- `getNullAppender()` returns singleton instance

532

- `requiresLayout()` returns false

533

534

**Usage Example:**

535

```java

536

import org.apache.log4j.Logger;

537

import org.apache.log4j.varia.NullAppender;

538

539

public class NullAppenderExample {

540

public void setupNullAppender() {

541

Logger logger = Logger.getLogger("noisy.component");

542

543

// Use singleton instance (preferred)

544

logger.addAppender(NullAppender.getNullAppender());

545

546

// Or create new instance

547

logger.addAppender(new NullAppender());

548

549

// This logger will now discard all messages

550

logger.info("This message will be discarded");

551

}

552

}

553

```