View Javadoc

1   /*******************************************************************************
2    * Copyright (c) 2007, 2014 Massimiliano Ziccardi
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *******************************************************************************/
16  package org.slf4j.impl;
17  
18  import org.osgi.framework.ServiceReference;
19  import org.osgi.service.log.LogService;
20  import org.slf4j.helpers.MarkerIgnoringBase;
21  import org.slf4j.helpers.MessageFormatter;
22  
23  public class OSGiLogger extends MarkerIgnoringBase
24  {
25      /**
26       * 
27       */
28      private static final long serialVersionUID = 1L;
29  
30      /**
31       * Check the availability of the OSGI logging service, and use it is available.
32       * Does nothing otherwise.
33       * @param level
34       * @param message
35       * @param t
36       */
37      final private void internalLog(final int level, final Object message, final Throwable t)
38      {
39          LogService logservice = OSGILogFactory.getLogService();
40          ServiceReference serviceref = OSGILogFactory.getServiceReference();
41  
42          if (logservice != null)
43          {
44              try {
45                  if (t != null)
46                      logservice.log(serviceref, level, message.toString(), t);
47                  else
48                      logservice.log(serviceref, level, message.toString());
49              } catch (Exception exc)
50              {
51                  // Service may have become invalid, just ignore any error
52                  // until the log service reference is updated by the
53                  // log factory.
54              }
55          }
56      }
57  
58      public boolean isTraceEnabled() {
59          return true;
60      }
61      public void trace(final String msg) {
62          internalLog(LogService.LOG_DEBUG, msg, null);
63      }
64  
65      public void trace(final String format, final Object arg) {
66          String msgStr = MessageFormatter.format(format, arg).getMessage();
67          internalLog(LogService.LOG_DEBUG, msgStr, null);
68      }
69  
70      public void trace(final String format, final Object arg1, final Object arg2) {
71          String msgStr = MessageFormatter.format(format, arg1, arg2).getMessage();
72          internalLog(LogService.LOG_DEBUG, msgStr, null);
73      }
74  
75  
76      public void trace(final String format, final Object ... argArray) {
77          String msgStr = MessageFormatter.arrayFormat(format, argArray).getMessage();
78          internalLog(LogService.LOG_DEBUG, msgStr, null);
79      }
80  
81      public void trace(final String msg, final Throwable t) {
82          internalLog(LogService.LOG_DEBUG, msg, t);
83      }
84  
85  
86  
87      public boolean isDebugEnabled() {
88          return true;
89      }
90      public void debug(final String msg) {
91          internalLog(LogService.LOG_DEBUG, msg, null);
92      }
93      public void debug(final String format, final Object arg) {
94          String msgStr = MessageFormatter.format(format, arg).getMessage();
95          internalLog(LogService.LOG_DEBUG, msgStr, null);
96      }
97  
98      public void debug(final String format, final Object arg1, final Object arg2) {
99          String msgStr = MessageFormatter.format(format, arg1, arg2).getMessage();
100         internalLog(LogService.LOG_DEBUG, msgStr, null);
101     }
102 
103     public void debug(final String format, final Object ... argArray) {
104         String msgStr = MessageFormatter.arrayFormat(format, argArray).getMessage();
105         internalLog(LogService.LOG_DEBUG, msgStr, null);
106     }
107 
108     public void debug(final String msg, final Throwable t) {
109         internalLog(LogService.LOG_DEBUG, msg, t);
110     }
111 
112     public boolean isInfoEnabled() {
113         return true;
114     }
115 
116     public void info(final String msg) {
117         internalLog(LogService.LOG_INFO, msg, null);
118     }
119 
120     public void info(final String format, final Object arg) {
121         String msgStr = MessageFormatter.format(format, arg).getMessage();
122         internalLog(LogService.LOG_INFO, msgStr, null);
123     }
124     public void info(final String format, final Object arg1, final Object arg2) {
125         String msgStr = MessageFormatter.format(format, arg1, arg2).getMessage();
126         internalLog(LogService.LOG_INFO, msgStr, null);
127     }
128 
129     public void info(final String format, final Object ... argArray) {
130         String msgStr = MessageFormatter.arrayFormat(format, argArray).getMessage();
131         internalLog(LogService.LOG_INFO, msgStr, null);
132     }
133 
134     public void info(final String msg, final Throwable t) {
135         internalLog(LogService.LOG_INFO, msg, t);
136     }
137 
138     public boolean isWarnEnabled() {
139         return true;
140     }
141     public void warn(final String msg) {
142         internalLog(LogService.LOG_WARNING, msg, null);
143     }
144 
145     public void warn(final String format, final Object arg) {
146         String msgStr = MessageFormatter.format(format, arg).getMessage();
147         internalLog(LogService.LOG_WARNING, msgStr, null);
148     }
149 
150     public void warn(final String format, final Object arg1, final Object arg2) {
151         String msgStr = MessageFormatter.format(format, arg1, arg2).getMessage();
152         internalLog(LogService.LOG_WARNING, msgStr, null);
153     }
154 
155     public void warn(final String format, final Object ... argArray) {
156         String msgStr = MessageFormatter.arrayFormat(format, argArray).getMessage();
157         internalLog(LogService.LOG_WARNING, msgStr, null);
158     }
159 
160     public void warn(final String msg, final Throwable t) {
161         internalLog(LogService.LOG_WARNING, msg, t);
162     }
163 
164     public boolean isErrorEnabled() {
165         return true;
166     }
167 
168     public void error(final String msg) {
169         internalLog(LogService.LOG_ERROR, msg, null);
170     }
171 
172     public void error(final String format, final Object arg) {
173         String msgStr = MessageFormatter.format(format, arg).getMessage();
174         internalLog(LogService.LOG_ERROR, msgStr, null);
175     }
176 
177     public void error(final String format, final Object arg1, final Object arg2) {
178         String msgStr = MessageFormatter.format(format, arg1, arg2).getMessage();
179         internalLog(LogService.LOG_ERROR, msgStr, null);
180     }
181     public void error(final String format, final Object ... argArray) {
182         String msgStr = MessageFormatter.arrayFormat(format, argArray).getMessage();
183         internalLog(LogService.LOG_ERROR, msgStr, null);
184     }
185 
186     public void error(final String msg, final Throwable t) {
187         internalLog(LogService.LOG_ERROR, msg, t);
188     }
189 
190 }