001/*
002 * Copyright (C) 2019 Michael N. Lipp (http://www.mnl.de)
003 * 
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *        http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package de.mnl.osgi.lf4osgi;
018
019import de.mnl.osgi.lf4osgi.core.DefaultLoggerGroup;
020import de.mnl.osgi.lf4osgi.core.LoggerCatalogue;
021
022import org.osgi.framework.Bundle;
023
024/**
025 * The factory that supplies the OSGi {@link Logger}s from static
026 * methods.
027 */
028@SuppressWarnings({ "PMD.ClassNamingConventions", "PMD.UseUtilityClass" })
029public class LoggerFactory {
030
031    @SuppressWarnings("PMD.FieldNamingConventions")
032    private static final LoggerCatalogue<
033            DefaultLoggerGroup<Lf4OsgiLogger>> catalogue
034                = new LoggerCatalogue<>(
035                    b -> new DefaultLoggerGroup<Lf4OsgiLogger>(b,
036                        (g, n) -> new Lf4OsgiLogger(g, n)));
037
038    /**
039     * Gets a logger with the given name.
040     *
041     * @param name the name
042     * @return the logger
043     */
044    public static Logger getLogger(String name) {
045        Bundle bundle = LoggerCatalogue
046            .findBundle(LoggerFactory.class.getName()).orElse(null);
047        return catalogue.getLoggerGoup(bundle).computeIfAbsent(name,
048            (c, n) -> new Lf4OsgiLogger(c, n));
049    }
050
051    /**
052     * Gets a logger, using the class name as name for the logger.
053     *
054     * @param clazz the clazz
055     * @return the logger
056     */
057    public static Logger getLogger(Class<?> clazz) {
058        String name = clazz.getName();
059        Bundle bundle = LoggerCatalogue
060            .findBundle(LoggerFactory.class.getName()).orElse(null);
061        return catalogue.getLoggerGoup(bundle).computeIfAbsent(name,
062            (c, n) -> new Lf4OsgiLogger(c, n));
063    }
064
065    /**
066     * Gets a logger with the given name for the given bundle.
067     * <P>
068     * If the logging bundle happens to be known in the context in which 
069     * {@code getLogger} is called, this method should be preferred over 
070     * {@link #getLogger(String)} because the latter implies a small
071     * overhead for finding out the calling bundle.
072     *
073     * @param bundle the bundle
074     * @param name the name
075     * @return the logger
076     */
077    public static Logger getLogger(Bundle bundle, String name) {
078        return catalogue.getLoggerGoup(bundle).computeIfAbsent(name,
079            (c, n) -> new Lf4OsgiLogger(c, n));
080    }
081
082    /**
083     * Gets a logger with the given class' name for the given bundle.
084     * <P>
085     * If the logging bundle happens to be known in the context in which 
086     * {@code getLogger} is called, this method should be preferred over 
087     * {@link #getLogger(Class)} because the latter implies a small
088     * overhead for finding out the calling bundle.
089     *
090     * @param bundle the bundle
091     * @param clazz the class
092     * @return the logger
093     */
094    public static Logger getLogger(Bundle bundle, Class<?> clazz) {
095        String name = clazz.getName();
096        return catalogue.getLoggerGoup(bundle).computeIfAbsent(name,
097            (c, n) -> new Lf4OsgiLogger(c, n));
098    }
099
100}