Appenders in Log4j

Available

List of appenders

The logger allows you to save data to several files at once. Such output data streams are called appenders (from append). There are quite a few standard appenders, so we will only cover the most popular ones:

Appenders Description
1 Console Prints data to the console
2 file Outputs data to a file
3 DailyRollingFile Outputs data to a file, the file is cyclically overwritten
4 Async Allows you to write data to another appender asynchronously
5 Socket Writes data to a specific socket
6 JDBC Writes messages to the database using the JDBC protocol
7 JPA Writes messages to the database using the JPA protocol
8 http Sends events via HTTP protocol to a remote server
9 SMTP Stores messages in a buffer, and then sends them as an email

Good documentation for all appenders is on their official website

And below we will consider the most popular and simple of them.

ConsoleAppender

The simplest appender isConsoleApender . As you may have guessed, he simply writes his messages to the console. It has several interesting parameters for us:

Attributes
1 name Appender name
2 filter Allows you to filter some messages
3 layout Specifies the formatting of messages when output
4 target Specifies where to write: SYTEM_OUTorSYSTEM_ERR

It's very easy to configure it:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
<Appenders>
    <Console name="STDOUT" target="SYSTEM_OUT">
    <PatternLayout pattern="%m%n"/>
    </Console>
</Appenders>
  <Loggers>
    <Root level="error">
    <AppenderRef ref="STDOUT"/>
    </Root>
</Loggers>
</Configuration>

FileAppender

The most useful appender isFileAppender . Unlike ConsoleAppenderhe writes his messages to a file. Which is very useful when your application is running somewhere on the server. It has a lot of parameters, because it must be able to write files on different operating systems.

But we will consider only the most popular of them.

1 name Specifies the name of the appender
2 filter Allows you to filter some messages
3 layout Specifies the formatting of messages when output
4 fileName Specifies the name of the file where to write messages
5 append If true, then the messages will be added to the old log, if false- the log file will be recreated each time the application is started.
6 bufferSize Sets the buffer size in bytes
7 immediateFlush If true, then each message is actually written to disk immediately (without a buffer). The log starts to work slowly, but this saves you from losing data when the program crashes.

You already know how to work well with files, so these settings are nothing new to you. Configuring such a logger is even easier than a console logger:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
<Appenders>
    <File name="MyFile" fileName="logs/app.log">
    <PatternLayout>
        <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
    </PatternLayout>
    </File>
</Appenders>
<Loggers>
    <Root level="error">
    <AppenderRef ref="MyFile"/>
    </Root>
</Loggers>
</Configuration>

RollingFileAppender

The most popular appender isRollingFileAppender . Unlike FileAppenderit, it allows you to split the log into many small files. This is very important for large logs. In addition, it allows you to set rules for what to do with old files after new ones have started to be written.

And this appender has almost a hundred different settings. You can find out more about them at the link .

Consider the most popular attributes of this appender:

Attributes
1 name Specifies the name of the appender
2 filter Allows you to filter some messages
3 layout Specifies the formatting of messages when output
4 fileName Specifies the name of the file where to write messages
5 filePattern Specifies a naming pattern for archive files that are no longer written
6 policy Specifies the condition when the file should start being overwritten
7 strategy Describes what to do with old files: archive, history for how many days to keep, etc.

Here is a good example:

<Configuration status="warn" name="MyApp" packages="">
  <Appenders>
    <RollingFile name="RollingFile" fileName="logs/app.log" filePattern="logs/app-%d{MM-dd-yyyy}-%i.log.gz">
    <PatternLayout>
        <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
    </PatternLayout>
    <Policies>
        <TimeBasedTriggeringPolicy />
        <SizeBasedTriggeringPolicy size="250 MB"/>
    </Policies>
    </RollingFile>
  </Appenders>
  <Loggers>
    <Root level="error">
    <AppenderRef ref="RollingFile"/>
    </Root>
  </Loggers>
</Configuration>

The parameter filePatternspecifies a template for archives of old logs. There are also two, when triggered, a new file will be written:

  • TimeBasedTriggeringPolicy– will work if a new day starts (the current date changes)
  • SizeBasedTriggeringPolicy– will work if the file size reaches 250MB
Comments
  • Popular
  • New
  • Old
You must be signed in to leave a comment
This page doesn't have any comments yet