View Javadoc

1   /*
2    * Copyright 2009 Red Hat, Inc.
3    *
4    * Red Hat licenses this file to you under the Apache License, version 2.0
5    * (the "License"); you may not use this file except in compliance with the
6    * 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.jboss.netty.util.internal.StackTraceSimplifier;
19  
20  /**
21   * Creates an {@link InternalLogger} or changes the default factory
22   * implementation.  This factory allows you to choose what logging framework
23   * Netty should use.  The default factory is {@link JdkLoggerFactory}.
24   * You can change it to your preferred logging framework before other Netty
25   * classes are loaded:
26   * <pre>
27   * {@link InternalLoggerFactory}.setDefaultFactory(new {@link Log4JLoggerFactory}());
28   * </pre>
29   * Please note that the new default factory is effective only for the classes
30   * which were loaded after the default factory is changed.  Therefore,
31   * {@link #setDefaultFactory(InternalLoggerFactory)} should be called as early
32   * as possible and shouldn't be called more than once.
33   *
34   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
35   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
36   *
37   * @version $Rev: 2122 $, $Date: 2010-02-02 11:00:04 +0900 (Tue, 02 Feb 2010) $
38   *
39   * @apiviz.landmark
40   * @apiviz.has org.jboss.netty.logging.InternalLogger oneway - - creates
41   */
42  public abstract class InternalLoggerFactory {
43      private static volatile InternalLoggerFactory defaultFactory = new JdkLoggerFactory();
44  
45      static {
46          // Load the dependent classes in advance to avoid the case where
47          // the VM fails to load the required classes because of too many open
48          // files.
49          StackTraceSimplifier.simplify(new Exception());
50      }
51  
52      /**
53       * Returns the default factory.  The initial default factory is
54       * {@link JdkLoggerFactory}.
55       */
56      public static InternalLoggerFactory getDefaultFactory() {
57          return defaultFactory;
58      }
59  
60      /**
61       * Changes the default factory.
62       */
63      public static void setDefaultFactory(InternalLoggerFactory defaultFactory) {
64          if (defaultFactory == null) {
65              throw new NullPointerException("defaultFactory");
66          }
67          InternalLoggerFactory.defaultFactory = defaultFactory;
68      }
69  
70      /**
71       * Creates a new logger instance with the name of the specified class.
72       */
73      public static InternalLogger getInstance(Class<?> clazz) {
74          return getInstance(clazz.getName());
75      }
76  
77      /**
78       * Creates a new logger instance with the specified name.
79       */
80      public static InternalLogger getInstance(String name) {
81          final InternalLogger logger = getDefaultFactory().newInstance(name);
82          return new InternalLogger() {
83  
84              public void debug(String msg) {
85                  logger.debug(msg);
86              }
87  
88              public void debug(String msg, Throwable cause) {
89                  StackTraceSimplifier.simplify(cause);
90                  logger.debug(msg, cause);
91              }
92  
93              public void error(String msg) {
94                  logger.error(msg);
95              }
96  
97              public void error(String msg, Throwable cause) {
98                  StackTraceSimplifier.simplify(cause);
99                  logger.error(msg, cause);
100             }
101 
102             public void info(String msg) {
103                 logger.info(msg);
104             }
105 
106             public void info(String msg, Throwable cause) {
107                 StackTraceSimplifier.simplify(cause);
108                 logger.info(msg, cause);
109             }
110 
111             public boolean isDebugEnabled() {
112                 return logger.isDebugEnabled();
113             }
114 
115             public boolean isErrorEnabled() {
116                 return logger.isErrorEnabled();
117             }
118 
119             public boolean isInfoEnabled() {
120                 return logger.isInfoEnabled();
121             }
122 
123             public boolean isWarnEnabled() {
124                 return logger.isWarnEnabled();
125             }
126 
127             public void warn(String msg) {
128                 logger.warn(msg);
129             }
130 
131             public void warn(String msg, Throwable cause) {
132                 StackTraceSimplifier.simplify(cause);
133                 logger.warn(msg, cause);
134             }
135 
136             public boolean isEnabled(InternalLogLevel level) {
137                 return logger.isEnabled(level);
138             }
139 
140             public void log(InternalLogLevel level, String msg) {
141                 logger.log(level, msg);
142             }
143 
144             public void log(InternalLogLevel level, String msg, Throwable cause) {
145                 StackTraceSimplifier.simplify(cause);
146                 logger.log(level, msg, cause);
147             }
148         };
149     }
150 
151     /**
152      * Creates a new logger instance with the specified name.
153      */
154     public abstract InternalLogger newInstance(String name);
155 }