package org.slf4j.impl;

import org.slf4j.ILoggerFactory;
import org.slf4j.Logger;
import org.xbib.logging.LogContext;

public final class Slf4jLoggerFactory implements ILoggerFactory {

    private static final org.xbib.logging.Logger.AttachmentKey<Logger> key = new org.xbib.logging.Logger.AttachmentKey<>();

    public Slf4jLoggerFactory() {
    }

    @Override
    public Logger getLogger(final String name) {
        final org.xbib.logging.Logger lmLogger = LogContext.getLogContext().getLogger(name);
        final Logger logger = lmLogger.getAttachment(key);
        if (logger != null) {
            return logger;
        }
        final Logger newLogger = new Slf4jLogger(lmLogger);
        final Logger appearingLogger = lmLogger.attachIfAbsent(key, newLogger);
        return appearingLogger != null ? appearingLogger : newLogger;
    }
}
