2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shale.remoting.logger;
19  
20  import java.util.HashMap;
21  import java.util.Map;
22  import java.util.logging.Level;
23  import java.util.logging.LogManager;
24  import java.util.logging.Logger;
25  
26  /***
27   * <p>Implementation of {@link Logger} that interacts with the standard
28   * <code>java.util.logging</code> APIs.</p>
29   */
30  public final class JdkLogger extends AbstractLogger {
31      
32  
33      // ------------------------------------------------------ Condition Checking
34  
35  
36      /*** {@inheritDoc} */
37      public boolean isTraceEnabled(String name) {
38          return logger(name).isLoggable(Level.FINEST);
39      }
40  
41  
42      /*** {@inheritDoc} */
43      public boolean isDebugEnabled(String name) {
44          return logger(name).isLoggable(Level.FINE);
45      }
46  
47  
48      /*** {@inheritDoc} */
49      public boolean isInfoEnabled(String name) {
50          return logger(name).isLoggable(Level.INFO);
51      }
52  
53  
54      /*** {@inheritDoc} */
55      public boolean isWarnEnabled(String name) {
56          return logger(name).isLoggable(Level.WARNING);
57      }
58  
59  
60      /*** {@inheritDoc} */
61      public boolean isErrorEnabled(String name) {
62          return logger(name).isLoggable(Level.SEVERE);
63      }
64  
65  
66      /*** {@inheritDoc} */
67      public boolean isFatalEnabled(String name) {
68          return logger(name).isLoggable(Level.SEVERE);
69      }
70  
71  
72      // ------------------------------------------------------------- Log Methods
73  
74  
75      /*** {@inheritDoc} */
76      public void trace(String name, String message,
77                        Throwable exception, Object[] params) {
78          if (exception == null) {
79              logger(name).log(Level.FINEST, message(message, params));
80          } else {
81              logger(name).log(Level.FINEST, message(message, params), exception);
82          }
83      }
84  
85  
86      /*** {@inheritDoc} */
87      public void debug(String name, String message,
88                        Throwable exception, Object[] params) {
89          if (exception == null) {
90              logger(name).log(Level.FINE, message(message, params));
91          } else {
92              logger(name).log(Level.FINE, message(message, params), exception);
93          }
94      }
95  
96  
97      /*** {@inheritDoc} */
98      public void info(String name, String message,
99                       Throwable exception, Object[] params) {
100         if (exception == null) {
101             logger(name).log(Level.INFO, message(message, params));
102         } else {
103             logger(name).log(Level.INFO, message(message, params), exception);
104         }
105     }
106 
107 
108     /*** {@inheritDoc} */
109     public void warn(String name, String message,
110                      Throwable exception, Object[] params) {
111         if (exception == null) {
112             logger(name).log(Level.WARNING, message(message, params));
113         } else {
114             logger(name).log(Level.WARNING, message(message, params), exception);
115         }
116     }
117 
118 
119     /*** {@inheritDoc} */
120     public void error(String name, String message,
121                       Throwable exception, Object[] params) {
122         if (exception == null) {
123             logger(name).log(Level.SEVERE, message(message, params));
124         } else {
125             logger(name).log(Level.SEVERE, message(message, params), exception);
126         }
127     }
128 
129 
130     /*** {@inheritDoc} */
131     public void fatal(String name, String message,
132                       Throwable exception, Object[] params) {
133         if (exception == null) {
134             logger(name).log(Level.SEVERE, message(message, params));
135         } else {
136             logger(name).log(Level.SEVERE, message(message, params), exception);
137         }
138     }
139 
140 
141     // --------------------------------------------------------- Private Methods
142 
143 
144     /***
145      * <p>Map of <code>Logger</code> instances, keyed by name.</p>
146      */
147     private Map loggers = new HashMap();
148 
149 
150     /***
151      * <p>Return a <code>Logger</code> instance for the specified
152      * logger name, creating a new one if necessary.</p>
153      *
154      * @param name Name of the requested logger
155      */
156     private synchronized Logger logger(String name) {
157 
158         Logger logger = (Logger) loggers.get(name);
159         if (logger == null) {
160             logger = LogManager.getLogManager().getLogger(name);
161             loggers.put(name, logger);
162         }
163         return logger;
164 
165     }
166 
167 
168 }