Richard S. Hall | f5931c1 | 2006-12-15 21:56:11 +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.ipojo.util; |
| 20 | |
| 21 | import org.osgi.framework.BundleContext; |
Richard S. Hall | f5931c1 | 2006-12-15 21:56:11 +0000 | [diff] [blame] | 22 | import org.osgi.framework.ServiceReference; |
| 23 | import org.osgi.service.log.LogService; |
| 24 | |
Richard S. Hall | 6d2ee1a | 2007-03-09 16:50:30 +0000 | [diff] [blame] | 25 | /** |
Richard S. Hall | 8c011c6 | 2007-04-17 14:31:35 +0000 | [diff] [blame] | 26 | * iPOJO Logger. This logger send log message to a log service if presents. |
| 27 | * |
Karl Pauls | d312acc | 2007-06-18 20:38:33 +0000 | [diff] [blame] | 28 | * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a> |
Richard S. Hall | 6d2ee1a | 2007-03-09 16:50:30 +0000 | [diff] [blame] | 29 | */ |
Clement Escoffier | 2e35112 | 2007-07-02 11:36:35 +0000 | [diff] [blame] | 30 | public class Logger { |
Clement Escoffier | ce0e1e5 | 2008-03-28 15:33:36 +0000 | [diff] [blame^] | 31 | |
| 32 | /** |
| 33 | * Ipojo default log level property. |
| 34 | */ |
| 35 | public static final String IPOJO_LOG_LEVEL = "ipojo.log.level"; |
Richard S. Hall | 8c011c6 | 2007-04-17 14:31:35 +0000 | [diff] [blame] | 36 | |
Richard S. Hall | 8c011c6 | 2007-04-17 14:31:35 +0000 | [diff] [blame] | 37 | /** |
| 38 | * Log Level ERROR. |
| 39 | */ |
| 40 | public static final int ERROR = 1; |
| 41 | |
| 42 | /** |
| 43 | * Log Level WARNING. |
Richard S. Hall | 6d2ee1a | 2007-03-09 16:50:30 +0000 | [diff] [blame] | 44 | */ |
Richard S. Hall | f5931c1 | 2006-12-15 21:56:11 +0000 | [diff] [blame] | 45 | public static final int WARNING = 2; |
Richard S. Hall | 8c011c6 | 2007-04-17 14:31:35 +0000 | [diff] [blame] | 46 | |
Richard S. Hall | 6d2ee1a | 2007-03-09 16:50:30 +0000 | [diff] [blame] | 47 | /** |
| 48 | * Log Level INFO. |
| 49 | */ |
Richard S. Hall | f5931c1 | 2006-12-15 21:56:11 +0000 | [diff] [blame] | 50 | public static final int INFO = 3; |
Richard S. Hall | 8c011c6 | 2007-04-17 14:31:35 +0000 | [diff] [blame] | 51 | |
Richard S. Hall | 6d2ee1a | 2007-03-09 16:50:30 +0000 | [diff] [blame] | 52 | /** |
Richard S. Hall | 8c011c6 | 2007-04-17 14:31:35 +0000 | [diff] [blame] | 53 | * Log Level DEBUG. |
Richard S. Hall | 6d2ee1a | 2007-03-09 16:50:30 +0000 | [diff] [blame] | 54 | */ |
Richard S. Hall | f5931c1 | 2006-12-15 21:56:11 +0000 | [diff] [blame] | 55 | public static final int DEBUG = 4; |
Richard S. Hall | 8c011c6 | 2007-04-17 14:31:35 +0000 | [diff] [blame] | 56 | |
Richard S. Hall | 6d2ee1a | 2007-03-09 16:50:30 +0000 | [diff] [blame] | 57 | /** |
| 58 | * Bundle Context. |
| 59 | */ |
Richard S. Hall | f5931c1 | 2006-12-15 21:56:11 +0000 | [diff] [blame] | 60 | private BundleContext m_context; |
Richard S. Hall | 8c011c6 | 2007-04-17 14:31:35 +0000 | [diff] [blame] | 61 | |
Richard S. Hall | 6d2ee1a | 2007-03-09 16:50:30 +0000 | [diff] [blame] | 62 | /** |
Richard S. Hall | 8c011c6 | 2007-04-17 14:31:35 +0000 | [diff] [blame] | 63 | * Name of the logger. |
Richard S. Hall | 6d2ee1a | 2007-03-09 16:50:30 +0000 | [diff] [blame] | 64 | */ |
Richard S. Hall | f5931c1 | 2006-12-15 21:56:11 +0000 | [diff] [blame] | 65 | private String m_name; |
Richard S. Hall | 8c011c6 | 2007-04-17 14:31:35 +0000 | [diff] [blame] | 66 | |
Richard S. Hall | 6d2ee1a | 2007-03-09 16:50:30 +0000 | [diff] [blame] | 67 | /** |
Richard S. Hall | 8c011c6 | 2007-04-17 14:31:35 +0000 | [diff] [blame] | 68 | * trace level of this logger. |
Richard S. Hall | 6d2ee1a | 2007-03-09 16:50:30 +0000 | [diff] [blame] | 69 | */ |
Richard S. Hall | f5931c1 | 2006-12-15 21:56:11 +0000 | [diff] [blame] | 70 | private int m_level; |
Richard S. Hall | 8c011c6 | 2007-04-17 14:31:35 +0000 | [diff] [blame] | 71 | |
Richard S. Hall | 6d2ee1a | 2007-03-09 16:50:30 +0000 | [diff] [blame] | 72 | /** |
| 73 | * Constructor. |
Richard S. Hall | 8c011c6 | 2007-04-17 14:31:35 +0000 | [diff] [blame] | 74 | * |
Clement Escoffier | ce0e1e5 | 2008-03-28 15:33:36 +0000 | [diff] [blame^] | 75 | * @param context : bundle context |
Richard S. Hall | 6d2ee1a | 2007-03-09 16:50:30 +0000 | [diff] [blame] | 76 | * @param name : name of the logger |
| 77 | * @param level : trace level |
| 78 | */ |
Clement Escoffier | ce0e1e5 | 2008-03-28 15:33:36 +0000 | [diff] [blame^] | 79 | public Logger(BundleContext context, String name, int level) { |
Richard S. Hall | 8c011c6 | 2007-04-17 14:31:35 +0000 | [diff] [blame] | 80 | m_name = name; |
| 81 | m_level = level; |
Clement Escoffier | ce0e1e5 | 2008-03-28 15:33:36 +0000 | [diff] [blame^] | 82 | m_context = context; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Constructor. |
| 87 | * |
| 88 | * @param context : bundle context |
| 89 | * @param name : name of the logger |
| 90 | */ |
| 91 | public Logger(BundleContext context, String name) { |
| 92 | this(context, name, getDefaultLevel(context)); |
Richard S. Hall | f5931c1 | 2006-12-15 21:56:11 +0000 | [diff] [blame] | 93 | } |
Richard S. Hall | 8c011c6 | 2007-04-17 14:31:35 +0000 | [diff] [blame] | 94 | |
Richard S. Hall | 6d2ee1a | 2007-03-09 16:50:30 +0000 | [diff] [blame] | 95 | /** |
| 96 | * Log a message. |
Richard S. Hall | 8c011c6 | 2007-04-17 14:31:35 +0000 | [diff] [blame] | 97 | * |
Richard S. Hall | 6d2ee1a | 2007-03-09 16:50:30 +0000 | [diff] [blame] | 98 | * @param level : level of the message |
| 99 | * @param msg : the message to log |
| 100 | */ |
Richard S. Hall | f5931c1 | 2006-12-15 21:56:11 +0000 | [diff] [blame] | 101 | public void log(int level, String msg) { |
Richard S. Hall | 8c011c6 | 2007-04-17 14:31:35 +0000 | [diff] [blame] | 102 | if (m_level >= level) { |
Clement Escoffier | ce0e1e5 | 2008-03-28 15:33:36 +0000 | [diff] [blame^] | 103 | dispatch(level, msg); |
Richard S. Hall | 8c011c6 | 2007-04-17 14:31:35 +0000 | [diff] [blame] | 104 | } |
Richard S. Hall | f5931c1 | 2006-12-15 21:56:11 +0000 | [diff] [blame] | 105 | } |
Richard S. Hall | 8c011c6 | 2007-04-17 14:31:35 +0000 | [diff] [blame] | 106 | |
Richard S. Hall | 6d2ee1a | 2007-03-09 16:50:30 +0000 | [diff] [blame] | 107 | /** |
| 108 | * Log a message with an exception. |
Richard S. Hall | 8c011c6 | 2007-04-17 14:31:35 +0000 | [diff] [blame] | 109 | * |
Richard S. Hall | 6d2ee1a | 2007-03-09 16:50:30 +0000 | [diff] [blame] | 110 | * @param level : level of the message |
| 111 | * @param msg : message to log |
Clement Escoffier | ce0e1e5 | 2008-03-28 15:33:36 +0000 | [diff] [blame^] | 112 | * @param exception : exception attached to the message |
Richard S. Hall | 6d2ee1a | 2007-03-09 16:50:30 +0000 | [diff] [blame] | 113 | */ |
Clement Escoffier | ce0e1e5 | 2008-03-28 15:33:36 +0000 | [diff] [blame^] | 114 | public void log(int level, String msg, Throwable exception) { |
Richard S. Hall | 8c011c6 | 2007-04-17 14:31:35 +0000 | [diff] [blame] | 115 | if (m_level >= level) { |
Clement Escoffier | ce0e1e5 | 2008-03-28 15:33:36 +0000 | [diff] [blame^] | 116 | dispatch(level, msg, exception); |
Richard S. Hall | 8c011c6 | 2007-04-17 14:31:35 +0000 | [diff] [blame] | 117 | } |
Richard S. Hall | f5931c1 | 2006-12-15 21:56:11 +0000 | [diff] [blame] | 118 | } |
Clement Escoffier | ce0e1e5 | 2008-03-28 15:33:36 +0000 | [diff] [blame^] | 119 | |
Richard S. Hall | 6d2ee1a | 2007-03-09 16:50:30 +0000 | [diff] [blame] | 120 | /** |
| 121 | * Internal log method. |
Richard S. Hall | 8c011c6 | 2007-04-17 14:31:35 +0000 | [diff] [blame] | 122 | * |
Richard S. Hall | 6d2ee1a | 2007-03-09 16:50:30 +0000 | [diff] [blame] | 123 | * @param level : level of the message. |
| 124 | * @param msg : message to log |
Richard S. Hall | 6d2ee1a | 2007-03-09 16:50:30 +0000 | [diff] [blame] | 125 | */ |
Clement Escoffier | ce0e1e5 | 2008-03-28 15:33:36 +0000 | [diff] [blame^] | 126 | private void dispatch(int level, String msg) { |
Clement Escoffier | 2e35112 | 2007-07-02 11:36:35 +0000 | [diff] [blame] | 127 | |
| 128 | ServiceReference ref = m_context.getServiceReference(LogService.class.getName()); |
| 129 | LogService log = null; |
| 130 | if (ref != null) { |
| 131 | log = (LogService) m_context.getService(ref); |
| 132 | } |
| 133 | |
| 134 | String message = null; |
Richard S. Hall | 6d2ee1a | 2007-03-09 16:50:30 +0000 | [diff] [blame] | 135 | switch (level) { |
Richard S. Hall | f5931c1 | 2006-12-15 21:56:11 +0000 | [diff] [blame] | 136 | case DEBUG: |
Clement Escoffier | ce0e1e5 | 2008-03-28 15:33:36 +0000 | [diff] [blame^] | 137 | message = "[" + m_name + "] DEBUG: " + msg; |
Clement Escoffier | 2e35112 | 2007-07-02 11:36:35 +0000 | [diff] [blame] | 138 | if (log != null) { |
| 139 | log.log(LogService.LOG_DEBUG, message); |
Richard S. Hall | 8c011c6 | 2007-04-17 14:31:35 +0000 | [diff] [blame] | 140 | } |
Clement Escoffier | ce0e1e5 | 2008-03-28 15:33:36 +0000 | [diff] [blame^] | 141 | System.err.println(message); // NOPMD |
Richard S. Hall | f5931c1 | 2006-12-15 21:56:11 +0000 | [diff] [blame] | 142 | break; |
| 143 | case ERROR: |
Clement Escoffier | ce0e1e5 | 2008-03-28 15:33:36 +0000 | [diff] [blame^] | 144 | message = "[" + m_name + "] ERROR: " + msg; |
Clement Escoffier | 2e35112 | 2007-07-02 11:36:35 +0000 | [diff] [blame] | 145 | if (log != null) { |
| 146 | log.log(LogService.LOG_ERROR, message); |
Richard S. Hall | 8c011c6 | 2007-04-17 14:31:35 +0000 | [diff] [blame] | 147 | } |
Clement Escoffier | ce0e1e5 | 2008-03-28 15:33:36 +0000 | [diff] [blame^] | 148 | System.err.println(message); //NOPMD |
Richard S. Hall | f5931c1 | 2006-12-15 21:56:11 +0000 | [diff] [blame] | 149 | break; |
| 150 | case INFO: |
Clement Escoffier | ce0e1e5 | 2008-03-28 15:33:36 +0000 | [diff] [blame^] | 151 | message = "[" + m_name + "] INFO: " + msg; |
Clement Escoffier | 2e35112 | 2007-07-02 11:36:35 +0000 | [diff] [blame] | 152 | if (log != null) { |
| 153 | log.log(LogService.LOG_INFO, message); |
Richard S. Hall | 8c011c6 | 2007-04-17 14:31:35 +0000 | [diff] [blame] | 154 | } |
Clement Escoffier | ce0e1e5 | 2008-03-28 15:33:36 +0000 | [diff] [blame^] | 155 | System.err.println(message); // NOPMD |
Richard S. Hall | f5931c1 | 2006-12-15 21:56:11 +0000 | [diff] [blame] | 156 | break; |
| 157 | case WARNING: |
Clement Escoffier | ce0e1e5 | 2008-03-28 15:33:36 +0000 | [diff] [blame^] | 158 | message = "[" + m_name + "] WARNING: " + msg; |
Clement Escoffier | 2e35112 | 2007-07-02 11:36:35 +0000 | [diff] [blame] | 159 | if (log != null) { |
| 160 | log.log(LogService.LOG_WARNING, message); |
Richard S. Hall | 8c011c6 | 2007-04-17 14:31:35 +0000 | [diff] [blame] | 161 | } |
Clement Escoffier | ce0e1e5 | 2008-03-28 15:33:36 +0000 | [diff] [blame^] | 162 | System.err.println(message); // NOPMD |
Richard S. Hall | f5931c1 | 2006-12-15 21:56:11 +0000 | [diff] [blame] | 163 | break; |
| 164 | default: |
Clement Escoffier | ce0e1e5 | 2008-03-28 15:33:36 +0000 | [diff] [blame^] | 165 | System.err.println("[" + m_name + "] UNKNOWN[" + level + "]: " + msg); // NOPMD |
Richard S. Hall | 8c011c6 | 2007-04-17 14:31:35 +0000 | [diff] [blame] | 166 | break; |
Richard S. Hall | f5931c1 | 2006-12-15 21:56:11 +0000 | [diff] [blame] | 167 | } |
Clement Escoffier | 2e35112 | 2007-07-02 11:36:35 +0000 | [diff] [blame] | 168 | |
| 169 | if (log != null) { |
| 170 | m_context.ungetService(ref); |
Richard S. Hall | 40223f1 | 2007-05-03 14:24:40 +0000 | [diff] [blame] | 171 | } |
Richard S. Hall | 8c011c6 | 2007-04-17 14:31:35 +0000 | [diff] [blame] | 172 | } |
Clement Escoffier | ce0e1e5 | 2008-03-28 15:33:36 +0000 | [diff] [blame^] | 173 | |
| 174 | /** |
| 175 | * Internal log method. |
| 176 | * |
| 177 | * @param level : level of the message. |
| 178 | * @param msg : message to log |
| 179 | * @param exception : exception attached to the message |
| 180 | */ |
| 181 | private void dispatch(int level, String msg, Throwable exception) { |
| 182 | |
| 183 | ServiceReference ref = m_context.getServiceReference(LogService.class.getName()); |
| 184 | LogService log = null; |
| 185 | if (ref != null) { |
| 186 | log = (LogService) m_context.getService(ref); |
| 187 | } |
| 188 | |
| 189 | String message = null; |
| 190 | switch (level) { |
| 191 | case DEBUG: |
| 192 | message = "[" + m_name + "] DEBUG: " + msg; |
| 193 | if (log != null) { |
| 194 | log.log(LogService.LOG_DEBUG, message, exception); |
| 195 | } |
| 196 | System.err.println(message); // NOPMD |
| 197 | exception.printStackTrace(); // NOPMD |
| 198 | break; |
| 199 | case ERROR: |
| 200 | message = "[" + m_name + "] ERROR: " + msg; |
| 201 | if (log != null) { |
| 202 | log.log(LogService.LOG_ERROR, message, exception); |
| 203 | } |
| 204 | System.err.println(message); // NOPMD |
| 205 | exception.printStackTrace(System.err); // NOPMD |
| 206 | break; |
| 207 | case INFO: |
| 208 | message = "[" + m_name + "] INFO: " + msg; |
| 209 | if (log != null) { |
| 210 | log.log(LogService.LOG_INFO, message, exception); |
| 211 | } |
| 212 | System.err.println(message); // NOPMD |
| 213 | exception.printStackTrace(System.err); // NOPMD |
| 214 | break; |
| 215 | case WARNING: |
| 216 | message = "[" + m_name + "] WARNING: " + msg; |
| 217 | if (log != null) { |
| 218 | log.log(LogService.LOG_WARNING, message, exception); |
| 219 | } |
| 220 | System.err.println(message); // NOPMD |
| 221 | exception.printStackTrace(System.err); // NOPMD |
| 222 | break; |
| 223 | default: |
| 224 | System.err.println("[" + m_name + "] UNKNOWN[" + level + "]: " + msg); // NOPMD |
| 225 | exception.printStackTrace(); // NOPMD |
| 226 | break; |
| 227 | } |
| 228 | |
| 229 | if (log != null) { |
| 230 | m_context.ungetService(ref); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Get the default logger level. |
| 236 | * The property is searched inside the framework properties, the system properties, |
| 237 | * and in the manifest from the given bundle context. By default, set the level to WARNING. |
| 238 | * @param context : bundle context. |
| 239 | * @return the default log level. |
| 240 | */ |
| 241 | private static int getDefaultLevel(BundleContext context) { |
| 242 | // First check in the framework and in the system properties |
| 243 | String level = context.getProperty(IPOJO_LOG_LEVEL); |
| 244 | |
| 245 | // If null, look in bundle manifest |
| 246 | if (level == null) { |
| 247 | level = (String) context.getBundle().getHeaders().get(IPOJO_LOG_LEVEL); |
| 248 | } |
| 249 | |
| 250 | if (level != null) { |
| 251 | if (level.equalsIgnoreCase("info")) { |
| 252 | return INFO; |
| 253 | } else if (level.equalsIgnoreCase("debug")) { |
| 254 | return DEBUG; |
| 255 | } else if (level.equalsIgnoreCase("warning")) { |
| 256 | return WARNING; |
| 257 | } else if (level.equalsIgnoreCase("error")) { |
| 258 | return ERROR; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | // Either l is null, either the specified log level was unknown |
| 263 | // Set the default to WARNING |
| 264 | return WARNING; |
| 265 | |
| 266 | } |
Richard S. Hall | f5931c1 | 2006-12-15 21:56:11 +0000 | [diff] [blame] | 267 | } |