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

org.guppy4j.log.Slf4jLog Maven / Gradle / Ivy

There is a newer version: 0.0.7
Show newest version
package org.guppy4j.log;

import org.slf4j.Logger;

import static org.slf4j.helpers.MessageFormatter.arrayFormat;

/**
 * Logs using an slf4j logger
 */
public class Slf4jLog implements Log {

    private final Logger logger;

    public Slf4jLog(Logger logger) {
        this.logger = logger;
    }

    @Override
    public void as(Level level, String message, Object... parameters) {
        switch (level) {
            case trace:
                logger.trace(message, parameters);
                break;
            case debug:
                logger.debug(message, parameters);
                break;
            case info:
                logger.info(message, parameters);
                break;
            case warn:
                logger.warn(message, parameters);
                break;
            case error:
                logger.error(message, parameters);
                break;
        }
    }

    @Override
    public void as(Level level, Throwable t) {
        as(level, "", t);
    }

    @Override
    public void as(Level level, Throwable t, String messageFormat, Object... parameters) {
        if (isEnabled(level)) {
            final String message = arrayFormat(messageFormat, parameters).getMessage();
            switch (level) {
                case error:
                    logger.error(message, t);
                    break;
                case warn:
                    logger.warn(message, t);
                    break;
                case info:
                    logger.info(message, t);
                    break;
                case debug:
                    logger.debug(message, t);
                    break;
                case trace:
                    logger.trace(message, t);
                    break;
            }
        }
    }

    private boolean isEnabled(Level level) {
        switch (level) {
            case error:
                return logger.isErrorEnabled();
            case warn:
                return logger.isWarnEnabled();
            case info:
                return logger.isInfoEnabled();
            case debug:
                return logger.isDebugEnabled();
            case trace:
                return logger.isTraceEnabled();
            default:
                throw new IllegalArgumentException("Unknown log level: level");
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy