Java tutorial
/** * Copyright (C) 2013 Seajas, the Netherlands. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License version 3, as * published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package com.seajas.search.utilities.logging; import ch.qos.logback.classic.Level; import ch.qos.logback.classic.Logger; import ch.qos.logback.classic.LoggerContext; import ch.qos.logback.classic.spi.ILoggingEvent; import ch.qos.logback.core.AppenderBase; import com.seajas.search.utilities.logging.dao.LoggingDAO; import java.sql.Connection; import java.sql.Date; import java.sql.PreparedStatement; import java.sql.SQLException; import org.apache.commons.dbcp.BasicDataSource; import org.apache.commons.lang.StringEscapeUtils; import org.slf4j.LoggerFactory; /** * Application logger which delegates messages to an SLF4J logger with a dynamically instantiated JDBC appender. * * @author Jasper van Veghel <jasper@seajas.com> */ public class SearchLogger { /** * The actual logger. */ private final Logger logger; /** * Constants. */ private static final String JDBC_INSERT = "INSERT INTO logging (level, message, creation_date) VALUES(?, ?, ?)"; /** * The log level. */ private String logLevel; /** * Default constructor. * * @param name * @param level * @param dataSource * @param loggingDAO */ public SearchLogger(final String name, final String level, final BasicDataSource dataSource, final LoggingDAO loggingDAO) { LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory(); // Create the actual logger Logger logger = (Logger) LoggerFactory.getLogger(name); logger.setLevel(Level.toLevel(level)); logger.setAdditive(true); // Create the logging appender AppenderBase<ILoggingEvent> appender = new AppenderBase<ILoggingEvent>() { @Override protected void append(final ILoggingEvent event) { Connection connection = null; PreparedStatement statement = null; try { connection = dataSource.getConnection(); statement = connection.prepareStatement(JDBC_INSERT); statement.setString(1, event.getLevel().toString()); statement.setString(2, event.getFormattedMessage()); statement.setDate(3, new Date(event.getTimeStamp())); statement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (statement != null) statement.close(); if (connection != null) connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } }; appender.setContext(loggerContext); appender.start(); // Make this the definitive logger logger.addAppender(appender); this.logger = logger; } /** * Delegates to the actual logger. * * @param message */ public void error(final String message) { logger.error(StringEscapeUtils.escapeSql(message)); } /** * Delegates to the actual logger. * * @param message * @param t */ public void error(final String message, final Throwable t) { logger.error(StringEscapeUtils.escapeSql(message), t); } /** * Delegates to the actual logger. * * @param message */ public void warn(final String message) { logger.warn(StringEscapeUtils.escapeSql(message)); } /** * Delegates to the actual logger. * * @param message * @param t */ public void warn(final String message, final Throwable t) { logger.warn(StringEscapeUtils.escapeSql(message), t); } /** * Delegates to the actual logger. * * @return boolean */ public boolean isInfoEnabled() { return logger.isInfoEnabled(); } /** * Delegates to the actual logger. * * @param message */ public void info(final String message) { logger.info(StringEscapeUtils.escapeSql(message)); } /** * Delegates to the actual logger. * * @param message * @param t */ public void info(final String message, final Throwable t) { logger.info(StringEscapeUtils.escapeSql(message), t); } /** * Delegates to the actual logger. * * @return boolean */ public boolean isDebugEnabled() { return logger.isDebugEnabled(); } /** * Delegates to the actual logger. * * @param message */ public void debug(final String message) { logger.debug(StringEscapeUtils.escapeSql(message)); } /** * Delegates to the actual logger. * * @param message * @param t */ public void debug(final String message, final Throwable t) { logger.debug(StringEscapeUtils.escapeSql(message), t); } /** * Delegates to the actual logger. * * @return boolean */ public boolean isTraceEnabled() { return logger.isTraceEnabled(); } /** * Delegates to the actual logger. * * @param message */ public void trace(final String message) { logger.trace(StringEscapeUtils.escapeSql(message)); } /** * Delegates to the actual logger. * * @param message * @param t */ public void trace(final String message, final Throwable t) { logger.trace(StringEscapeUtils.escapeSql(message), t); } /** * Retrieve the current logLevel. * * @return String */ public String getLogLevel() { return logLevel; } /** * Set the current logLevel. * * @param logLevel */ public void setLogLevel(final String logLevel) { logger.setLevel(Level.toLevel(logLevel)); this.logLevel = logLevel; } }