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