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  
17  package io.netty.util.internal.logging;
18  
19  /**
20   * Creates an {@link InternalLogger} or changes the default factory
21   * implementation.  This factory allows you to choose what logging framework
22   * Netty should use.  The default factory is {@link Slf4JLoggerFactory}.  If SLF4J
23   * is not available, {@link Log4JLoggerFactory} is used.  If Log4J is not available,
24   * {@link JdkLoggerFactory} is used.  You can change it to your preferred
25   * logging framework before other Netty classes are loaded:
26   * <pre>
27   * {@link InternalLoggerFactory}.setDefaultFactory({@link Log4JLoggerFactory}.INSTANCE);
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  public abstract class InternalLoggerFactory {
35  
36      private static volatile InternalLoggerFactory defaultFactory;
37  
38      @SuppressWarnings("UnusedCatchParameter")
39      private static InternalLoggerFactory newDefaultFactory(String name) {
40          InternalLoggerFactory f;
41          try {
42              f = new Slf4JLoggerFactory(true);
43              f.newInstance(name).debug("Using SLF4J as the default logging framework");
44          } catch (Throwable t1) {
45              try {
46                  f = Log4JLoggerFactory.INSTANCE;
47                  f.newInstance(name).debug("Using Log4J as the default logging framework");
48              } catch (Throwable t2) {
49                  f = JdkLoggerFactory.INSTANCE;
50                  f.newInstance(name).debug("Using java.util.logging as the default logging framework");
51              }
52          }
53          return f;
54      }
55  
56      /**
57       * Returns the default factory.  The initial default factory is
58       * {@link JdkLoggerFactory}.
59       */
60      public static InternalLoggerFactory getDefaultFactory() {
61          if (defaultFactory == null) {
62              defaultFactory = newDefaultFactory(InternalLoggerFactory.class.getName());
63          }
64          return defaultFactory;
65      }
66  
67      /**
68       * Changes the default factory.
69       */
70      public static void setDefaultFactory(InternalLoggerFactory defaultFactory) {
71          if (defaultFactory == null) {
72              throw new NullPointerException("defaultFactory");
73          }
74          InternalLoggerFactory.defaultFactory = defaultFactory;
75      }
76  
77      /**
78       * Creates a new logger instance with the name of the specified class.
79       */
80      public static InternalLogger getInstance(Class<?> clazz) {
81          return getInstance(clazz.getName());
82      }
83  
84      /**
85       * Creates a new logger instance with the specified name.
86       */
87      public static InternalLogger getInstance(String name) {
88          return getDefaultFactory().newInstance(name);
89      }
90  
91      /**
92       * Creates a new logger instance with the specified name.
93       */
94      protected abstract InternalLogger newInstance(String name);
95  
96  }