All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.microsphere.logging.LoggerFactory Maven / Gradle / Ivy

The newest version!
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF 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 io.microsphere.logging;

import io.microsphere.lang.Prioritized;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.List;

import static io.microsphere.collection.ListUtils.newLinkedList;
import static java.util.Collections.sort;
import static java.util.ServiceLoader.load;

/**
 * The factory class for {@link Logger}
 *
 * @author Mercy
 * @see Logger
 * @since 1.0.0
 */
public abstract class LoggerFactory {

    private static final ClassLoader classLoader = LoggerFactory.class.getClassLoader();

    @Nullable
    private static final LoggerFactory factory = loadFactory();

    @Nullable
    private static LoggerFactory loadFactory() {
        List availableFactories = loadAvailableFactories();
        return availableFactories.isEmpty() ? null : availableFactories.get(0);
    }

    static List loadAvailableFactories() {
        List factories = loadFactories();
        factories.removeIf(factory -> !factory.isAvailable());
        return factories;
    }

    static List loadFactories() {
        List factories = newLinkedList(load(LoggerFactory.class, classLoader));
        sort(factories, Prioritized.COMPARATOR);
        return factories;
    }

    /**
     * Get an instance of {@link Logger} by type
     *
     * @param type the type
     * @return non-null
     */
    @Nonnull
    public static Logger getLogger(Class type) {
        return getLogger(type.getName());
    }

    /**
     * Get an instance of {@link Logger} by name
     *
     * @param name the name of {@link Logger}
     * @return {@link Logger}
     */
    @Nonnull
    public static Logger getLogger(String name) {
        if (factory == null) {
            return new NoOpLogger(name);
        }
        return factory.createLogger(name);
    }

    /**
     * Current {@link LoggerFactory} is available or not
     *
     * @return true if available
     */
    protected boolean isAvailable() {
        return getDelegateLoggerClass() != null;
    }

    /**
     * The class of delegate Logger
     *
     * @return null if not found
     */
    private Class getDelegateLoggerClass() {
        String className = getDelegateLoggerClassName();
        Class delegateLoggerClass = null;
        try {
            delegateLoggerClass = classLoader.loadClass(className);
        } catch (Throwable e) {
        }
        return delegateLoggerClass;
    }

    /**
     * The class name of delegate Logger
     *
     * @return non-null
     */
    protected abstract String getDelegateLoggerClassName();

    /**
     * Create a new {@link Logger}
     *
     * @param name the name of {@link Logger }
     * @return non-null
     */
    public abstract Logger createLogger(String name);

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy