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;
021import org.osgi.framework.Bundle;
022
023/**
024 * The factory that supplies the OSGi {@link Logger}s from static methods.
025 */
026@SuppressWarnings({ "PMD.ClassNamingConventions", "PMD.UseUtilityClass" })
027public class LoggerFactory {
028
029    @SuppressWarnings("PMD.FieldNamingConventions")
030    private static final LoggerCatalogue<
031            DefaultLoggerGroup<Lf4OsgiLogger>> catalogue
032                = new LoggerCatalogue<>(
033                    b -> new DefaultLoggerGroup<Lf4OsgiLogger>(b,
034                        (g, n) -> new Lf4OsgiLogger(g, n)));
035
036    /**
037     * Gets a logger with the given name.
038     *
039     * @param name the name
040     * @return the logger
041     */
042    public static Logger getLogger(String name) {
043        Bundle bundle = LoggerCatalogue
044            .findBundle(LoggerFactory.class.getName()).orElse(null);
045        return catalogue.getLoggerGoup(bundle).computeIfAbsent(name,
046            (c, n) -> new Lf4OsgiLogger(c, n));
047    }
048
049    /**
050     * Gets a logger, using the class name as name for the logger.
051     *
052     * @param clazz the clazz
053     * @return the logger
054     */
055    public static Logger getLogger(Class<?> clazz) {
056        String name = clazz.getName();
057        Bundle bundle = LoggerCatalogue
058            .findBundle(LoggerFactory.class.getName()).orElse(null);
059        return catalogue.getLoggerGoup(bundle).computeIfAbsent(name,
060            (c, n) -> new Lf4OsgiLogger(c, n));
061    }
062
063    /**
064     * Gets a logger with the given name for the given bundle.
065     * <P>
066     * If the logging bundle happens to be known in the context in which
067     * {@code getLogger} is called, this method should be preferred over
068     * {@link #getLogger(String)} because the latter implies a small overhead for
069     * finding out the calling bundle.
070     *
071     * @param bundle the bundle
072     * @param name   the name
073     * @return the logger
074     */
075    public static Logger getLogger(Bundle bundle, String name) {
076        return catalogue.getLoggerGoup(bundle).computeIfAbsent(name,
077            (c, n) -> new Lf4OsgiLogger(c, n));
078    }
079
080    /**
081     * Gets a logger with the given class' name for the given bundle.
082     * <P>
083     * If the logging bundle happens to be known in the context in which
084     * {@code getLogger} is called, this method should be preferred over
085     * {@link #getLogger(Class)} because the latter implies a small overhead for
086     * finding out the calling bundle.
087     *
088     * @param bundle the bundle
089     * @param clazz  the class
090     * @return the logger
091     */
092    public static Logger getLogger(Bundle bundle, Class<?> clazz) {
093        String name = clazz.getName();
094        return catalogue.getLoggerGoup(bundle).computeIfAbsent(name,
095            (c, n) -> new Lf4OsgiLogger(c, n));
096    }
097
098}