View Javadoc

1   /*
2    * Copyright (c) 2004 UNINETT FAS
3    *
4    * This program is free software; you can redistribute it and/or modify it
5    * under the terms of the GNU General Public License as published by the Free
6    * Software Foundation; either version 2 of the License, or (at your option)
7    * any later version.
8    *
9    * This program is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11   * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12   * more details.
13   *
14   * You should have received a copy of the GNU General Public License along with
15   * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16   * Place - Suite 330, Boston, MA 02111-1307, USA.
17   *
18   * $Id: AccessLogger.java,v 1.9 2004/12/15 20:51:31 jk Exp $
19   *
20   */
21  
22  package no.feide.moria.log;
23  
24  import java.io.Serializable;
25  
26  import org.apache.log4j.Logger;
27  
28  /***
29   * Logs system events in a strict format that may later
30   * be used for usage statistics generation.
31   *
32   * Logging is done at log4j level WARN.  If the loglevel of
33   * log4j is set above this, no log-entries will be written.
34   *
35   * The format of the log-lines is the following:
36   * <pre>
37   * [2004-04-30 17:10:19,046] "BAD USER CREDENTIALS" "no.feide.test" "demo@feide.no" "235892791" "350215527"
38   *
39   * [Timestamp] "STATUS" "service principal" "userid" "incoming ticket" "outgoing ticket"
40   * </pre>
41   *
42   * @author Bj&oslash;rn Ola Smievoll &lt;b.o@smievoll.no&gt;
43   * @version $Revision: 1.9 $
44   */
45  public final class AccessLogger implements Serializable {
46  
47      /***
48       * The name of this logger.
49       * Same for all classes, so we can be redirect output to a common file.
50       */
51      private static final Class ACCESS_LOGGER_CLASS = AccessLogger.class;
52  
53      /***
54       * Log to this logger.
55       * Transient, so the class can be serialized.
56       */
57      private transient Logger logger = null;
58  
59      /***
60       * Default constructor.
61       */
62      public AccessLogger() {
63          this.logger = Logger.getLogger(ACCESS_LOGGER_CLASS);
64      }
65  
66      /***
67       * Logs user-initiated access (user interaction through the
68       * web interface).
69       *
70       * @param status
71       *            Indicates the type of event.
72       * @param servicePrincipal
73       *            The id of the service that is responsible for this operation.
74       * @param userId
75       *            The id of the user, may be null if unknown at time of event.
76       * @param incomingTicketId
77       *            The id of the ticket given with the request.
78       * @param outgoingTicketId
79       *            The id of the potentially returned ticket, may be null.
80       */
81      public void logUser(final AccessStatusType status, final String servicePrincipal, final String userId,
82              final String incomingTicketId, final String outgoingTicketId) {
83          /* Generate the log message and log it. */
84          getLogger().warn(generateLogMessage(status, servicePrincipal, userId, incomingTicketId, outgoingTicketId));
85      }
86  
87      /***
88       * Logs service-initiated access.
89       *
90       * @param status
91       *            Indicates the type of event.
92       * @param servicePrincipal
93       *            The id of the service that is performing the operation.
94       * @param incomingTicketId
95       *            The id of the ticket given with the request.
96       * @param outgoingTicketId
97       *            The id of the potentially returned ticket, may be null.
98       */
99      public void logService(final AccessStatusType status, final String servicePrincipal, final String incomingTicketId,
100             final String outgoingTicketId) {
101 
102         /* Generate the log message and log it. */
103         getLogger().warn(generateLogMessage(status, servicePrincipal, null, incomingTicketId, outgoingTicketId));
104     }
105 
106     /***
107      * Generates log messages in the correct format.
108      *
109      * @param status
110      *            Indicates the type of event.
111      * @param servicePrincipal
112      *            The id of the service that is performing the operation.
113      * @param userId
114      *            The id of the user.
115      * @param incomingTicketId
116      *            The id of the ticket given with the request.
117      * @param outgoingTicketId
118      *            The id of the potentially returned ticket, may be null.
119      * @return The string to be logged.
120      */
121     private String generateLogMessage(final AccessStatusType status, final String servicePrincipal, final String userId,
122             final String incomingTicketId, final String outgoingTicketId) {
123 
124         StringBuffer buffer = new StringBuffer();
125 
126         /* Add default value "-" if variabel is null */
127         buffer.append(status != null ? "\"" + status + "\" " : "\"-\" ");
128         buffer.append(servicePrincipal != null ? "\"" + servicePrincipal + "\" " : "\"-\" ");
129         buffer.append(userId != null ? "\"" + userId + "\" " : "\"-\" ");
130         buffer.append(incomingTicketId != null ? "\"" + incomingTicketId + "\" " : "\"-\" ");
131         buffer.append(outgoingTicketId != null ? "\"" + outgoingTicketId + "\" " : "\"-\" ");
132 
133         return buffer.toString();
134     }
135 
136     /***
137      * Returns the logger, instantiates it if not already done.
138      * Private, so that nothing is able to override the formatting that is
139      * done by generateLogMessage.
140      *
141      * @return The logger instance of this class.
142      */
143     private Logger getLogger() {
144         if (logger == null)
145             logger = Logger.getLogger(ACCESS_LOGGER_CLASS);
146 
147         return logger;
148     }
149 }