001
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
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 }