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 io.netty.util.internal.logging;
17  
18  import io.netty.util.internal.ThreadLocalRandom;
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 Slf4JLoggerFactory}.  If SLF4J
24   * is not available, {@link Log4JLoggerFactory} is used.  If Log4J is not available,
25   * {@link JdkLoggerFactory} is used.  You can change it to your preferred
26   * logging framework before other Netty classes are loaded:
27   * <pre>
28   * {@link InternalLoggerFactory}.setDefaultFactory(new {@link Log4JLoggerFactory}());
29   * </pre>
30   * Please note that the new default factory is effective only for the classes
31   * which were loaded after the default factory is changed.  Therefore,
32   * {@link #setDefaultFactory(InternalLoggerFactory)} should be called as early
33   * as possible and shouldn't be called more than once.
34   */
35  public abstract class InternalLoggerFactory {
36  
37      private static volatile InternalLoggerFactory defaultFactory =
38              newDefaultFactory(InternalLoggerFactory.class.getName());
39  
40      static {
41          // Initiate some time-consuming background jobs here,
42          // because this class is often initialized at the earliest time.
43          try {
44              Class.forName(ThreadLocalRandom.class.getName(), true, InternalLoggerFactory.class.getClassLoader());
45          } catch (Exception ignored) {
46              // Should not fail, but it does not harm to fail.
47          }
48      }
49  
50      @SuppressWarnings("UnusedCatchParameter")
51      private static InternalLoggerFactory newDefaultFactory(String name) {
52          InternalLoggerFactory f;
53          try {
54              f = new Slf4JLoggerFactory(true);
55              f.newInstance(name).debug("Using SLF4J as the default logging framework");
56          } catch (Throwable t1) {
57              try {
58                  f = new Log4JLoggerFactory();
59                  f.newInstance(name).debug("Using Log4J as the default logging framework");
60              } catch (Throwable t2) {
61                  f = new JdkLoggerFactory();
62                  f.newInstance(name).debug("Using java.util.logging as the default logging framework");
63              }
64          }
65          return f;
66      }
67  
68      /**
69       * Returns the default factory.  The initial default factory is
70       * {@link JdkLoggerFactory}.
71       */
72      public static InternalLoggerFactory getDefaultFactory() {
73          return defaultFactory;
74      }
75  
76      /**
77       * Changes the default factory.
78       */
79      public static void setDefaultFactory(InternalLoggerFactory defaultFactory) {
80          if (defaultFactory == null) {
81              throw new NullPointerException("defaultFactory");
82          }
83          InternalLoggerFactory.defaultFactory = defaultFactory;
84      }
85  
86      /**
87       * Creates a new logger instance with the name of the specified class.
88       */
89      public static InternalLogger getInstance(Class<?> clazz) {
90          return getInstance(clazz.getName());
91      }
92  
93      /**
94       * Creates a new logger instance with the specified name.
95       */
96      public static InternalLogger getInstance(String name) {
97          return getDefaultFactory().newInstance(name);
98      }
99  
100     /**
101      * Creates a new logger instance with the specified name.
102      */
103     protected abstract InternalLogger newInstance(String name);
104 }