001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.slf4j;
018
019import java.util.concurrent.ConcurrentHashMap;
020import java.util.concurrent.ConcurrentMap;
021
022import org.apache.logging.log4j.message.MessageFactory;
023import org.apache.logging.log4j.spi.ExtendedLogger;
024import org.apache.logging.log4j.spi.LoggerContext;
025import org.apache.logging.log4j.spi.LoggerContextKey;
026import org.slf4j.LoggerFactory;
027
028/**
029 *
030 */
031public class SLF4JLoggerContext implements LoggerContext {
032    private final ConcurrentMap<String, SLF4JLogger> loggers = new ConcurrentHashMap<>();
033
034    @Override
035    public Object getExternalContext() {
036        return null;
037    }
038
039    @Override
040    public ExtendedLogger getLogger(final String name) {
041        if (!loggers.containsKey(name)) {
042            loggers.putIfAbsent(name, new SLF4JLogger(name, LoggerFactory.getLogger(name)));
043        }
044        return loggers.get(name);
045    }
046
047    @Override
048    public ExtendedLogger getLogger(final String name, final MessageFactory messageFactory) {
049        if (!loggers.containsKey(name)) {
050            loggers.putIfAbsent(name, new SLF4JLogger(name, messageFactory, LoggerFactory.getLogger(name)));
051        }
052        return loggers.get(name);
053    }
054
055    @Override
056    public boolean hasLogger(final String name) {
057        return loggers.containsKey(LoggerContextKey.create(name));
058    }
059
060    @Override
061    public boolean hasLogger(String name, MessageFactory messageFactory) {
062        return loggers.containsKey(LoggerContextKey.create(name, messageFactory));
063    }
064
065    @Override
066    public boolean hasLogger(String name, Class<? extends MessageFactory> messageFactoryClass) {
067        return loggers.containsKey(LoggerContextKey.create(name, messageFactoryClass));
068    }
069}