View Javadoc

    1   /*
    2    * Copyright 2009 Red Hat, Inc.
    3    *
    4    * Red Hat licenses this file to you under the Apache License, version 2.0
    5    * (the "License"); you may not use this file except in compliance with the
    6    * License.  You may obtain a copy of the License at:
    7    *
    8    *    http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
    13   * License for the specific language governing permissions and limitations
    14   * under the License.
    15   */
    16  package org.jboss.netty.logging;
    17
    18  import org.osgi.service.log.LogService;
    19
    20  /**
    21   * <a href="http://www.osgi.org/">OSGi</a> {@link LogService} logger.
    22   *
    23   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
    24   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
    25   *
    26   * @version $Rev: 2080 $, $Date: 2010-01-26 18:04:19 +0900 (Tue, 26 Jan 2010) $
    27   *
    28   */
    29  class OsgiLogger extends AbstractInternalLogger {
    30
    31      private final OsgiLoggerFactory parent;
    32      private final InternalLogger fallback;
    33      private final String name;
    34      private final String prefix;
    35
    36      OsgiLogger(OsgiLoggerFactory parent, String name, InternalLogger fallback) {
    37          this.parent = parent;
    38          this.name = name;
    39          this.fallback = fallback;
    40          prefix = "[" + name + "] ";
    41      }
    42
    43      public void debug(String msg) {
    44          LogService logService = parent.getLogService();
    45          if (logService != null) {
    46              logService.log(LogService.LOG_DEBUG, prefix + msg);
    47          } else {
    48              fallback.debug(msg);
    49          }
    50      }
    51
    52      public void debug(String msg, Throwable cause) {
    53          LogService logService = parent.getLogService();
    54          if (logService != null) {
    55              logService.log(LogService.LOG_DEBUG, prefix + msg, cause);
    56          } else {
    57              fallback.debug(msg, cause);
    58          }
    59      }
    60
    61      public void error(String msg) {
    62          LogService logService = parent.getLogService();
    63          if (logService != null) {
    64              logService.log(LogService.LOG_ERROR, prefix + msg);
    65          } else {
    66              fallback.error(msg);
    67          }
    68      }
    69
    70      public void error(String msg, Throwable cause) {
    71          LogService logService = parent.getLogService();
    72          if (logService != null) {
    73              logService.log(LogService.LOG_ERROR, prefix + msg, cause);
    74          } else {
    75              fallback.error(msg, cause);
    76          }
    77      }
    78
    79      public void info(String msg) {
    80          LogService logService = parent.getLogService();
    81          if (logService != null) {
    82              logService.log(LogService.LOG_INFO, prefix + msg);
    83          } else {
    84              fallback.info(msg);
    85          }
    86      }
    87
    88      public void info(String msg, Throwable cause) {
    89          LogService logService = parent.getLogService();
    90          if (logService != null) {
    91              logService.log(LogService.LOG_INFO, prefix + msg, cause);
    92          } else {
    93              fallback.info(msg, cause);
    94          }
    95      }
    96
    97      public boolean isDebugEnabled() {
    98          return true;
    99      }
    100
    101     public boolean isErrorEnabled() {
    102         return true;
    103     }
    104
    105     public boolean isInfoEnabled() {
    106         return true;
    107     }
    108
    109     public boolean isWarnEnabled() {
    110         return true;
    111     }
    112
    113     public void warn(String msg) {
    114         LogService logService = parent.getLogService();
    115         if (logService != null) {
    116             logService.log(LogService.LOG_WARNING, prefix + msg);
    117         } else {
    118             fallback.warn(msg);
    119         }
    120     }
    121
    122     public void warn(String msg, Throwable cause) {
    123         LogService logService = parent.getLogService();
    124         if (logService != null) {
    125             logService.log(LogService.LOG_WARNING, prefix + msg, cause);
    126         } else {
    127             fallback.warn(msg, cause);
    128         }
    129     }
    130
    131     @Override
    132     public String toString() {
    133         return name;
    134     }
    135 }