View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.filter.logging;
21  
22  import org.apache.mina.core.filterchain.IoFilterAdapter;
23  import org.apache.mina.core.session.IdleStatus;
24  import org.apache.mina.core.session.IoEventType;
25  import org.apache.mina.core.session.IoSession;
26  import org.apache.mina.core.write.WriteRequest;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  
30  /**
31   * Logs all MINA protocol events.  Each event can be
32   * tuned to use a different level based on the user's specific requirements.  Methods
33   * are in place that allow the user to use either the get or set method for each event
34   * and pass in the {@link IoEventType} and the {@link LogLevel}.
35   *
36   * By default, all events are logged to the {@link LogLevel#INFO} level except
37   * {@link IoFilterAdapter#exceptionCaught(IoFilter.NextFilter, IoSession, Throwable)},
38   * which is logged to {@link LogLevel#WARN}.
39   *
40   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
41   * @org.apache.xbean.XBean
42   */
43  public class LoggingFilter extends IoFilterAdapter {
44      /** The logger name */
45      private final String name;
46      
47      /** The logger */
48      private final Logger logger;
49      
50      /** The log level for the exceptionCaught event. Default to WARN. */
51      private LogLevel exceptionCaughtLevel = LogLevel.WARN;
52      
53      /** The log level for the messageSent event. Default to INFO. */
54      private LogLevel messageSentLevel = LogLevel.INFO;
55      
56      /** The log level for the messageReceived event. Default to INFO. */
57      private LogLevel messageReceivedLevel = LogLevel.INFO;
58      
59      /** The log level for the sessionCreated event. Default to INFO. */
60      private LogLevel sessionCreatedLevel = LogLevel.INFO;
61      
62      /** The log level for the sessionOpened event. Default to INFO. */
63      private LogLevel sessionOpenedLevel = LogLevel.INFO;
64      
65      /** The log level for the sessionIdle event. Default to INFO. */
66      private LogLevel sessionIdleLevel = LogLevel.INFO;
67      
68      /** The log level for the sessionClosed event. Default to INFO. */
69      private LogLevel sessionClosedLevel = LogLevel.INFO;
70      
71      /**
72       * Default Constructor.
73       */
74      public LoggingFilter() {
75          this(LoggingFilter.class.getName());
76      }
77      
78      /**
79       * Create a new NoopFilter using a class name
80       * 
81       * @param clazz the cass which name will be used to create the logger
82       */
83      public LoggingFilter(Class<?> clazz) {
84          this(clazz.getName());
85      }
86  
87      /**
88       * Create a new NoopFilter using a name
89       * 
90       * @param name the name used to create the logger. If null, will default to "NoopFilter"
91       */
92      public LoggingFilter(String name) {
93          if (name == null) {
94              this.name = LoggingFilter.class.getName();
95          } else {
96              this.name = name;
97          }
98          
99          logger = LoggerFactory.getLogger(name);
100     }
101 
102     /**
103      * @return The logger's name
104      */
105     public String getName() {
106         return name;
107     }
108     
109     /**
110      * Log if the logger and the current event log level are compatible. We log
111      * a message and an exception.
112      * 
113      * @param eventLevel the event log level as requested by the user
114      * @param message the message to log
115      * @param cause the exception cause to log
116      */
117     private void log(LogLevel eventLevel, String message, Throwable cause) {
118         switch (eventLevel) {
119             case TRACE : logger.trace(message, cause); return;
120             case DEBUG : logger.debug(message, cause); return;
121             case INFO  : logger.info(message, cause); return;
122             case WARN  : logger.warn(message, cause); return;
123             case ERROR : logger.error(message, cause); return;
124             default    : return;
125         }
126     }
127 
128     /**
129      * Log if the logger and the current event log level are compatible. We log
130      * a formated message and its parameters. 
131      * 
132      * @param eventLevel the event log level as requested by the user
133      * @param message the formated message to log
134      * @param param the parameter injected into the message
135      */
136     private void log(LogLevel eventLevel, String message, Object param) {
137         switch (eventLevel) {
138             case TRACE : logger.trace(message, param); return;
139             case DEBUG : logger.debug(message, param); return;
140             case INFO  : logger.info(message, param); return;
141             case WARN  : logger.warn(message, param); return;
142             case ERROR : logger.error(message, param); return;
143             default    : return;
144         }
145     }
146 
147     /**
148      * Log if the logger and the current event log level are compatible. We log
149      * a simple message. 
150      * 
151      * @param eventLevel the event log level as requested by the user
152      * @param message the message to log
153      */
154     private void log(LogLevel eventLevel, String message) {
155         switch (eventLevel) {
156             case TRACE : logger.trace(message); return;
157             case DEBUG : logger.debug(message); return;
158             case INFO  : logger.info(message); return;
159             case WARN  : logger.warn(message); return;
160             case ERROR : logger.error(message); return;
161             default    : return;
162         }
163     }
164 
165     @Override
166     public void exceptionCaught(NextFilter nextFilter, IoSession session,
167             Throwable cause) throws Exception {
168         log(exceptionCaughtLevel, "EXCEPTION :", cause);
169         nextFilter.exceptionCaught(session, cause);
170     }
171 
172     @Override
173     public void messageReceived(NextFilter nextFilter, IoSession session,
174             Object message) throws Exception {
175         log(messageReceivedLevel, "RECEIVED: {}", message );
176         nextFilter.messageReceived(session, message);
177     }
178 
179     @Override
180     public void messageSent(NextFilter nextFilter, IoSession session,
181             WriteRequest writeRequest) throws Exception {
182         log(messageSentLevel, "SENT: {}", writeRequest.getMessage() );
183         nextFilter.messageSent(session, writeRequest);
184     }
185 
186     @Override
187     public void sessionCreated(NextFilter nextFilter, IoSession session)
188             throws Exception {
189         log(sessionCreatedLevel, "CREATED");
190         nextFilter.sessionCreated(session);
191     }
192 
193     @Override
194     public void sessionOpened(NextFilter nextFilter, IoSession session)
195     throws Exception {
196         log(sessionOpenedLevel, "OPENED");
197         nextFilter.sessionOpened(session);
198     }
199 
200     @Override
201     public void sessionIdle(NextFilter nextFilter, IoSession session,
202             IdleStatus status) throws Exception {
203         log(sessionIdleLevel, "IDLE");
204         nextFilter.sessionIdle(session, status);
205     }
206 
207     @Override
208     public void sessionClosed(NextFilter nextFilter, IoSession session) throws Exception {
209         log(sessionClosedLevel, "CLOSED");
210         nextFilter.sessionClosed(session);
211     }
212     
213     /**
214      * Set the LogLevel for the ExceptionCaught event.
215      * 
216      * @param level The LogLevel to set
217      */
218     public void setExceptionCaughtLogLevel(LogLevel level) {
219         exceptionCaughtLevel = level;
220     }
221     
222     /**
223      * Get the LogLevel for the ExceptionCaught event.
224      * 
225      * @return The LogLevel for the ExceptionCaught eventType
226      */
227     public LogLevel getExceptionCaughtLogLevel() {
228         return exceptionCaughtLevel;
229     }
230     
231     /**
232      * Set the LogLevel for the MessageReceived event.
233      * 
234      * @param level The LogLevel to set
235      */
236     public void setMessageReceivedLogLevel(LogLevel level) {
237         messageReceivedLevel = level;
238     }
239     
240     /**
241      * Get the LogLevel for the MessageReceived event.
242      * 
243      * @return The LogLevel for the MessageReceived eventType
244      */
245     public LogLevel getMessageReceivedLogLevel() {
246         return messageReceivedLevel;
247     }
248     
249     /**
250      * Set the LogLevel for the MessageSent event.
251      * 
252      * @param level The LogLevel to set
253      */
254     public void setMessageSentLogLevel(LogLevel level) {
255         messageSentLevel = level;
256     }
257     
258     /**
259      * Get the LogLevel for the MessageSent event.
260      * 
261      * @return The LogLevel for the MessageSent eventType
262      */
263     public LogLevel getMessageSentLogLevel() {
264         return messageSentLevel;
265     }
266     
267     /**
268      * Set the LogLevel for the SessionCreated event.
269      * 
270      * @param level The LogLevel to set
271      */
272     public void setSessionCreatedLogLevel(LogLevel level) {
273         sessionCreatedLevel = level;
274     }
275     
276     /**
277      * Get the LogLevel for the SessionCreated event.
278      * 
279      * @return The LogLevel for the SessionCreated eventType
280      */
281     public LogLevel getSessionCreatedLogLevel() {
282         return sessionCreatedLevel;
283     }
284     
285     /**
286      * Set the LogLevel for the SessionOpened event.
287      * 
288      * @param level The LogLevel to set
289      */
290     public void setSessionOpenedLogLevel(LogLevel level) {
291         sessionOpenedLevel = level;
292     }
293     
294     /**
295      * Get the LogLevel for the SessionOpened event.
296      * 
297      * @return The LogLevel for the SessionOpened eventType
298      */
299     public LogLevel getSessionOpenedLogLevel() {
300         return sessionOpenedLevel;
301     }
302     
303     /**
304      * Set the LogLevel for the SessionIdle event.
305      * 
306      * @param level The LogLevel to set
307      */
308     public void setSessionIdleLogLevel(LogLevel level) {
309         sessionIdleLevel = level;
310     }
311     
312     /**
313      * Get the LogLevel for the SessionIdle event.
314      * 
315      * @return The LogLevel for the SessionIdle eventType
316      */
317     public LogLevel getSessionIdleLogLevel() {
318         return sessionIdleLevel;
319     }
320     
321     /**
322      * Set the LogLevel for the SessionClosed event.
323      * 
324      * @param level The LogLevel to set
325      */
326     public void setSessionClosedLogLevel(LogLevel level) {
327         sessionClosedLevel = level;
328     }
329 
330     /**
331      * Get the LogLevel for the SessionClosed event.
332      * 
333      * @return The LogLevel for the SessionClosed eventType
334      */
335     public LogLevel getSessionClosedLogLevel() {
336         return sessionClosedLevel;
337     }
338 }