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  /**
19   * Creates an {@link InternalLogger} or changes the default factory
20   * implementation.  This factory allows you to choose what logging framework
21   * Netty should use.  The default factory is {@link JdkLoggerFactory}.
22   * You can change it to your preferred logging framework before other Netty
23   * classes are loaded:
24   * <pre>
25   * {@link InternalLoggerFactory}.setDefaultFactory(new {@link Log4JLoggerFactory}());
26   * </pre>
27   * Please note that the new default factory is effective only for the classes
28   * which were loaded after the default factory is changed.  Therefore,
29   * {@link #setDefaultFactory(InternalLoggerFactory)} should be called as early
30   * as possible and shouldn't be called more than once.
31   *
32   * @apiviz.landmark
33   * @apiviz.has org.jboss.netty.logging.InternalLogger oneway - - creates
34   */
35  public abstract class InternalLoggerFactory {
36      private static volatile InternalLoggerFactory defaultFactory = new JdkLoggerFactory();
37  
38      /**
39       * Returns the default factory.  The initial default factory is
40       * {@link JdkLoggerFactory}.
41       */
42      public static InternalLoggerFactory getDefaultFactory() {
43          return defaultFactory;
44      }
45  
46      /**
47       * Changes the default factory.
48       */
49      public static void setDefaultFactory(InternalLoggerFactory defaultFactory) {
50          if (defaultFactory == null) {
51              throw new NullPointerException("defaultFactory");
52          }
53          InternalLoggerFactory.defaultFactory = defaultFactory;
54      }
55  
56      /**
57       * Creates a new logger instance with the name of the specified class.
58       */
59      public static InternalLogger getInstance(Class<?> clazz) {
60          return getInstance(clazz.getName());
61      }
62  
63      /**
64       * Creates a new logger instance with the specified name.
65       */
66      public static InternalLogger getInstance(String name) {
67          return getDefaultFactory().newInstance(name);
68      }
69  
70      /**
71       * Creates a new logger instance with the specified name.
72       */
73      public abstract InternalLogger newInstance(String name);
74  }