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