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 com.foxinmy.weixin4j.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 Slf4JLoggerFactory}. If SLF4J
22   * is not available, {@link JdkLoggerFactory} is used. You can change it to your
23   * preferred logging framework before other Netty classes are loaded:
24   * 
25   * <pre>
26   * {@link InternalLoggerFactory}.setDefaultFactory(new {@link Log4JLoggerFactory}());
27   * </pre>
28   * 
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  	private static volatile InternalLoggerFactory defaultFactory = newDefaultFactory(InternalLoggerFactory.class
36  			.getName());
37  
38  	private static InternalLoggerFactory newDefaultFactory(String name) {
39  		InternalLoggerFactory f;
40  		try {
41  			f = new Slf4JLoggerFactory(true);
42  			f.newInstance(name).debug(
43  					"Using SLF4J as the default logging framework");
44  		} catch (Throwable t1) {
45  			f = new JdkLoggerFactory();
46  			f.newInstance(name).debug(
47  					"Using java.util.logging as the default logging framework");
48  		}
49  		return f;
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  		return getDefaultFactory().newInstance(name);
82  	}
83  
84  	/**
85  	 * Creates a new logger instance with the specified name.
86  	 */
87  	protected abstract InternalLogger newInstance(String name);
88  }