View Javadoc
1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. 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, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package org.jboss.netty.logging;
17  
18  import org.osgi.service.log.LogService;
19  
20  /**
21   * <a href="http://www.osgi.org/">OSGi</a> {@link LogService} logger.
22   */
23  class OsgiLogger extends AbstractInternalLogger {
24  
25      private final OsgiLoggerFactory parent;
26      private final InternalLogger fallback;
27      private final String name;
28      private final String prefix;
29  
30      OsgiLogger(OsgiLoggerFactory parent, String name, InternalLogger fallback) {
31          this.parent = parent;
32          this.name = name;
33          this.fallback = fallback;
34          prefix = '[' + name + "] ";
35      }
36  
37      public void debug(String msg) {
38          LogService logService = parent.getLogService();
39          if (logService != null) {
40              logService.log(LogService.LOG_DEBUG, prefix + msg);
41          } else {
42              fallback.debug(msg);
43          }
44      }
45  
46      public void debug(String msg, Throwable cause) {
47          LogService logService = parent.getLogService();
48          if (logService != null) {
49              logService.log(LogService.LOG_DEBUG, prefix + msg, cause);
50          } else {
51              fallback.debug(msg, cause);
52          }
53      }
54  
55      public void error(String msg) {
56          LogService logService = parent.getLogService();
57          if (logService != null) {
58              logService.log(LogService.LOG_ERROR, prefix + msg);
59          } else {
60              fallback.error(msg);
61          }
62      }
63  
64      public void error(String msg, Throwable cause) {
65          LogService logService = parent.getLogService();
66          if (logService != null) {
67              logService.log(LogService.LOG_ERROR, prefix + msg, cause);
68          } else {
69              fallback.error(msg, cause);
70          }
71      }
72  
73      public void info(String msg) {
74          LogService logService = parent.getLogService();
75          if (logService != null) {
76              logService.log(LogService.LOG_INFO, prefix + msg);
77          } else {
78              fallback.info(msg);
79          }
80      }
81  
82      public void info(String msg, Throwable cause) {
83          LogService logService = parent.getLogService();
84          if (logService != null) {
85              logService.log(LogService.LOG_INFO, prefix + msg, cause);
86          } else {
87              fallback.info(msg, cause);
88          }
89      }
90  
91      public boolean isDebugEnabled() {
92          return true;
93      }
94  
95      public boolean isErrorEnabled() {
96          return true;
97      }
98  
99      public boolean isInfoEnabled() {
100         return true;
101     }
102 
103     public boolean isWarnEnabled() {
104         return true;
105     }
106 
107     public void warn(String msg) {
108         LogService logService = parent.getLogService();
109         if (logService != null) {
110             logService.log(LogService.LOG_WARNING, prefix + msg);
111         } else {
112             fallback.warn(msg);
113         }
114     }
115 
116     public void warn(String msg, Throwable cause) {
117         LogService logService = parent.getLogService();
118         if (logService != null) {
119             logService.log(LogService.LOG_WARNING, prefix + msg, cause);
120         } else {
121             fallback.warn(msg, cause);
122         }
123     }
124 
125     @Override
126     public String toString() {
127         return name;
128     }
129 }