Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2010-2013 Ning, Inc.
   *
   * Ning licenses this file to you under the Apache License, version 2.0
   * (the "License"); you may not use this file except in compliance with the
   * License.  You may obtain a copy of the License at:
   *
   *    http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
  * License for the specific language governing permissions and limitations
  * under the License.
  */
 
 package com.ning.billing.osgi.bundles.logger;
 
 import java.util.Map;
 
 
 // Inspired by osgi-over-slf4j
 public class KillbillLogWriter implements LogListener {
 
     private static final String UNKNOWN = "[Unknown]";
 
     private final Map<StringLoggerdelegates = new HashMap<StringLogger>();
 
     // Invoked by the log service implementation for each log entry
     public void logged(final LogEntry entry) {
         final Bundle bundle = entry.getBundle();
         final Logger delegate = getDelegateForBundle(bundle);
 
         final ServiceReference serviceReference = entry.getServiceReference();
         final int level = entry.getLevel();
         final String message = entry.getMessage();
         final Throwable exception = entry.getException();
 
         if (serviceReference != null && exception != null) {
             log(delegateserviceReferencelevelmessageexception);
         } else if (serviceReference != null) {
             log(delegateserviceReferencelevelmessage);
         } else if (exception != null) {
             log(delegatelevelmessageexception);
         } else {
             log(delegatelevelmessage);
         }
     }
 
     private Logger getDelegateForBundle(/* @Nullable */ final Bundle bundle) {
         final String loggerName;
         if (bundle != null) {
             final String name = bundle.getSymbolicName();
             Version version = bundle.getVersion();
             if (version == null) {
                 version = .;
             }
             loggerName = name + '.' + version;
         } else {
             loggerName = KillbillLogWriter.class.getName();
         }
 
         if (.get(loggerName) == null) {
             synchronized () {
                 if (.get(loggerName) == null) {
                     .put(loggerName, LoggerFactory.getLogger(loggerName));
                 }
             }
         }
 
         return .get(loggerName);
     }
 
     private void log(final Logger delegatefinal int levelfinal String message) {
         switch (level) {
             case .:
                 delegate.debug(message);
                 break;
             case .:
                 delegate.error(message);
                 break;
             case .:
                 delegate.info(message);
                 break;
             case .:
                 delegate.warn(message);
                 break;
             default:
                 break;
         }
    }
    private void log(final Logger delegatefinal int levelfinal String messagefinal Throwable exception) {
        switch (level) {
            case .:
                delegate.debug(messageexception);
                break;
            case .:
                delegate.error(messageexception);
                break;
            case .:
                delegate.info(messageexception);
                break;
            case .:
                delegate.warn(messageexception);
                break;
            default:
                break;
        }
    }
    private void log(final Logger delegatefinal ServiceReference srfinal int levelfinal String message) {
        switch (level) {
            case .:
                if (delegate.isDebugEnabled()) {
                    delegate.debug(createMessage(srmessage));
                }
                break;
            case .:
                if (delegate.isErrorEnabled()) {
                    delegate.error(createMessage(srmessage));
                }
                break;
            case .:
                if (delegate.isInfoEnabled()) {
                    delegate.info(createMessage(srmessage));
                }
                break;
            case .:
                if (delegate.isWarnEnabled()) {
                    delegate.warn(createMessage(srmessage));
                }
                break;
            default:
                break;
        }
    }
    private void log(final Logger delegatefinal ServiceReference srfinal int levelfinal String messagefinal Throwable exception) {
        switch (level) {
            case .:
                if (delegate.isDebugEnabled()) {
                    delegate.debug(createMessage(srmessage), exception);
                }
                break;
            case .:
                if (delegate.isErrorEnabled()) {
                    delegate.error(createMessage(srmessage), exception);
                }
                break;
            case .:
                if (delegate.isInfoEnabled()) {
                    delegate.info(createMessage(srmessage), exception);
                }
                break;
            case .:
                if (delegate.isWarnEnabled()) {
                    delegate.warn(createMessage(srmessage), exception);
                }
                break;
            default:
                break;
        }
    }

    
Formats the log message to indicate the service sending it, if known.

Parameters:
sr the ServiceReference sending the message.
message The message to log.
Returns:
The formatted log message.
    private String createMessage(final ServiceReference srfinal String message) {
        final StringBuilder output = new StringBuilder();
        if (sr != null) {
            output.append('[').append(sr.toString()).append(']');
        } else {
            output.append();
        }
        output.append(message);
        return output.toString();
    }
New to GrepCode? Check out our FAQ X