--- /dev/null
+/**
+ * Macaco
+ * Copyright (c) 2009-2013 Marco Zanon <info@marcozanon.com>.
+ * 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<String, Object> p = new LinkedHashMap<String, Object>();
+ 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);
+ }
+ }
+
+}