All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ops4j.pax.logging.slf4j.Slf4jMDCAdapter Maven / Gradle / Ivy

Go to download

Pax Logging API Library is a collection of logging APIs from different libraries/facades. It supports SLF4J, Commons Logging, JULI Logging, Log4J1 API, Log4J2 API, JBoss Logging and Avalon APIs. Additionally, Pax Logging specific library is available as backend implementation with its specific configuration mechanisms, but it's not required.

The newest version!
/*
 * Copyright 2006 Niclas Hedhman.
 *
 * Licensed  under the  Apache License,  Version 2.0  (the "License");
 * you may not use  this file  except in  compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed  under the  License is distributed on an "AS IS" BASIS,
 * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
 * implied.
 *
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.ops4j.pax.logging.slf4j;

import java.util.Map;

import org.ops4j.pax.logging.PaxContext;
import org.ops4j.pax.logging.PaxLoggingManager;
import org.ops4j.pax.logging.PaxLoggingService;
import org.slf4j.spi.MDCAdapter;

/**
 * pax-logging specific {@link MDCAdapter} returned from {@link org.slf4j.impl.StaticMDCBinder}
 */
public class Slf4jMDCAdapter implements MDCAdapter {

    /** {@link PaxContext} used when {@link org.ops4j.pax.logging.PaxLoggingService} is not available */
    private static PaxContext m_defaultContext = new PaxContext();
    /** {@link PaxContext} obtained from {@link org.ops4j.pax.logging.PaxLoggingService} */
    private static PaxContext m_context;

    /**
     * For all the methods that use the context, default, static, {@link PaxContext} may be used (tied to pax-logging-api
     * bundle) if there's no available {@link PaxLoggingManager} or {@link PaxLoggingService}. If the service is
     * available, it is always used to get service specific {@link PaxContext}.
     *
     * Refering always to {@link PaxLoggingService#getPaxContext()} is cheap operation, as it's
     * only reference to fields.
     *
     * See: https://ops4j1.jira.com/browse/PAXLOGGING-247
     *
     * @return m_context if the MDC should use the PaxContext object from the PaxLoggingManager,
     *      or m_defaultContext if the logging manager is not set, or does not have its context available yet.
     */
    private static PaxContext getContext() {
        PaxLoggingManager manager = Slf4jLoggerFactory.m_paxLogging;
        if (manager != null) {
            synchronized (Slf4jMDCAdapter.class) {
                PaxLoggingService service = manager.getPaxLoggingService();
                m_context = service != null ? service.getPaxContext() : null;
            }
        }
        return m_context != null ? m_context : m_defaultContext;
    }

    @Override
    public void put(String key, String val) {
        getContext().put(key, val);
    }

    @Override
    public String get(String key) {
        Object value = getContext().get(key);
        return value != null ? value.toString() : null;
    }

    @Override
    public void remove(String key) {
        getContext().remove(key);
    }

    @Override
    public void clear() {
        getContext().clear();
    }

    @Override
    @SuppressWarnings("unchecked")
    public Map getCopyOfContextMap() {
        return getContext().getCopyOfContextMap();
    }

    @Override
    @SuppressWarnings("unchecked")
    public void setContextMap(Map contextMap) {
        getContext().setContextMap(contextMap);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy