blob: c8b8c2690e60652e2c908aa9c4544f42c54e15de [file] [log] [blame]
Richard S. Hallf5931c12006-12-15 21:56:11 +00001/*
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 */
19package org.apache.felix.ipojo.util;
20
21import org.osgi.framework.BundleContext;
Richard S. Hallf5931c12006-12-15 21:56:11 +000022import org.osgi.framework.ServiceReference;
23import org.osgi.service.log.LogService;
24
Richard S. Hall6d2ee1a2007-03-09 16:50:30 +000025/**
Richard S. Hall8c011c62007-04-17 14:31:35 +000026 * iPOJO Logger. This logger send log message to a log service if presents.
27 *
Karl Paulsd312acc2007-06-18 20:38:33 +000028 * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
Richard S. Hall6d2ee1a2007-03-09 16:50:30 +000029 */
Clement Escoffier2e351122007-07-02 11:36:35 +000030public class Logger {
Clement Escoffierce0e1e52008-03-28 15:33:36 +000031
32 /**
33 * Ipojo default log level property.
34 */
35 public static final String IPOJO_LOG_LEVEL = "ipojo.log.level";
Richard S. Hall8c011c62007-04-17 14:31:35 +000036
Richard S. Hall8c011c62007-04-17 14:31:35 +000037 /**
38 * Log Level ERROR.
39 */
40 public static final int ERROR = 1;
41
42 /**
43 * Log Level WARNING.
Richard S. Hall6d2ee1a2007-03-09 16:50:30 +000044 */
Richard S. Hallf5931c12006-12-15 21:56:11 +000045 public static final int WARNING = 2;
Richard S. Hall8c011c62007-04-17 14:31:35 +000046
Richard S. Hall6d2ee1a2007-03-09 16:50:30 +000047 /**
48 * Log Level INFO.
49 */
Richard S. Hallf5931c12006-12-15 21:56:11 +000050 public static final int INFO = 3;
Richard S. Hall8c011c62007-04-17 14:31:35 +000051
Richard S. Hall6d2ee1a2007-03-09 16:50:30 +000052 /**
Richard S. Hall8c011c62007-04-17 14:31:35 +000053 * Log Level DEBUG.
Richard S. Hall6d2ee1a2007-03-09 16:50:30 +000054 */
Richard S. Hallf5931c12006-12-15 21:56:11 +000055 public static final int DEBUG = 4;
Richard S. Hall8c011c62007-04-17 14:31:35 +000056
Richard S. Hall6d2ee1a2007-03-09 16:50:30 +000057 /**
58 * Bundle Context.
59 */
Richard S. Hallf5931c12006-12-15 21:56:11 +000060 private BundleContext m_context;
Richard S. Hall8c011c62007-04-17 14:31:35 +000061
Richard S. Hall6d2ee1a2007-03-09 16:50:30 +000062 /**
Richard S. Hall8c011c62007-04-17 14:31:35 +000063 * Name of the logger.
Richard S. Hall6d2ee1a2007-03-09 16:50:30 +000064 */
Richard S. Hallf5931c12006-12-15 21:56:11 +000065 private String m_name;
Richard S. Hall8c011c62007-04-17 14:31:35 +000066
Richard S. Hall6d2ee1a2007-03-09 16:50:30 +000067 /**
Richard S. Hall8c011c62007-04-17 14:31:35 +000068 * trace level of this logger.
Richard S. Hall6d2ee1a2007-03-09 16:50:30 +000069 */
Richard S. Hallf5931c12006-12-15 21:56:11 +000070 private int m_level;
Richard S. Hall8c011c62007-04-17 14:31:35 +000071
Richard S. Hall6d2ee1a2007-03-09 16:50:30 +000072 /**
73 * Constructor.
Richard S. Hall8c011c62007-04-17 14:31:35 +000074 *
Clement Escoffierce0e1e52008-03-28 15:33:36 +000075 * @param context : bundle context
Richard S. Hall6d2ee1a2007-03-09 16:50:30 +000076 * @param name : name of the logger
77 * @param level : trace level
78 */
Clement Escoffierce0e1e52008-03-28 15:33:36 +000079 public Logger(BundleContext context, String name, int level) {
Richard S. Hall8c011c62007-04-17 14:31:35 +000080 m_name = name;
81 m_level = level;
Clement Escoffierce0e1e52008-03-28 15:33:36 +000082 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. Hallf5931c12006-12-15 21:56:11 +000093 }
Richard S. Hall8c011c62007-04-17 14:31:35 +000094
Richard S. Hall6d2ee1a2007-03-09 16:50:30 +000095 /**
96 * Log a message.
Richard S. Hall8c011c62007-04-17 14:31:35 +000097 *
Richard S. Hall6d2ee1a2007-03-09 16:50:30 +000098 * @param level : level of the message
99 * @param msg : the message to log
100 */
Richard S. Hallf5931c12006-12-15 21:56:11 +0000101 public void log(int level, String msg) {
Richard S. Hall8c011c62007-04-17 14:31:35 +0000102 if (m_level >= level) {
Clement Escoffierce0e1e52008-03-28 15:33:36 +0000103 dispatch(level, msg);
Richard S. Hall8c011c62007-04-17 14:31:35 +0000104 }
Richard S. Hallf5931c12006-12-15 21:56:11 +0000105 }
Richard S. Hall8c011c62007-04-17 14:31:35 +0000106
Richard S. Hall6d2ee1a2007-03-09 16:50:30 +0000107 /**
108 * Log a message with an exception.
Richard S. Hall8c011c62007-04-17 14:31:35 +0000109 *
Richard S. Hall6d2ee1a2007-03-09 16:50:30 +0000110 * @param level : level of the message
111 * @param msg : message to log
Clement Escoffierce0e1e52008-03-28 15:33:36 +0000112 * @param exception : exception attached to the message
Richard S. Hall6d2ee1a2007-03-09 16:50:30 +0000113 */
Clement Escoffierce0e1e52008-03-28 15:33:36 +0000114 public void log(int level, String msg, Throwable exception) {
Richard S. Hall8c011c62007-04-17 14:31:35 +0000115 if (m_level >= level) {
Clement Escoffierce0e1e52008-03-28 15:33:36 +0000116 dispatch(level, msg, exception);
Richard S. Hall8c011c62007-04-17 14:31:35 +0000117 }
Richard S. Hallf5931c12006-12-15 21:56:11 +0000118 }
Clement Escoffierce0e1e52008-03-28 15:33:36 +0000119
Richard S. Hall6d2ee1a2007-03-09 16:50:30 +0000120 /**
121 * Internal log method.
Richard S. Hall8c011c62007-04-17 14:31:35 +0000122 *
Richard S. Hall6d2ee1a2007-03-09 16:50:30 +0000123 * @param level : level of the message.
124 * @param msg : message to log
Richard S. Hall6d2ee1a2007-03-09 16:50:30 +0000125 */
Clement Escoffierce0e1e52008-03-28 15:33:36 +0000126 private void dispatch(int level, String msg) {
Clement Escoffier2e351122007-07-02 11:36:35 +0000127
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. Hall6d2ee1a2007-03-09 16:50:30 +0000135 switch (level) {
Richard S. Hallf5931c12006-12-15 21:56:11 +0000136 case DEBUG:
Clement Escoffierce0e1e52008-03-28 15:33:36 +0000137 message = "[" + m_name + "] DEBUG: " + msg;
Clement Escoffier2e351122007-07-02 11:36:35 +0000138 if (log != null) {
139 log.log(LogService.LOG_DEBUG, message);
Richard S. Hall8c011c62007-04-17 14:31:35 +0000140 }
Clement Escoffierce0e1e52008-03-28 15:33:36 +0000141 System.err.println(message); // NOPMD
Richard S. Hallf5931c12006-12-15 21:56:11 +0000142 break;
143 case ERROR:
Clement Escoffierce0e1e52008-03-28 15:33:36 +0000144 message = "[" + m_name + "] ERROR: " + msg;
Clement Escoffier2e351122007-07-02 11:36:35 +0000145 if (log != null) {
146 log.log(LogService.LOG_ERROR, message);
Richard S. Hall8c011c62007-04-17 14:31:35 +0000147 }
Clement Escoffierce0e1e52008-03-28 15:33:36 +0000148 System.err.println(message); //NOPMD
Richard S. Hallf5931c12006-12-15 21:56:11 +0000149 break;
150 case INFO:
Clement Escoffierce0e1e52008-03-28 15:33:36 +0000151 message = "[" + m_name + "] INFO: " + msg;
Clement Escoffier2e351122007-07-02 11:36:35 +0000152 if (log != null) {
153 log.log(LogService.LOG_INFO, message);
Richard S. Hall8c011c62007-04-17 14:31:35 +0000154 }
Clement Escoffierce0e1e52008-03-28 15:33:36 +0000155 System.err.println(message); // NOPMD
Richard S. Hallf5931c12006-12-15 21:56:11 +0000156 break;
157 case WARNING:
Clement Escoffierce0e1e52008-03-28 15:33:36 +0000158 message = "[" + m_name + "] WARNING: " + msg;
Clement Escoffier2e351122007-07-02 11:36:35 +0000159 if (log != null) {
160 log.log(LogService.LOG_WARNING, message);
Richard S. Hall8c011c62007-04-17 14:31:35 +0000161 }
Clement Escoffierce0e1e52008-03-28 15:33:36 +0000162 System.err.println(message); // NOPMD
Richard S. Hallf5931c12006-12-15 21:56:11 +0000163 break;
164 default:
Clement Escoffierce0e1e52008-03-28 15:33:36 +0000165 System.err.println("[" + m_name + "] UNKNOWN[" + level + "]: " + msg); // NOPMD
Richard S. Hall8c011c62007-04-17 14:31:35 +0000166 break;
Richard S. Hallf5931c12006-12-15 21:56:11 +0000167 }
Clement Escoffier2e351122007-07-02 11:36:35 +0000168
169 if (log != null) {
170 m_context.ungetService(ref);
Richard S. Hall40223f12007-05-03 14:24:40 +0000171 }
Richard S. Hall8c011c62007-04-17 14:31:35 +0000172 }
Clement Escoffierce0e1e52008-03-28 15:33:36 +0000173
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. Hallf5931c12006-12-15 21:56:11 +0000267}