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.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   * @apiviz.landmark
35   * @apiviz.has org.jboss.netty.logging.InternalLogger oneway - - creates
36   */
37  public abstract class InternalLoggerFactory {
38      private static volatile InternalLoggerFactory defaultFactory = new JdkLoggerFactory();
39  
40      static {
41          // Load the dependent classes in advance to avoid the case where
42          // the VM fails to load the required classes because of too many open
43          // files.
44          StackTraceSimplifier.simplify(new Exception());
45      }
46  
47      /**
48       * Returns the default factory.  The initial default factory is
49       * {@link JdkLoggerFactory}.
50       */
51      public static InternalLoggerFactory getDefaultFactory() {
52          return defaultFactory;
53      }
54  
55      /**
56       * Changes the default factory.
57       */
58      public static void setDefaultFactory(InternalLoggerFactory defaultFactory) {
59          if (defaultFactory == null) {
60              throw new NullPointerException("defaultFactory");
61          }
62          InternalLoggerFactory.defaultFactory = defaultFactory;
63      }
64  
65      /**
66       * Creates a new logger instance with the name of the specified class.
67       */
68      public static InternalLogger getInstance(Class<?> clazz) {
69          return getInstance(clazz.getName());
70      }
71  
72      /**
73       * Creates a new logger instance with the specified name.
74       */
75      public static InternalLogger getInstance(String name) {
76          final InternalLogger logger = getDefaultFactory().newInstance(name);
77          return new InternalLogger() {
78  
79              public void debug(String msg) {
80                  logger.debug(msg);
81              }
82  
83              public void debug(String msg, Throwable cause) {
84                  StackTraceSimplifier.simplify(cause);
85                  logger.debug(msg, cause);
86              }
87  
88              public void error(String msg) {
89                  logger.error(msg);
90              }
91  
92              public void error(String msg, Throwable cause) {
93                  StackTraceSimplifier.simplify(cause);
94                  logger.error(msg, cause);
95              }
96  
97              public void info(String msg) {
98                  logger.info(msg);
99              }
100 
101             public void info(String msg, Throwable cause) {
102                 StackTraceSimplifier.simplify(cause);
103                 logger.info(msg, cause);
104             }
105 
106             public boolean isDebugEnabled() {
107                 return logger.isDebugEnabled();
108             }
109 
110             public boolean isErrorEnabled() {
111                 return logger.isErrorEnabled();
112             }
113 
114             public boolean isInfoEnabled() {
115                 return logger.isInfoEnabled();
116             }
117 
118             public boolean isWarnEnabled() {
119                 return logger.isWarnEnabled();
120             }
121 
122             public void warn(String msg) {
123                 logger.warn(msg);
124             }
125 
126             public void warn(String msg, Throwable cause) {
127                 StackTraceSimplifier.simplify(cause);
128                 logger.warn(msg, cause);
129             }
130 
131             public boolean isEnabled(InternalLogLevel level) {
132                 return logger.isEnabled(level);
133             }
134 
135             public void log(InternalLogLevel level, String msg) {
136                 logger.log(level, msg);
137             }
138 
139             public void log(InternalLogLevel level, String msg, Throwable cause) {
140                 StackTraceSimplifier.simplify(cause);
141                 logger.log(level, msg, cause);
142             }
143         };
144     }
145 
146     /**
147      * Creates a new logger instance with the specified name.
148      */
149     public abstract InternalLogger newInstance(String name);
150 }