01: /*
02: * CoadunationLib: The coaduntion implementation library.
03: * Copyright (C) 2007 Rift IT Contracting
04: *
05: * This library is free software; you can redistribute it and/or
06: * modify it under the terms of the GNU Lesser General Public
07: * License as published by the Free Software Foundation; either
08: * version 2.1 of the License, or (at your option) any later version.
09: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library; if not, write to the Free Software
17: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18: *
19: * BasicFormatter.java
20: */
21:
22: // package path
23: package com.rift.coad.lib.audit.basic;
24:
25: // java imports
26: import java.text.SimpleDateFormat;
27:
28: // log4j import
29: import org.apache.log4j.Logger;
30:
31: // coadunation imports
32: import com.rift.coad.lib.audit.AuditTrailFormatter;
33: import com.rift.coad.lib.audit.AuditTrailEntry;
34: import com.rift.coad.lib.configuration.Configuration;
35: import com.rift.coad.lib.configuration.ConfigurationFactory;
36:
37: /**
38: * This object is reponsible for logging an audit trail entry using the standard
39: * log4j objects.
40: *
41: * @author brett chaldecott
42: */
43: public class BasicFormatter implements AuditTrailFormatter {
44:
45: // class constants
46: private final static String DATE_FORMAT = "DateFormat";
47: private final static String DEFAULT_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
48:
49: // private member variables
50: private Logger log = Logger.getLogger(BasicFormatter.class
51: .getName());
52: private SimpleDateFormat format = null;
53:
54: /**
55: * Creates a new instance of BasicFormatter
56: */
57: public BasicFormatter() {
58: try {
59: Configuration config = ConfigurationFactory.getInstance()
60: .getConfig(BasicFormatter.class);
61: format = new SimpleDateFormat(config.getString(DATE_FORMAT,
62: DEFAULT_DATE_FORMAT));
63: } catch (Exception ex) {
64: log.warn(
65: "Failed to retrieve the simple date format from the "
66: + "configuration defaulting to ["
67: + DEFAULT_DATE_FORMAT + "]", ex);
68: format = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
69: }
70: }
71:
72: /**
73: * This method is responsible for formatting an audit trail entry.
74: *
75: * @param entry The entry to add to the audit trail.
76: */
77: public void format(AuditTrailEntry entry) {
78: if (entry.getCause() == null) {
79: log.info(String.format("[%s][Success][%s][%s][%s]", format
80: .format(entry.getRecordTime()), entry.getUser(),
81: entry.getTarget(), entry.getMethod()));
82: } else {
83: log
84: .info(String.format(
85: "[%s][Failure][%s][%s][%s][%s]", format
86: .format(entry.getRecordTime()),
87: entry.getUser(), entry.getTarget(), entry
88: .getMethod(), entry.getCause()
89: .getMessage()));
90: }
91: }
92:
93: }
|