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 java.nio.ByteBuffer;
    23
    24  import org.apache.mina.api.IdleStatus;
    25  import org.apache.mina.api.IoFilter;
    26  import org.apache.mina.api.IoSession;
    27  import org.apache.mina.filterchain.ReadFilterChainController;
    28  import org.apache.mina.filterchain.WriteFilterChainController;
    29  import org.apache.mina.session.WriteRequest;
    30  import org.apache.mina.util.ByteBufferDumper;
    31  import org.slf4j.Logger;
    32  import org.slf4j.LoggerFactory;
    33
    34  /**
    35   * A simple filter logging incoming events.
    36   *
    37   * @author jvermillar
    38   *
    39   */
    40  public class LoggingFilter implements IoFilter {
    41
    42      /** The logger */
    43      private final Logger logger;
    44
    45      /** The log level for the messageWritting event. Default to INFO. */
    46      private LogLevel messageWritingLevel = LogLevel.INFO;
    47
    48      /** The log level for the messageSent event. Default to INFO. */
    49      private LogLevel messageSentLevel = LogLevel.INFO;
    50
    51      /** The log level for the messageReceived event. Default to INFO. */
    52      private LogLevel messageReceivedLevel = LogLevel.INFO;
    53
    54      /** The log level for the sessionOpened event. Default to INFO. */
    55      private LogLevel sessionOpenedLevel = LogLevel.INFO;
    56
    57      /** The log level for the sessionClosed event. Default to INFO. */
    58      private LogLevel sessionClosedLevel = LogLevel.INFO;
    59
    60      /** The log level for the sessionIdle event. Default to INFO. */
    61      private LogLevel sessionIdleLevel = LogLevel.INFO;
    62
    63      /**
    64       * Default Constructor.
    65       */
    66      public LoggingFilter() {
    67          this(LoggingFilter.class.getName());
    68      }
    69
    70      /**
    71       * Create a new LoggingFilter using a class name
    72       *
    73       * @param clazz the class which name will be used to create the logger
    74       */
    75      public LoggingFilter(final Class<?> clazz) {
    76          this(clazz.getName());
    77      }
    78
    79      /**
    80       * Create a new LoggingFilter using a name
    81       *
    82       * @param name the name used to create the logger. If null, will default to "LoggingFilter"
    83       */
    84      public LoggingFilter(final String name) {
    85          if (name == null) {
    86              logger = LoggerFactory.getLogger(LoggingFilter.class.getName());
    87          } else {
    88              logger = LoggerFactory.getLogger(name);
    89          }
    90      }
    91
    92      /**
    93       * Log if the logger and the current event log level are compatible. We log a formated message and its parameters.
    94       *
    95       * @param eventLevel the event log level as requested by the user
    96       * @param message the formated message to log
    97       * @param param the parameter injected into the message
    98       */
    99      private void log(final LogLevel eventLevel, final String message, final Object param) {
    100         switch (eventLevel) {
    101         case TRACE:
    102             logger.trace(message, param);
    103             return;
    104         case DEBUG:
    105             logger.debug(message, param);
    106             return;
    107         case INFO:
    108             logger.info(message, param);
    109             return;
    110         case WARN:
    111             logger.warn(message, param);
    112             return;
    113         case ERROR:
    114             logger.error(message, param);
    115             return;
    116         default:
    117             return;
    118         }
    119     }
    120
    121     /**
    122      * Log if the logger and the current event log level are compatible. We log a simple message.
    123      *
    124      * @param eventLevel the event log level as requested by the user
    125      * @param message the message to log
    126      */
    127     private void log(final LogLevel eventLevel, final String message) {
    128         switch (eventLevel) {
    129         case TRACE:
    130             logger.trace(message);
    131             return;
    132         case DEBUG:
    133             logger.debug(message);
    134             return;
    135         case INFO:
    136             logger.info(message);
    137             return;
    138         case WARN:
    139             logger.warn(message);
    140             return;
    141         case ERROR:
    142             logger.error(message);
    143             return;
    144         default:
    145             return;
    146         }
    147     }
    148
    149     /**
    150      * {@inheritDoc}
    151      */
    152     @Override
    153     public void sessionOpened(final IoSession session) {
    154         log(sessionOpenedLevel, "OPENED");
    155
    156     }
    157
    158     /**
    159      * {@inheritDoc}
    160      */
    161     @Override
    162     public void sessionClosed(final IoSession session) {
    163         log(sessionClosedLevel, "CLOSED");
    164     }
    165
    166     /**
    167      * {@inheritDoc}
    168      */
    169     @Override
    170     public void sessionIdle(final IoSession session, final IdleStatus status) {
    171         log(sessionIdleLevel, "IDLE");
    172     }
    173
    174     /**
    175      * {@inheritDoc}
    176      */
    177     @Override
    178     public void messageSent(final IoSession session, final Object message) {
    179         log(messageSentLevel, "IDLE");
    180     }
    181
    182     /**
    183      * {@inheritDoc}
    184      */
    185     @Override
    186     public void messageReceived(final IoSession session, final Object message,
    187             final ReadFilterChainController controller) {
    188         if (message instanceof ByteBuffer) {
    189             log(messageReceivedLevel, "RECEIVED: {}", ByteBufferDumper.dump((ByteBuffer) message));
    190         } else {
    191             log(messageReceivedLevel, "RECEIVED: {}", message);
    192         }
    193
    194         controller.callReadNextFilter(message);
    195     }
    196
    197     /**
    198      * {@inheritDoc}
    199      */
    200     @Override
    201     public void messageWriting(final IoSession session, final WriteRequest message,
    202             final WriteFilterChainController controller) {
    203         log(messageReceivedLevel, "WRITTING: {}", message);
    204         controller.callWriteNextFilter(message);
    205     }
    206
    207     // =========================
    208     // SETTERS & GETTERS
    209     // =========================
    210
    211     /**
    212      * Set the LogLevel for the MessageReceived event.
    213      *
    214      * @param level The LogLevel to set
    215      */
    216     public void setMessageReceivedLogLevel(final LogLevel level) {
    217         messageReceivedLevel = level;
    218     }
    219
    220     /**
    221      * Get the LogLevel for the MessageReceived event.
    222      *
    223      * @return The LogLevel for the MessageReceived eventType
    224      */
    225     public LogLevel getMessageReceivedLogLevel() {
    226         return messageReceivedLevel;
    227     }
    228
    229     /**
    230      * Set the LogLevel for the MessageWriting event.
    231      *
    232      * @param level The LogLevel to set
    233      */
    234     public void setMessageWritingLogLevel(final LogLevel level) {
    235         messageWritingLevel = level;
    236     }
    237
    238     /**
    239      * Get the LogLevel for the MessageWriting event.
    240      *
    241      * @return The LogLevel for the MessageWriting eventType
    242      */
    243     public LogLevel getMessageWritingLogLevel() {
    244         return messageWritingLevel;
    245     }
    246
    247     /**
    248      * Set the LogLevel for the SessionOpened event.
    249      *
    250      * @param level The LogLevel to set
    251      */
    252     public void setSessionOpenedLogLevel(final LogLevel level) {
    253         sessionOpenedLevel = level;
    254     }
    255
    256     /**
    257      * Get the LogLevel for the SessionOpened event.
    258      *
    259      * @return The LogLevel for the SessionOpened eventType
    260      */
    261     public LogLevel getSessionOpenedLogLevel() {
    262         return sessionOpenedLevel;
    263     }
    264
    265     /**
    266      * Set the LogLevel for the SessionIdle event.
    267      *
    268      * @param level The LogLevel to set
    269      */
    270     public void setSessionIdleLogLevel(final LogLevel level) {
    271         sessionIdleLevel = level;
    272     }
    273
    274     /**
    275      * Get the LogLevel for the SessionIdle event.
    276      *
    277      * @return The LogLevel for the SessionIdle eventType
    278      */
    279     public LogLevel getSessionIdleLogLevel() {
    280         return sessionIdleLevel;
    281     }
    282
    283     /**
    284      * Set the LogLevel for the SessionClosed event.
    285      *
    286      * @param level The LogLevel to set
    287      */
    288     public void setSessionClosedLogLevel(final LogLevel level) {
    289         sessionClosedLevel = level;
    290     }
    291
    292     /**
    293      * Get the LogLevel for the SessionClosed event.
    294      *
    295      * @return The LogLevel for the SessionClosed eventType
    296      */
    297     public LogLevel getSessionClosedLogLevel() {
    298         return sessionClosedLevel;
    299     }
    300
    301     /**
    302      * Get the LogLevel for the messageSent event.
    303      *
    304      * @return The LogLevel for the messageSent eventType
    305      */
    306     public LogLevel getMessageSentLevel() {
    307         return messageSentLevel;
    308     }
    309
    310     /**
    311      * Set the LogLevel for the messageSent event.
    312      *
    313      * @param level The LogLevel to set
    314      */
    315     public void setMessageSentLevel(final LogLevel messageSentLevel) {
    316         this.messageSentLevel = messageSentLevel;
    317     }
    318
    319 }