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 org.slf4j.impl;
018
019import de.mnl.osgi.lf4osgi.core.DefaultLoggerGroup;
020import de.mnl.osgi.lf4osgi.core.LoggerCatalogue;
021import org.osgi.framework.Bundle;
022import org.slf4j.ILoggerFactory;
023import org.slf4j.Logger;
024
025/**
026 * An implementation of {@link ILoggerFactory} which returns
027 * {@link Fwd2OsgiLogger} instances.
028 */
029public class Fwd2OsgiLoggerFactory implements ILoggerFactory {
030
031    private final LoggerCatalogue<DefaultLoggerGroup<Fwd2OsgiLogger>> loggers;
032
033    /**
034     * Instantiates a new logger factory.
035     */
036    public Fwd2OsgiLoggerFactory() {
037        loggers = new LoggerCatalogue<>(b -> new DefaultLoggerGroup<>(b,
038            (g, n) -> new Fwd2OsgiLogger(g, n)));
039    }
040
041    /**
042     * Return an appropriate {@link Fwd2OsgiLogger} instance by name.
043     */
044    public Logger getLogger(String name) {
045        Bundle bundle = LoggerCatalogue
046            .findBundle(org.slf4j.LoggerFactory.class.getName()).orElse(null);
047        return loggers.getLoggerGoup(bundle).computeIfAbsent(name);
048    }
049}