Question

I am interested in using slf4j-logback in my project, and would like to use the DBAppender.

Apparently, unless you implement your own DBNameResolver, you must abide by the specific table criteria/schema outlined in the link above. Specifically, you need 3 tables with very specific columns.

Although the information on that page is fairly verbose, it doesn't include any "table metadata" (keys, indexes, default values, etc.) and I'm wondering if we're left to add those at our own discretion or if they need to be defined with specific values.

I tried looking for a DDL or SQL script for creating these tables, but couldn't find any. Do such scripts exist? How have other SOers dealt with the creation of these DBAppender tables? Thanks in advance!

Edit: I found this article on Grails discussing DBAppender:

You must create the database tables yourself. There are three tables, and the Logback distribution ships with sample DDL for several popular databases.

I downloaded the latest (1.0.13) distribution and searched it high and low for .ddl and .sql files, and found something that resembled what I was looking for, located at:

logback-1.0.13/logback-access/src/main/java/ch/qos/logback/access/db/script/mysql.sql

# Logback: the reliable, generic, fast and flexible logging framework.
# Copyright (C) 1999-2010, QOS.ch. All rights reserved.
#
# See http://logback.qos.ch/license.html for the applicable licensing 
# conditions.

# This SQL script creates the required tables by ch.qos.logback.access.db.DBAppender.
#
# It is intended for MySQL databases. It has been tested on MySQL 5.0.22 with 
# INNODB tables.


BEGIN;
DROP TABLE IF EXISTS access_event_header;
DROP TABLE IF EXISTS access_event;
COMMIT;

BEGIN;
CREATE TABLE ACCESS_EVENT 
(
    timestmp          BIGINT NOT NULL,
    requestURI        VARCHAR(254),
    requestURL        VARCHAR(254),
    remoteHost        VARCHAR(254),
    remoteUser        VARCHAR(254),
    remoteAddr        VARCHAR(254),
    protocol          VARCHAR(254),
    method            VARCHAR(254),
    serverName        VARCHAR(254),
    postContent       VARCHAR(254),
    event_id          BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY
);
COMMIT;

BEGIN;
CREATE TABLE access_event_header
(
    event_id          BIGINT NOT NULL,
    header_key        VARCHAR(254) NOT NULL,
    header_value      VARCHAR(1024),
    PRIMARY KEY(event_id, header_key),
    FOREIGN KEY (event_id) REFERENCES access_event(event_id)
);
COMMIT;

However these tables (access_event and access_event_header) are not the same 3 tables as what the documentation cites (logging_event, logging_event_property, and logging_event_exception). So I'm still at a loss here...

Was it helpful?

Solution 2

I looked at the source code for 1.0.13's DBAppender. The Javadocs for the DBAppender class state:

The DBAppender inserts access events into three database tables in a format independent of the Java programming language.

However when you dig down into the code, the log messages are actually just being appended to access_event_header and access_event, not 3 tables like the Javadocs say.

So, in conclusion:

The logback Javadocs and developer docs are not up to date with the latest release, and do not reflect the true table structure that the DBAppender requires.

OTHER TIPS

You are searching the wrong logback module! logback-classic is the module you are talking about. The script you need can be found below logback-classic\src\main\java\ch\qos\logback\classic\db\<db>.sql.

eg. the mysql.sql:

# Logback: the reliable, generic, fast and flexible logging framework.
# Copyright (C) 1999-2010, QOS.ch. All rights reserved.
#
# See http://logback.qos.ch/license.html for the applicable licensing 
# conditions.

# This SQL script creates the required tables by ch.qos.logback.classic.db.DBAppender.
#
# It is intended for MySQL databases. It has been tested on MySQL 5.1.37 
# on Linux


BEGIN;
DROP TABLE IF EXISTS logging_event_property;
DROP TABLE IF EXISTS logging_event_exception;
DROP TABLE IF EXISTS logging_event;
COMMIT;


BEGIN;
CREATE TABLE logging_event 
  (
    timestmp         BIGINT NOT NULL,
    formatted_message  TEXT NOT NULL,
    logger_name       VARCHAR(254) NOT NULL,
    level_string      VARCHAR(254) NOT NULL,
    thread_name       VARCHAR(254),
    reference_flag    SMALLINT,
    arg0              VARCHAR(254),
    arg1              VARCHAR(254),
    arg2              VARCHAR(254),
    arg3              VARCHAR(254),
    caller_filename   VARCHAR(254) NOT NULL,
    caller_class      VARCHAR(254) NOT NULL,
    caller_method     VARCHAR(254) NOT NULL,
    caller_line       CHAR(4) NOT NULL,
    event_id          BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY
  );
COMMIT;

BEGIN;
CREATE TABLE logging_event_property
  (
    event_id          BIGINT NOT NULL,
    mapped_key        VARCHAR(254) NOT NULL,
    mapped_value      TEXT,
    PRIMARY KEY(event_id, mapped_key),
    FOREIGN KEY (event_id) REFERENCES logging_event(event_id)
  );
COMMIT;

BEGIN;
CREATE TABLE logging_event_exception
  (
    event_id         BIGINT NOT NULL,
    i                SMALLINT NOT NULL,
    trace_line       VARCHAR(254) NOT NULL,
    PRIMARY KEY(event_id, i),
    FOREIGN KEY (event_id) REFERENCES logging_event(event_id)
  );
COMMIT;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top