001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.util.sl4fj;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    
020    import java.util.HashMap;
021    import java.util.Map;
022    import java.util.concurrent.locks.Lock;
023    import java.util.concurrent.locks.ReadWriteLock;
024    import java.util.concurrent.locks.ReentrantReadWriteLock;
025    
026    import org.slf4j.ILoggerFactory;
027    import org.slf4j.Logger;
028    
029    /**
030     * @author Michael C. Han
031     */
032    public class LiferayLoggerFactory implements ILoggerFactory {
033    
034            public LiferayLoggerFactory() {
035                    ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
036    
037                    _readLock = readWriteLock.readLock();
038                    _writeLock = readWriteLock.writeLock();
039            }
040    
041            @Override
042            public Logger getLogger(String name) {
043                    Logger logger = null;
044    
045                    _readLock.lock();
046    
047                    try {
048                            logger = _loggers.get(name);
049                    }
050                    finally {
051                            _readLock.unlock();
052                    }
053    
054                    if (logger == null) {
055                            _writeLock.lock();
056    
057                            try {
058                                    Log log = LogFactoryUtil.getLog(name);
059    
060                                    logger = new LiferayLoggerAdapter(log);
061    
062                                    _loggers.put(name, logger);
063                            }
064                            finally {
065                                    _writeLock.unlock();
066                            }
067                    }
068    
069                    return logger;
070            }
071    
072            private Map<String, Logger> _loggers = new HashMap<String, Logger>();
073            private Lock _readLock;
074            private Lock _writeLock;
075    
076    }