Felix Meschberger | c7ee015 | 2008-03-26 09:24:35 +0000 | [diff] [blame] | 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 | package org.apache.felix.http.jetty; |
| 20 | |
| 21 | |
| 22 | import java.util.HashMap; |
| 23 | import java.util.Map; |
| 24 | |
| 25 | import org.mortbay.log.Logger; |
| 26 | import org.osgi.service.log.LogService; |
| 27 | |
| 28 | |
| 29 | public class LogServiceLog implements Logger |
| 30 | { |
| 31 | private static Map loggers = new HashMap(); |
| 32 | |
| 33 | private final String m_name; |
| 34 | |
| 35 | private boolean m_debugEnabled; |
| 36 | |
| 37 | |
| 38 | public LogServiceLog() |
| 39 | { |
| 40 | this( "org.mortbay.log" ); |
| 41 | } |
| 42 | |
| 43 | |
| 44 | public LogServiceLog( String name ) |
| 45 | { |
| 46 | this.m_name = name; |
| 47 | } |
| 48 | |
| 49 | |
| 50 | public Logger getLogger( String name ) |
| 51 | { |
| 52 | Logger logger = ( Logger ) loggers.get( name ); |
| 53 | if ( logger == null ) |
| 54 | { |
| 55 | logger = new LogServiceLog( name ); |
| 56 | logger.setDebugEnabled( isDebugEnabled() ); |
| 57 | loggers.put( name, logger ); |
| 58 | } |
| 59 | return logger; |
| 60 | } |
| 61 | |
| 62 | |
| 63 | public boolean isDebugEnabled() |
| 64 | { |
| 65 | return m_debugEnabled; |
| 66 | } |
| 67 | |
| 68 | |
| 69 | public void setDebugEnabled( boolean enabled ) |
| 70 | { |
| 71 | this.m_debugEnabled = enabled; |
| 72 | } |
| 73 | |
| 74 | |
| 75 | public void debug( String msg, Throwable throwable ) |
| 76 | { |
| 77 | log( LogService.LOG_DEBUG, msg, throwable ); |
| 78 | } |
| 79 | |
| 80 | |
| 81 | public void debug( String msg, Object arg0, Object arg1 ) |
| 82 | { |
| 83 | log( LogService.LOG_DEBUG, format( msg, arg0, arg1 ), null ); |
| 84 | } |
| 85 | |
| 86 | |
| 87 | public void info( String msg, Object arg0, Object arg1 ) |
| 88 | { |
| 89 | log( LogService.LOG_INFO, format( msg, arg0, arg1 ), null ); |
| 90 | } |
| 91 | |
| 92 | |
| 93 | public void warn( String msg, Throwable throwable ) |
| 94 | { |
| 95 | log( LogService.LOG_WARNING, msg, throwable ); |
| 96 | } |
| 97 | |
| 98 | |
| 99 | public void warn( String msg, Object arg0, Object arg1 ) |
| 100 | { |
| 101 | log( LogService.LOG_WARNING, format( msg, arg0, arg1 ), null ); |
| 102 | } |
| 103 | |
| 104 | |
| 105 | public String toString() |
| 106 | { |
| 107 | return m_name; |
| 108 | } |
| 109 | |
| 110 | |
| 111 | private String format( String msg, Object arg0, Object arg1 ) |
| 112 | { |
| 113 | int i0 = msg.indexOf( "{}" ); |
| 114 | int i1 = i0 < 0 ? -1 : msg.indexOf( "{}", i0 + 2 ); |
| 115 | |
| 116 | if ( arg1 != null && i1 >= 0 ) |
| 117 | msg = msg.substring( 0, i1 ) + arg1 + msg.substring( i1 + 2 ); |
| 118 | if ( arg0 != null && i0 >= 0 ) |
| 119 | msg = msg.substring( 0, i0 ) + arg0 + msg.substring( i0 + 2 ); |
| 120 | return msg; |
| 121 | } |
| 122 | |
| 123 | |
| 124 | private void log( int level, String message, Throwable throwable ) |
| 125 | { |
| 126 | Activator.log( level, m_name + ":" + message, throwable ); |
| 127 | } |
| 128 | } |