Source code

    001/*
    002 * Copyright (c) 2004-2005 QOS.ch
    003 *
    004 * All rights reserved.
    005 *
    006 * Permission is hereby granted, free of charge, to any person obtaining
    007 * a copy of this software and associated documentation files (the
    008 * "Software"), to  deal in  the Software without  restriction, including
    009 * without limitation  the rights to  use, copy, modify,  merge, publish,
    010 * distribute, and/or sell copies of  the Software, and to permit persons
    011 * to whom  the Software is furnished  to do so, provided  that the above
    012 * copyright notice(s) and this permission notice appear in all copies of
    013 * the  Software and  that both  the above  copyright notice(s)  and this
    014 * permission notice appear in supporting documentation.
    015 *
    016 * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
    017 * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
    018 * MERCHANTABILITY, FITNESS FOR  A PARTICULAR PURPOSE AND NONINFRINGEMENT
    019 * OF  THIRD PARTY  RIGHTS. IN  NO EVENT  SHALL THE  COPYRIGHT  HOLDER OR
    020 * HOLDERS  INCLUDED IN  THIS  NOTICE BE  LIABLE  FOR ANY  CLAIM, OR  ANY
    021 * SPECIAL INDIRECT  OR CONSEQUENTIAL DAMAGES, OR  ANY DAMAGES WHATSOEVER
    022 * RESULTING FROM LOSS  OF USE, DATA OR PROFITS, WHETHER  IN AN ACTION OF
    023 * CONTRACT, NEGLIGENCE  OR OTHER TORTIOUS  ACTION, ARISING OUT OF  OR IN
    024 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
    025 *
    026 * Except as  contained in  this notice, the  name of a  copyright holder
    027 * shall not be used in advertising or otherwise to promote the sale, use
    028 * or other dealings in this Software without prior written authorization
    029 * of the copyright holder.
    030 *
    031 */
    032
    033package org.slf4j.osgi.logservice.impl;
    034
    035import org.osgi.framework.Bundle;
    036import org.osgi.framework.ServiceReference;
    037import org.osgi.framework.Version;
    038import org.osgi.service.log.LogService;
    039import org.slf4j.Logger;
    040import org.slf4j.LoggerFactory;
    041
    042/**
    043 * <code>LogServiceImpl</code> is a simple OSGi LogService implementation that delegates to a slf4j
    044 * Logger.
    045 *
    046 * @author John Conlon
    047 * @author Matt Bishop
    048 */
    049public class LogServiceImpl implements LogService {
    050
    051    private static final String UNKNOWN = "[Unknown]";
    052
    053    private final Logger delegate;
    054
    055    /**
    056     * Creates a new instance of LogServiceImpl.
    057     *
    058     * @param bundle The bundle to create a new LogService for.
    059     */
    060    public LogServiceImpl(Bundle bundle) {
    061
    062        String name = bundle.getSymbolicName();
    063        Version version = bundle.getVersion();
    064        if (version == null) {
    065            version = Version.emptyVersion;
    066        }
    067        delegate = LoggerFactory.getLogger(name + '.' + version);
    068    }
    069
    070    /*
    071     * (non-Javadoc)
    072     *
    073     * @see org.osgi.service.log.LogService#log(int, java.lang.String)
    074     */
    075    public void log(int level, String message) {
    076
    077        switch (level) {
    078        case LOG_DEBUG:
    079            delegate.debug(message);
    080            break;
    081        case LOG_ERROR:
    082            delegate.error(message);
    083            break;
    084        case LOG_INFO:
    085            delegate.info(message);
    086            break;
    087        case LOG_WARNING:
    088            delegate.warn(message);
    089            break;
    090        default:
    091            break;
    092        }
    093    }
    094
    095    /*
    096     * (non-Javadoc)
    097     *
    098     * @see org.osgi.service.log.LogService#log(int, java.lang.String, java.lang.Throwable)
    099     */
    100    public void log(int level, String message, Throwable exception) {
    101
    102        switch (level) {
    103        case LOG_DEBUG:
    104            delegate.debug(message, exception);
    105            break;
    106        case LOG_ERROR:
    107            delegate.error(message, exception);
    108            break;
    109        case LOG_INFO:
    110            delegate.info(message, exception);
    111            break;
    112        case LOG_WARNING:
    113            delegate.warn(message, exception);
    114            break;
    115        default:
    116            break;
    117        }
    118    }
    119
    120    /*
    121     * (non-Javadoc)
    122     *
    123     * @see org.osgi.service.log.LogService#log(org.osgi.framework.ServiceReference, int, java.lang.String)
    124     */
    125    public void log(ServiceReference sr, int level, String message) {
    126
    127        switch (level) {
    128        case LOG_DEBUG:
    129            if (delegate.isDebugEnabled()) {
    130                delegate.debug(createMessage(sr, message));
    131            }
    132            break;
    133        case LOG_ERROR:
    134            if (delegate.isErrorEnabled()) {
    135                delegate.error(createMessage(sr, message));
    136            }
    137            break;
    138        case LOG_INFO:
    139            if (delegate.isInfoEnabled()) {
    140                delegate.info(createMessage(sr, message));
    141            }
    142            break;
    143        case LOG_WARNING:
    144            if (delegate.isWarnEnabled()) {
    145                delegate.warn(createMessage(sr, message));
    146            }
    147            break;
    148        default:
    149            break;
    150        }
    151    }
    152
    153    /**
    154     * Formats the log message to indicate the service sending it, if known.
    155     *
    156     * @param sr the ServiceReference sending the message.
    157     * @param message The message to log.
    158     * @return The formatted log message.
    159     */
    160    private String createMessage(ServiceReference sr, String message) {
    161
    162        StringBuilder output = new StringBuilder();
    163        if (sr != null) {
    164            output.append('[').append(sr.toString()).append(']');
    165        } else {
    166            output.append(UNKNOWN);
    167        }
    168        output.append(message);
    169
    170        return output.toString();
    171    }
    172
    173    /*
    174     * (non-Javadoc)
    175     *
    176     * @see org.osgi.service.log.LogService#log(org.osgi.framework.ServiceReference, int, java.lang.String,
    177     * java.lang.Throwable)
    178     */
    179    public void log(ServiceReference sr, int level, String message, Throwable exception) {
    180
    181        switch (level) {
    182        case LOG_DEBUG:
    183            if (delegate.isDebugEnabled()) {
    184                delegate.debug(createMessage(sr, message), exception);
    185            }
    186            break;
    187        case LOG_ERROR:
    188            if (delegate.isErrorEnabled()) {
    189                delegate.error(createMessage(sr, message), exception);
    190            }
    191            break;
    192        case LOG_INFO:
    193            if (delegate.isInfoEnabled()) {
    194                delegate.info(createMessage(sr, message), exception);
    195            }
    196            break;
    197        case LOG_WARNING:
    198            if (delegate.isWarnEnabled()) {
    199                delegate.warn(createMessage(sr, message), exception);
    200            }
    201            break;
    202        default:
    203            break;
    204        }
    205    }
    206}