001/**
002 * Copyright (c) 2004-2011 QOS.ch
003 * All rights reserved.
004 *
005 * Permission is hereby granted, free  of charge, to any person obtaining
006 * a  copy  of this  software  and  associated  documentation files  (the
007 * "Software"), to  deal in  the Software without  restriction, including
008 * without limitation  the rights to  use, copy, modify,  merge, publish,
009 * distribute,  sublicense, and/or sell  copies of  the Software,  and to
010 * permit persons to whom the Software  is furnished to do so, subject to
011 * the following conditions:
012 *
013 * The  above  copyright  notice  and  this permission  notice  shall  be
014 * included in all copies or substantial portions of the Software.
015 *
016 * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
017 * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
018 * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
019 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
020 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
021 * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
022 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023 *
024 */
025package org.slf4j.helpers;
026
027import java.util.ArrayList;
028import java.util.List;
029import java.util.Map;
030import java.util.concurrent.ConcurrentHashMap;
031import java.util.concurrent.LinkedBlockingQueue;
032
033import org.slf4j.ILoggerFactory;
034import org.slf4j.Logger;
035import org.slf4j.event.SubstituteLoggingEvent;
036
037/**
038 * SubstituteLoggerFactory manages instances of {@link SubstituteLogger}.
039 *
040 * @author Ceki Gülcü
041 * @author Chetan Mehrotra
042 */
043public class SubstituteLoggerFactory implements ILoggerFactory {
044
045    volatile boolean postInitialization = false;
046    
047    final Map<String, SubstituteLogger> loggers = new ConcurrentHashMap<String, SubstituteLogger>();
048
049    final LinkedBlockingQueue<SubstituteLoggingEvent> eventQueue = new LinkedBlockingQueue<SubstituteLoggingEvent>();
050
051    synchronized public  Logger getLogger(String name) {
052        SubstituteLogger logger = loggers.get(name);
053        if (logger == null) {
054            logger = new SubstituteLogger(name, eventQueue, postInitialization);
055            loggers.put(name, logger);
056        }
057        return logger;
058    }
059
060    public List<String> getLoggerNames() {
061        return new ArrayList<String>(loggers.keySet());
062    }
063
064    public List<SubstituteLogger> getLoggers() {
065        return new ArrayList<SubstituteLogger>(loggers.values());
066    }
067
068    public LinkedBlockingQueue<SubstituteLoggingEvent> getEventQueue() {
069        return eventQueue;
070    }
071
072    public void postInitialization() {
073        postInitialization = true;
074    }
075    
076    public void clear() {
077        loggers.clear();
078        eventQueue.clear();
079    }
080}