View Javadoc

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  	/**
57  	 * Creates a new instance of LogServiceImpl.
58  	 *
59  	 * @param bundle The bundle to create a new LogService for.
60  	 */
61  	public LogServiceImpl(Bundle bundle) {
62  
63  		String name = bundle.getSymbolicName();
64  		Version version = bundle.getVersion();
65  		if (version == null) {
66  			version = Version.emptyVersion;
67  		}
68  		delegate = LoggerFactory.getLogger(name + '.' + version);
69  	}
70  
71  	/*
72  	 * (non-Javadoc)
73  	 *
74  	 * @see org.osgi.service.log.LogService#log(int, java.lang.String)
75  	 */
76  	public void log(int level, String message) {
77  
78  		switch (level) {
79  		case LOG_DEBUG:
80  			delegate.debug(message);
81  			break;
82  		case LOG_ERROR:
83  			delegate.error(message);
84  			break;
85  		case LOG_INFO:
86  			delegate.info(message);
87  			break;
88  		case LOG_WARNING:
89  			delegate.warn(message);
90  			break;
91  		default:
92  			break;
93  		}
94  	}
95  
96  	/*
97  	 * (non-Javadoc)
98  	 *
99  	 * @see org.osgi.service.log.LogService#log(int, java.lang.String,
100 	 *      java.lang.Throwable)
101 	 */
102 	public void log(int level, String message, Throwable exception) {
103 
104 		switch (level) {
105 		case LOG_DEBUG:
106 			delegate.debug(message, exception);
107 			break;
108 		case LOG_ERROR:
109 			delegate.error(message, exception);
110 			break;
111 		case LOG_INFO:
112 			delegate.info(message, exception);
113 			break;
114 		case LOG_WARNING:
115 			delegate.warn(message, exception);
116 			break;
117 		default:
118 			break;
119 		}
120 	}
121 
122 	/*
123 	 * (non-Javadoc)
124 	 *
125 	 * @see org.osgi.service.log.LogService#log(org.osgi.framework.ServiceReference,
126 	 *      int, java.lang.String)
127 	 */
128 	public void log(ServiceReference sr, int level, String message) {
129 
130 		switch (level) {
131 		case LOG_DEBUG:
132 			if(delegate.isDebugEnabled()){
133 				delegate.debug(createMessage(sr, message));
134 			}
135 			break;
136 		case LOG_ERROR:
137 			if(delegate.isErrorEnabled()){
138 				delegate.error(createMessage(sr, message));
139 			}
140 			break;
141 		case LOG_INFO:
142 			if(delegate.isInfoEnabled()){
143 				delegate.info(createMessage(sr, message));
144 			}
145 			break;
146 		case LOG_WARNING:
147 			if(delegate.isWarnEnabled()){
148 				delegate.warn(createMessage(sr, message));
149 			}
150 			break;
151 		default:
152 			break;
153 		}
154 	}
155 
156 	/**
157 	 * Formats the log message to indicate the service sending it, if known.
158 	 *
159 	 * @param sr the ServiceReference sending the message.
160 	 * @param message The message to log.
161 	 * @return The formatted log message.
162 	 */
163 	private String createMessage(ServiceReference sr, String message) {
164 
165 		StringBuilder output = new StringBuilder();
166 		if (sr != null) {
167 			output.append('[').append(sr.toString()).append(']');
168 		} else {
169 			output.append(UNKNOWN);
170 		}
171 		output.append(message);
172 
173 		return output.toString();
174 	}
175 
176 	/*
177 	 * (non-Javadoc)
178 	 *
179 	 * @see org.osgi.service.log.LogService#log(org.osgi.framework.ServiceReference,
180 	 *      int, java.lang.String, java.lang.Throwable)
181 	 */
182 	public void log(ServiceReference sr, int level, String message, Throwable exception) {
183 
184 		switch (level) {
185 		case LOG_DEBUG:
186 			if(delegate.isDebugEnabled()){
187 				delegate.debug(createMessage(sr, message), exception);
188 			}
189 			break;
190 		case LOG_ERROR:
191 			if(delegate.isErrorEnabled()){
192 				delegate.error(createMessage(sr, message), exception);
193 			}
194 			break;
195 		case LOG_INFO:
196 			if(delegate.isInfoEnabled()){
197 				delegate.info(createMessage(sr, message), exception);
198 			}
199 			break;
200 		case LOG_WARNING:
201 			if(delegate.isWarnEnabled()){
202 				delegate.warn(createMessage(sr, message), exception);
203 			}
204 			break;
205 		default:
206 			break;
207 		}
208 	}
209 }