Files
maven-mvnd/logging/src/main/java/org/slf4j/impl/MvndBaseLogger.java
2023-07-10 15:16:32 +02:00

300 lines
9.7 KiB
Java

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.slf4j.impl;
import org.slf4j.event.LoggingEvent;
import org.slf4j.helpers.FormattingTuple;
import org.slf4j.helpers.MarkerIgnoringBase;
import org.slf4j.helpers.MessageFormatter;
import org.slf4j.spi.LocationAwareLogger;
public abstract class MvndBaseLogger extends MarkerIgnoringBase {
protected static final int LOG_LEVEL_TRACE = LocationAwareLogger.TRACE_INT;
protected static final int LOG_LEVEL_DEBUG = LocationAwareLogger.DEBUG_INT;
protected static final int LOG_LEVEL_INFO = LocationAwareLogger.INFO_INT;
protected static final int LOG_LEVEL_WARN = LocationAwareLogger.WARN_INT;
protected static final int LOG_LEVEL_ERROR = LocationAwareLogger.ERROR_INT;
/** The current log level */
protected int currentLogLevel = LOG_LEVEL_INFO;
public MvndBaseLogger(String name) {
this.name = name;
}
protected void formatAndLog(int level, String format, Object arg1, Object arg2) {
if (!isLevelEnabled(level)) {
return;
}
FormattingTuple tp = MessageFormatter.format(format, arg1, arg2);
log(level, tp.getMessage(), tp.getThrowable());
}
protected void formatAndLog(int level, String format, Object... arguments) {
if (!isLevelEnabled(level)) {
return;
}
FormattingTuple tp = MessageFormatter.arrayFormat(format, arguments);
log(level, tp.getMessage(), tp.getThrowable());
}
protected void log(int level, String message, Throwable t) {
if (!isLevelEnabled(level)) {
return;
}
doLog(level, message, t);
}
protected abstract void doLog(int level, String message, Throwable t);
/**
* Is the given log level currently enabled?
*
* @param logLevel
* is this level enabled?
*/
protected boolean isLevelEnabled(int logLevel) {
// log level are numerically ordered so can use simple numeric
// comparison
return (logLevel >= currentLogLevel);
}
/** Are {@code trace} messages currently enabled? */
public boolean isTraceEnabled() {
return isLevelEnabled(LOG_LEVEL_TRACE);
}
/**
* A simple implementation which logs messages of level TRACE according to
* the format outlined above.
*/
public void trace(String msg) {
log(LOG_LEVEL_TRACE, msg, null);
}
/**
* Perform single parameter substitution before logging the message of level
* TRACE according to the format outlined above.
*/
public void trace(String format, Object param1) {
formatAndLog(LOG_LEVEL_TRACE, format, param1, null);
}
/**
* Perform double parameter substitution before logging the message of level
* TRACE according to the format outlined above.
*/
public void trace(String format, Object param1, Object param2) {
formatAndLog(LOG_LEVEL_TRACE, format, param1, param2);
}
/**
* Perform double parameter substitution before logging the message of level
* TRACE according to the format outlined above.
*/
public void trace(String format, Object... argArray) {
formatAndLog(LOG_LEVEL_TRACE, format, argArray);
}
/** Log a message of level TRACE, including an exception. */
public void trace(String msg, Throwable t) {
log(LOG_LEVEL_TRACE, msg, t);
}
/** Are {@code debug} messages currently enabled? */
public boolean isDebugEnabled() {
return isLevelEnabled(LOG_LEVEL_DEBUG);
}
/**
* A simple implementation which logs messages of level DEBUG according to
* the format outlined above.
*/
public void debug(String msg) {
log(LOG_LEVEL_DEBUG, msg, null);
}
/**
* Perform single parameter substitution before logging the message of level
* DEBUG according to the format outlined above.
*/
public void debug(String format, Object param1) {
formatAndLog(LOG_LEVEL_DEBUG, format, param1, null);
}
/**
* Perform double parameter substitution before logging the message of level
* DEBUG according to the format outlined above.
*/
public void debug(String format, Object param1, Object param2) {
formatAndLog(LOG_LEVEL_DEBUG, format, param1, param2);
}
/**
* Perform double parameter substitution before logging the message of level
* DEBUG according to the format outlined above.
*/
public void debug(String format, Object... argArray) {
formatAndLog(LOG_LEVEL_DEBUG, format, argArray);
}
/** Log a message of level DEBUG, including an exception. */
public void debug(String msg, Throwable t) {
log(LOG_LEVEL_DEBUG, msg, t);
}
/** Are {@code info} messages currently enabled? */
public boolean isInfoEnabled() {
return isLevelEnabled(LOG_LEVEL_INFO);
}
/**
* A simple implementation which logs messages of level INFO according to
* the format outlined above.
*/
public void info(String msg) {
log(LOG_LEVEL_INFO, msg, null);
}
/**
* Perform single parameter substitution before logging the message of level
* INFO according to the format outlined above.
*/
public void info(String format, Object arg) {
formatAndLog(LOG_LEVEL_INFO, format, arg, null);
}
/**
* Perform double parameter substitution before logging the message of level
* INFO according to the format outlined above.
*/
public void info(String format, Object arg1, Object arg2) {
formatAndLog(LOG_LEVEL_INFO, format, arg1, arg2);
}
/**
* Perform double parameter substitution before logging the message of level
* INFO according to the format outlined above.
*/
public void info(String format, Object... argArray) {
formatAndLog(LOG_LEVEL_INFO, format, argArray);
}
/** Log a message of level INFO, including an exception. */
public void info(String msg, Throwable t) {
log(LOG_LEVEL_INFO, msg, t);
}
/** Are {@code warn} messages currently enabled? */
public boolean isWarnEnabled() {
return isLevelEnabled(LOG_LEVEL_WARN);
}
/**
* A simple implementation which always logs messages of level WARN
* according to the format outlined above.
*/
public void warn(String msg) {
log(LOG_LEVEL_WARN, msg, null);
}
/**
* Perform single parameter substitution before logging the message of level
* WARN according to the format outlined above.
*/
public void warn(String format, Object arg) {
formatAndLog(LOG_LEVEL_WARN, format, arg, null);
}
/**
* Perform double parameter substitution before logging the message of level
* WARN according to the format outlined above.
*/
public void warn(String format, Object arg1, Object arg2) {
formatAndLog(LOG_LEVEL_WARN, format, arg1, arg2);
}
/**
* Perform double parameter substitution before logging the message of level
* WARN according to the format outlined above.
*/
public void warn(String format, Object... argArray) {
formatAndLog(LOG_LEVEL_WARN, format, argArray);
}
/** Log a message of level WARN, including an exception. */
public void warn(String msg, Throwable t) {
log(LOG_LEVEL_WARN, msg, t);
}
/** Are {@code error} messages currently enabled? */
public boolean isErrorEnabled() {
return isLevelEnabled(LOG_LEVEL_ERROR);
}
/**
* A simple implementation which always logs messages of level ERROR
* according to the format outlined above.
*/
public void error(String msg) {
log(LOG_LEVEL_ERROR, msg, null);
}
/**
* Perform single parameter substitution before logging the message of level
* ERROR according to the format outlined above.
*/
public void error(String format, Object arg) {
formatAndLog(LOG_LEVEL_ERROR, format, arg, null);
}
/**
* Perform double parameter substitution before logging the message of level
* ERROR according to the format outlined above.
*/
public void error(String format, Object arg1, Object arg2) {
formatAndLog(LOG_LEVEL_ERROR, format, arg1, arg2);
}
/**
* Perform double parameter substitution before logging the message of level
* ERROR according to the format outlined above.
*/
public void error(String format, Object... argArray) {
formatAndLog(LOG_LEVEL_ERROR, format, argArray);
}
/** Log a message of level ERROR, including an exception. */
public void error(String msg, Throwable t) {
log(LOG_LEVEL_ERROR, msg, t);
}
public void log(LoggingEvent event) {
int levelInt = event.getLevel().toInt();
if (!isLevelEnabled(levelInt)) {
return;
}
FormattingTuple tp =
MessageFormatter.arrayFormat(event.getMessage(), event.getArgumentArray(), event.getThrowable());
log(levelInt, tp.getMessage(), event.getThrowable());
}
}