From: Marco Zanon Date: Fri, 2 Aug 2013 10:05:40 +0000 (+0000) Subject: Implemented logging into a database table. X-Git-Tag: 3.0~5 X-Git-Url: https://gitweb.marcozanon.com/?a=commitdiff_plain;h=bc6deea019e1186a2d98fe01894b85e0ebc7a842;p=Macaco Implemented logging into a database table. --- diff --git a/src/java/com/marcozanon/macaco/logging/MLogDatabaseTable.java b/src/java/com/marcozanon/macaco/logging/MLogDatabaseTable.java new file mode 100644 index 0000000..19ede6c --- /dev/null +++ b/src/java/com/marcozanon/macaco/logging/MLogDatabaseTable.java @@ -0,0 +1,134 @@ +/** + * Macaco + * Copyright (c) 2009-2013 Marco Zanon . + * Released under MIT license (see LICENSE for details). + */ + +package com.marcozanon.macaco.logging; + +import com.marcozanon.macaco.sql.MConnectionSqlException; +import com.marcozanon.macaco.sql.MSqlConnection; +import com.marcozanon.macaco.sql.MSqlConnectionGenerator; +import com.marcozanon.macaco.sql.MSqlTable; +import com.marcozanon.macaco.sql.MStatementSqlException; +import com.marcozanon.macaco.text.MText; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.LinkedHashMap; + +public class MLogDatabaseTable extends MLogTarget { + + protected MSqlConnectionGenerator sqlConnectionGenerator = null; + + protected String logDatabaseTable = null; + protected String logDatabaseTablePrimaryKey = null; + protected String logDatabaseField = null; + + protected MSqlConnection sqlConnection = null; + + /* */ + + public MLogDatabaseTable(MSqlConnectionGenerator sqlConnectionGenerator, String logDatabaseTable, String logDatabaseTablePrimaryKey, String logDatabaseField) throws MLoggingException { + super(); + // + if (null == sqlConnectionGenerator) { + throw new IllegalArgumentException("Invalid 'sqlConnectionGenerator': null."); + } + if (MText.isEmpty(logDatabaseTable)) { + throw new IllegalArgumentException("Invalid 'logDatabaseTable': null or empty."); + } + if (MText.isEmpty(logDatabaseTablePrimaryKey)) { + throw new IllegalArgumentException("Invalid 'logDatabaseTablePrimaryKey': null or empty."); + } + if (MText.isEmpty(logDatabaseField)) { + throw new IllegalArgumentException("Invalid 'logDatabaseField': null or empty."); + } + // + this.sqlConnectionGenerator = sqlConnectionGenerator; + this.logDatabaseTable = logDatabaseTable; + this.logDatabaseTablePrimaryKey = logDatabaseTablePrimaryKey; + this.logDatabaseField = logDatabaseField; + } + + public void close() throws MLoggingException { + try { + this.getSqlConnectionReference().close(); + } + catch (MConnectionSqlException exception) { + throw new MLoggingException("Could not close Sql connection.", exception); + } + } + + protected void finalize() { + try { + this.close(); + } + catch (Exception exception) { + } + } + + /* Sql connection generator */ + + protected MSqlConnectionGenerator getSqlConnectionGeneratorReference() { + return this.sqlConnectionGenerator; + } + + /* Logging database parameters */ + + protected String getLogDatabaseTable() { + return this.logDatabaseTable; + } + + protected String getLogDatabaseTablePrimaryKey() { + return this.logDatabaseTablePrimaryKey; + } + + protected String getLogDatabaseField() { + return this.logDatabaseField; + } + + /* Sql connection */ + + protected MSqlConnection getSqlConnectionReference() { + return this.sqlConnection; + } + + /* Output */ + + public void appendMessage(String message) throws MLoggingException { + this.appendMessage(message, 0); + } + + public void appendMessage(String message, int indentation) throws MLoggingException { + if (null == message) { + throw new IllegalArgumentException("Invalid 'message': null."); + } + if (0 > indentation) { + throw new IllegalArgumentException(String.format("Invalid 'indentation': %s: cannot be negative.", indentation)); + } + // + try { + String timestamp = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date()); + // + StringBuilder separator = new StringBuilder(" "); + for (int i = 0; i < indentation; i++) { + separator.append(" "); + } + // + if (null == this.getSqlConnectionReference()) { + this.sqlConnection = this.getSqlConnectionGeneratorReference().getConnection(); + } + // + LinkedHashMap p = new LinkedHashMap(); + p.put(this.getLogDatabaseField(), timestamp + separator + message); + (new MSqlTable(this.getSqlConnectionReference(), this.getLogDatabaseTable(), this.getLogDatabaseTablePrimaryKey())).setRecord(p, null); + } + catch (MConnectionSqlException exception) { + throw new MLoggingException("Could not write to database table.", exception); + } + catch (MStatementSqlException exception) { + throw new MLoggingException("Could not write to database table.", exception); + } + } + +}