Skip to content

Logback

Two Logback appenders explained: daily rolling log files with size and history limits, and log events written to a database over JNDI.

3 min readUpdated

Logback is the logging framework behind SLF4J in many Java applications. Appenders decide where log events go. This note explains two of them: RollingFileAppender, which writes to a file and starts a new one every day or when the file grows too large, and DBAppender, which writes every event into database tables through a data source from the application server.

Both snippets go inside the <configuration> element of logback.xml; the last section shows how to activate them.

Roll log files daily with a size limit

logback.xml
<property name="LOG_DIR" value="/var/log/myapp" />
<appender name="rollingFileAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_DIR}/logback.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>${LOG_DIR}/logback.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<!-- each file should be at most 100MB, keep 60 days worth of history, but at most 20GB -->
<maxFileSize>100MB</maxFileSize>
<maxHistory>60</maxHistory>
<totalSizeCap>20GB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} >> %-3relative [%thread] %-5level %logger{25} - %msg%n</pattern>
</encoder>
</appender>

The LOG_DIR property keeps the directory in one place. Set it to a directory the application can write to. Logback does not expand ~, so use an absolute path.

File and rolling policy

  • file is the active log file. The application always writes here.
  • SizeAndTimeBasedRollingPolicy rolls the file over when either a time period ends or the file reaches a size limit.
  • fileNamePattern names the archived files. %d{yyyy-MM-dd} gives the date, and because the smallest unit in the pattern is a day, the file rolls over with the first event after midnight. %i is a counter for the files of the same day, starting at 0. Both %d and %i are required with this policy. Ending the pattern in .gz or .zip compresses the archives.
  • maxFileSize is the size limit of one file, here 100 MB. When the active file reaches it, Logback archives it as the next %i for the day and starts a new one, so a busy day produces logback.2026-11-25.0.log, logback.2026-11-25.1.log and so on.
  • maxHistory is the number of periods to keep, counted in the unit of the date pattern: 60 days here. Older archives are deleted.
  • totalSizeCap limits the total size of all archives to 20 GB. When it is exceeded, the oldest archives are deleted first, even if they are within maxHistory.

Encoder pattern

The pattern formats each line:

Element Output
%d{HH:mm:ss.SSS} Time of the event, with milliseconds
>> The literal text >>
%-3relative Milliseconds since the logging system started, left-aligned, at least 3 characters wide
[%thread] Name of the thread, in brackets
%-5level Level (INFO, WARN, …), padded to 5 characters so the columns line up
%logger{25} Logger name, with the package parts abbreviated to keep it near 25 characters
%msg%n The message and a line break

There is a tab character between %-5level and %logger{25}.

Write log events to a database over JNDI

DBAppender inserts every event into database tables. With JNDIConnectionSource, it borrows connections from a data source that the application server manages, so the pool size and credentials stay in the server configuration:

logback.xml
<appender name="databaseAppender" class="ch.qos.logback.classic.db.DBAppender">
<connectionSource class="ch.qos.logback.core.db.JNDIConnectionSource">
<jndiLocation>jdbc/pingerbee</jndiLocation>
</connectionSource>
</appender>
  • jndiLocation is the JNDI name of the data source, here a JDBC resource jdbc/pingerbee defined in GlassFish or Payara. Servers that publish resources under the component environment need the full name, for example java:comp/env/jdbc/logging.
  • The appender writes to three tables, logging_event, logging_event_property and logging_event_exception, which must exist before the first event. SQL scripts for the common databases ship with the appender, under ch/qos/logback/classic/db/script/.
  • Every event costs a database insert on the logging thread. Wrap the appender in an AsyncAppender if logging volume is high.

Note: since Logback 1.2.8, DBAppender is no longer part of logback-classic. Add the separate module ch.qos.logback.db:logback-classic-db (from the logback-db project), which keeps the same class names.

Activate the appenders

An appender only receives events once a logger refers to it. Attach both to the root logger:

logback.xml
<root level="INFO">
<appender-ref ref="rollingFileAppender" />
<appender-ref ref="databaseAppender" />
</root>

References

Related notes

4 min read

Jakarta EE

Jakarta EE 10 and 11: component versions, deployment descriptor headers, Faces namespaces and a multi-module project layout.

  • Java & Jakarta EE
13 min read

Jakarta Faces (JSF)

Jakarta Faces (JSF) recipes: WebSocket push, validators with injection, resource handling, component binding, java.time and scopes.

  • Java & Jakarta EE
  • Web & Frontend
3 min read

JasperReports

JasperReports tips: localise reports with resource bundles, print page X of Y, control band splitting and page breaks, and float elements.

  • Java & Jakarta EE

Type to search the whole site.