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

com.openhtmltopdf.slf4j.Slf4jLogger Maven / Gradle / Ivy

Go to download

Open HTML to PDF is a CSS 2.1 renderer written in Java. This artifact supports sending Open HTML to PDF log output to the slf4j logging facade.

The newest version!
/*
 * {{{ header & license
 * Copyright (c) 2007 Wisconsin Court System
 *
 * This program 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 2.1
 * 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 * }}}
 */
package com.openhtmltopdf.slf4j;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;

import com.openhtmltopdf.util.Diagnostic;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.openhtmltopdf.util.XRLog;
import com.openhtmltopdf.util.XRLogger;

public class Slf4jLogger implements XRLogger {
    private static final String DEFAULT_LOGGER_NAME = "com.openhtmltopdf.other";
    
    private static final Map LOGGER_NAME_MAP;
    static {
        LOGGER_NAME_MAP = new HashMap<>();
        
        LOGGER_NAME_MAP.put(XRLog.CONFIG, "com.openhtmltopdf.config");
        LOGGER_NAME_MAP.put(XRLog.EXCEPTION, "com.openhtmltopdf.exception");
        LOGGER_NAME_MAP.put(XRLog.GENERAL, "com.openhtmltopdf.general");
        LOGGER_NAME_MAP.put(XRLog.INIT, "com.openhtmltopdf.init");
        LOGGER_NAME_MAP.put(XRLog.JUNIT, "com.openhtmltopdf.junit");
        LOGGER_NAME_MAP.put(XRLog.LOAD, "com.openhtmltopdf.load");
        LOGGER_NAME_MAP.put(XRLog.MATCH, "com.openhtmltopdf.match");
        LOGGER_NAME_MAP.put(XRLog.CASCADE, "com.openhtmltopdf.cascade");
        LOGGER_NAME_MAP.put(XRLog.XML_ENTITIES, "com.openhtmltopdf.load.xmlentities");
        LOGGER_NAME_MAP.put(XRLog.CSS_PARSE, "com.openhtmltopdf.cssparse");
        LOGGER_NAME_MAP.put(XRLog.LAYOUT, "com.openhtmltopdf.layout");
        LOGGER_NAME_MAP.put(XRLog.RENDER, "com.openhtmltopdf.render");
    }
    
    private String _defaultLoggerName = DEFAULT_LOGGER_NAME;
    private Map _loggerNameMap = LOGGER_NAME_MAP;

    @Override
    public boolean isLogLevelEnabled(Diagnostic diagnostic) {
        Level level = diagnostic.getLevel();
        Logger logger = LoggerFactory.getLogger(getLoggerName(diagnostic.getLogMessageId().getWhere()));
        if (level == Level.SEVERE) {
            return logger.isErrorEnabled();
        } else if (level == Level.WARNING) {
            return logger.isWarnEnabled();
        } else if (level == Level.INFO || level == Level.CONFIG) {
            return logger.isInfoEnabled();
        } else if (level == Level.FINE || level == Level.FINER || level == Level.FINEST) {
            return logger.isDebugEnabled();
        } else {
            return true;
        }
    }

    @Override
    public void log(Diagnostic diagnostic) {
        Logger logger = LoggerFactory.getLogger(getLoggerName(diagnostic.getLogMessageId().getWhere()));
        Level level = diagnostic.getLevel();
        String msg = diagnostic.getLogMessageId().getMessageFormat();
        Object[] args = diagnostic.getArgs();
        if (level == Level.SEVERE)
            logger.error(msg, args);
        else if (level == Level.WARNING)
            logger.warn(msg, args);
        else if (level == Level.INFO || level == Level.CONFIG)
            logger.info(msg, args);
        else if (level == Level.FINE || level == Level.FINER || level == Level.FINEST)
            logger.debug(msg, args);
        else
            logger.info(msg, args);
    }

    @Override
    public void log(String where, Level level, String msg) {
    	Logger logger = LoggerFactory.getLogger(getLoggerName(where));
    	
    	if (level == Level.SEVERE)
    		logger.error(msg);
    	else if (level == Level.WARNING)
    		logger.warn(msg);
    	else if (level == Level.INFO || level == Level.CONFIG)
    		logger.info(msg);
    	else if (level == Level.FINE || level == Level.FINER || level == Level.FINEST)
    		logger.debug(msg);
    	else
    		logger.info(msg);
    }

    @Override
    public void log(String where, Level level, String msg, Throwable th) {
    	Logger logger = LoggerFactory.getLogger(getLoggerName(where));
    	
    	if (level == Level.SEVERE)
    		logger.error(msg, th);
    	else if (level == Level.WARNING)
    		logger.warn(msg, th);
    	else if (level == Level.INFO || level == Level.CONFIG)
    		logger.info(msg, th);
    	else if (level == Level.FINE || level == Level.FINER || level == Level.FINEST)
    		logger.debug(msg, th);
    	else
    		logger.info(msg, th);
    }
    
    private String getLoggerName(String xrLoggerName) {
        String result = _loggerNameMap.get(xrLoggerName);
        if (result != null) {
            return result;
        } else {
            return _defaultLoggerName;
        }
    }

    @Override
    public void setLevel(String logger, Level level) {
        throw new UnsupportedOperationException("log4j should be not be configured here");
    }
    
    public Map getLoggerNameMap() {
        return _loggerNameMap;
    }

    public void setLoggerNameMap(Map loggerNameMap) {
        _loggerNameMap = loggerNameMap;
    }

    public String getDefaultLoggerName() {
        return _defaultLoggerName;
    }

    public void setDefaultLoggerName(String defaultLoggerName) {
        _defaultLoggerName = defaultLoggerName;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy