1 /*
2 * Copyright (c) 2004-2005 QOS.ch
3 *
4 * All rights reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, and/or sell copies of the Software, and to permit persons
11 * to whom the Software is furnished to do so, provided that the above
12 * copyright notice(s) and this permission notice appear in all copies of
13 * the Software and that both the above copyright notice(s) and this
14 * permission notice appear in supporting documentation.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
19 * OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
20 * HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY
21 * SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER
22 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
23 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
24 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 *
26 * Except as contained in this notice, the name of a copyright holder
27 * shall not be used in advertising or otherwise to promote the sale, use
28 * or other dealings in this Software without prior written authorization
29 * of the copyright holder.
30 *
31 */
32
33 package org.slf4j.osgi.logservice.impl;
34
35 import org.osgi.framework.Bundle;
36 import org.osgi.framework.ServiceReference;
37 import org.osgi.framework.Version;
38 import org.osgi.service.log.LogService;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 /**
43 * <code>LogServiceImpl</code> is a simple OSGi LogService implementation that delegates to a slf4j
44 * Logger.
45 *
46 * @author John Conlon
47 * @author Matt Bishop
48 */
49 public class LogServiceImpl implements LogService {
50
51 private static final String UNKNOWN = "[Unknown]";
52
53 private final Logger delegate;
54
55 /**
56 * Creates a new instance of LogServiceImpl.
57 *
58 * @param bundle The bundle to create a new LogService for.
59 */
60 public LogServiceImpl(Bundle bundle) {
61
62 String name = bundle.getSymbolicName();
63 Version version = bundle.getVersion();
64 if (version == null) {
65 version = Version.emptyVersion;
66 }
67 delegate = LoggerFactory.getLogger(name + '.' + version);
68 }
69
70 /*
71 * (non-Javadoc)
72 *
73 * @see org.osgi.service.log.LogService#log(int, java.lang.String)
74 */
75 public void log(int level, String message) {
76
77 switch (level) {
78 case LOG_DEBUG:
79 delegate.debug(message);
80 break;
81 case LOG_ERROR:
82 delegate.error(message);
83 break;
84 case LOG_INFO:
85 delegate.info(message);
86 break;
87 case LOG_WARNING:
88 delegate.warn(message);
89 break;
90 default:
91 break;
92 }
93 }
94
95 /*
96 * (non-Javadoc)
97 *
98 * @see org.osgi.service.log.LogService#log(int, java.lang.String, java.lang.Throwable)
99 */
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 }