com.couchbase.client.core.logging.CouchbaseLoggerFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of core-io Show documentation
Show all versions of core-io Show documentation
The official Couchbase JVM Core IO Library
/**
* Copyright (C) 2014 Couchbase, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALING
* IN THE SOFTWARE.
*/
/*
* Copyright 2012 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package com.couchbase.client.core.logging;
import com.couchbase.client.deps.io.netty.util.internal.ThreadLocalRandom;
/**
* Creates an {@link CouchbaseLogger} or changes the default factory
* implementation. This factory allows you to choose what logging framework
* Netty should use. The default factory is {@link Slf4JLoggerFactory}. If SLF4J
* is not available, {@link Log4JLoggerFactory} is used. If Log4J is not available,
* {@link JdkLoggerFactory} is used. You can change it to your preferred
* logging framework before other SDK classes are loaded:
*
* {@link CouchbaseLoggerFactory}.setDefaultFactory(new {@link Log4JLoggerFactory}());
*
* Please note that the new default factory is effective only for the classes
* which were loaded after the default factory is changed. Therefore,
* {@link #setDefaultFactory(CouchbaseLoggerFactory)} should be called as early
* as possible and shouldn't be called more than once.
*/
public abstract class CouchbaseLoggerFactory {
private static volatile CouchbaseLoggerFactory defaultFactory =
newDefaultFactory(CouchbaseLoggerFactory.class.getName());
static {
// Initiate some time-consuming background jobs here,
// because this class is often initialized at the earliest time.
try {
Class.forName(ThreadLocalRandom.class.getName(), true, CouchbaseLoggerFactory.class.getClassLoader());
} catch (Exception ignored) {
// Should not fail, but it does not harm to fail.
}
}
@SuppressWarnings("UnusedCatchParameter")
private static CouchbaseLoggerFactory newDefaultFactory(String name) {
CouchbaseLoggerFactory f;
try {
f = new Slf4JLoggerFactory(true);
f.newInstance(name).debug("Using SLF4J as the default logging framework");
} catch (Throwable t1) {
try {
f = new Log4JLoggerFactory();
f.newInstance(name).debug("Using Log4J as the default logging framework");
} catch (Throwable t2) {
f = new JdkLoggerFactory();
f.newInstance(name).debug("Using java.util.logging as the default logging framework");
}
}
return f;
}
/**
* Returns the default factory. The initial default factory is
* {@link JdkLoggerFactory}.
*/
public static CouchbaseLoggerFactory getDefaultFactory() {
return defaultFactory;
}
/**
* Changes the default factory.
*/
public static void setDefaultFactory(CouchbaseLoggerFactory defaultFactory) {
if (defaultFactory == null) {
throw new NullPointerException("defaultFactory");
}
CouchbaseLoggerFactory.defaultFactory = defaultFactory;
}
/**
* Creates a new logger instance with the name of the specified class.
*/
public static CouchbaseLogger getInstance(Class clazz) {
return getInstance(clazz.getName());
}
/**
* Creates a new logger instance with the specified name.
*/
public static CouchbaseLogger getInstance(String name) {
return getDefaultFactory().newInstance(name);
}
/**
* Creates a new logger instance with the specified name.
*/
protected abstract CouchbaseLogger newInstance(String name);
}