001
002package io.vrap.rmf.base.client.http;
003
004import java.util.function.Supplier;
005
006import org.slf4j.Logger;
007import org.slf4j.LoggerFactory;
008import org.slf4j.event.Level;
009
010/**
011 * Unified logger instance to perform logging only for configured logging levels.
012 * Creates a hierarchy of loggers for requests and responses
013 */
014public class InternalLogger {
015    private final Logger underlyingLogger;
016
017    public static final String TOPIC_REQUEST = "request";
018    public static final String TOPIC_RESPONSE = "response";
019
020    protected InternalLogger(final Logger underlyingLogger) {
021        this.underlyingLogger = underlyingLogger;
022    }
023
024    public static InternalLogger getLogger(final Class<?> clazz) {
025        return new InternalLogger(LoggerFactory.getLogger(clazz));
026    }
027
028    public static InternalLogger getLogger(final String loggerName) {
029        return new InternalLogger(LoggerFactory.getLogger(loggerName));
030    }
031
032    public boolean isTraceEnabled() {
033        return underlyingLogger.isTraceEnabled();
034    }
035
036    public InternalLogger debug(final Supplier<Object> message) {
037        if (underlyingLogger.isDebugEnabled()) {
038            underlyingLogger.debug(message.get().toString());
039        }
040        return this;
041    }
042
043    public InternalLogger debug(final Supplier<Object> message, final Throwable throwable) {
044        if (underlyingLogger.isDebugEnabled()) {
045            underlyingLogger.debug(message.get().toString(), throwable);
046        }
047        return this;
048    }
049
050    public InternalLogger info(final Supplier<Object> message) {
051        if (underlyingLogger.isInfoEnabled()) {
052            underlyingLogger.info(message.get().toString());
053        }
054        return this;
055    }
056
057    public InternalLogger info(final Supplier<Object> message, final Throwable throwable) {
058        if (underlyingLogger.isInfoEnabled()) {
059            underlyingLogger.info(message.get().toString(), throwable);
060        }
061        return this;
062    }
063
064    public InternalLogger trace(final Supplier<Object> message) {
065        if (isTraceEnabled()) {
066            underlyingLogger.trace(message.get().toString());
067        }
068        return this;
069    }
070
071    public InternalLogger trace(final Supplier<Object> message, final Throwable throwable) {
072        if (underlyingLogger.isTraceEnabled()) {
073            underlyingLogger.trace(message.get().toString(), throwable);
074        }
075        return this;
076    }
077
078    public InternalLogger warn(final Supplier<Object> message) {
079        if (underlyingLogger.isWarnEnabled()) {
080            underlyingLogger.warn(message.get().toString());
081        }
082        return this;
083    }
084
085    public InternalLogger warn(final Supplier<Object> message, final Throwable throwable) {
086        if (underlyingLogger.isWarnEnabled()) {
087            underlyingLogger.warn(message.get().toString(), throwable);
088        }
089        return this;
090    }
091
092    public InternalLogger error(final Supplier<Object> message) {
093        if (underlyingLogger.isErrorEnabled()) {
094            underlyingLogger.error(message.get().toString());
095        }
096        return this;
097    }
098
099    public InternalLogger error(final Supplier<Object> message, final Throwable throwable) {
100        if (underlyingLogger.isErrorEnabled()) {
101            underlyingLogger.error(message.get().toString(), throwable);
102        }
103        return this;
104    }
105
106    public InternalLogger log(final Level level, final Supplier<Object> message) {
107        switch (level) {
108            case INFO:
109                return info(message);
110            case ERROR:
111                return error(message);
112            case WARN:
113                return warn(message);
114            case DEBUG:
115                return debug(message);
116            case TRACE:
117                return trace(message);
118        }
119        return this;
120    }
121
122    public InternalLogger log(final Level level, final Supplier<Object> message, final Throwable throwable) {
123        switch (level) {
124            case INFO:
125                return info(message, throwable);
126            case ERROR:
127                return error(message, throwable);
128            case WARN:
129                return warn(message, throwable);
130            case DEBUG:
131                return debug(message, throwable);
132            case TRACE:
133                return trace(message, throwable);
134        }
135        return this;
136    }
137}