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