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
<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
fileis the active log file. The application always writes here.SizeAndTimeBasedRollingPolicyrolls the file over when either a time period ends or the file reaches a size limit.fileNamePatternnames 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.%iis a counter for the files of the same day, starting at 0. Both%dand%iare required with this policy. Ending the pattern in.gzor.zipcompresses the archives.maxFileSizeis the size limit of one file, here 100 MB. When the active file reaches it, Logback archives it as the next%ifor the day and starts a new one, so a busy day produceslogback.2026-11-25.0.log,logback.2026-11-25.1.logand so on.maxHistoryis the number of periods to keep, counted in the unit of the date pattern: 60 days here. Older archives are deleted.totalSizeCaplimits the total size of all archives to 20 GB. When it is exceeded, the oldest archives are deleted first, even if they are withinmaxHistory.
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:
<appender name="databaseAppender" class="ch.qos.logback.classic.db.DBAppender"> <connectionSource class="ch.qos.logback.core.db.JNDIConnectionSource"> <jndiLocation>jdbc/pingerbee</jndiLocation> </connectionSource></appender>jndiLocationis the JNDI name of the data source, here a JDBC resourcejdbc/pingerbeedefined in GlassFish or Payara. Servers that publish resources under the component environment need the full name, for examplejava:comp/env/jdbc/logging.- The appender writes to three tables,
logging_event,logging_event_propertyandlogging_event_exception, which must exist before the first event. SQL scripts for the common databases ship with the appender, underch/qos/logback/classic/db/script/. - Every event costs a database insert on the logging thread. Wrap the appender in an
AsyncAppenderif 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:
<root level="INFO"> <appender-ref ref="rollingFileAppender" /> <appender-ref ref="databaseAppender" /></root>