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

org.coos.messaging.util.Slf4jLogImpl Maven / Gradle / Ivy

There is a newer version: 1.3.1
Show newest version
/**
 * COOS - Connected Objects Operating System (www.connectedobjects.org).
 *
 * Copyright (C) 2009 Telenor ASA and Tellu AS. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see .
 *
 * You may also contact one of the following for additional information:
 * Telenor ASA, Snaroyveien 30, N-1331 Fornebu, Norway (www.telenor.no)
 * Tellu AS, Hagalokkveien 13, N-1383 Asker, Norway (www.tellu.no)
 */
package org.coos.messaging.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

import java.util.Hashtable;


/**
 * This logger class is a proxy towards slf4j logger class
 *
 * @author Knut Eilif Husa, Tellu AS
 *
 */
public class Slf4jLogImpl implements Log {

    private Logger logger = LoggerFactory.getLogger(Slf4jLogImpl.class);
    private final Hashtable mdc = new Hashtable();
    private boolean inheritMDC = true;

    public void setLoggerName(String loggerName) {
        logger = LoggerFactory.getLogger(loggerName);
    }

    public boolean isInheritMDC() {
        return inheritMDC;
    }

    public void setInheritMDC(boolean inheritMDC) {
        this.inheritMDC = inheritMDC;
    }

    public void debug(String msg) {
        logger.debug(msg);
        updateMdc();

    }

    public void debug(String msg, Exception e) {
        logger.debug(msg, e);
        updateMdc();

    }

    public void error(String msg) {
        logger.error(msg);
        updateMdc();
    }

    public void info(String msg) {
        logger.info(msg);
        updateMdc();

    }

    public void info(String msg, Exception e) {
        logger.info(msg, e);
        updateMdc();

    }

    public void warn(String msg) {
        logger.warn(msg);
        updateMdc();
    }

    public void warn(String msg, Exception e) {
        logger.warn(msg, e);
        updateMdc();
    }

    public void trace(String msg) {
        logger.trace(msg);
        updateMdc();
    }

    public void removeMDC(String key) {
        mdc.remove(key);
        MDC.remove(key);
    }

    public void putMDC(String key, String value) {
        mdc.put(key, value);
    }

    private void updateMdc() {

        if (!inheritMDC) {
            MDC.clear();
        }

        for (String key : mdc.keySet()) {
            MDC.put(key, mdc.get(key));
        }
    }

    @Override public void error(String msg, Exception e) {
        logger.error(msg, e);
        updateMdc();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy