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

org.refcodes.logger.ext.slf4j.RuntimeLoggerAdapter Maven / Gradle / Ivy

The newest version!
// /////////////////////////////////////////////////////////////////////////////
// REFCODES.ORG
// =============================================================================
// This code is copyright (c) by Siegfried Steiner, Munich, Germany, distributed
// on an "AS IS" BASIS WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, and licen-
// sed under the following (see "http://en.wikipedia.org/wiki/Multi-licensing")
// licenses:
// =============================================================================
// GNU General Public License, v3.0 ("http://www.gnu.org/licenses/gpl-3.0.html")
// together with the GPL linking exception applied; as being applied by the GNU
// Classpath ("http://www.gnu.org/software/classpath/license.html")
// =============================================================================
// Apache License, v2.0 ("http://www.apache.org/licenses/TEXT-2.0")
// =============================================================================
// Please contact the copyright holding author(s) of the software artifacts in
// question for licensing issues not being covered by the above listed licenses,
// also regarding commercial licensing models or regarding the compatibility
// with other open source licenses.
// /////////////////////////////////////////////////////////////////////////////
package org.refcodes.logger.ext.slf4j;

import org.refcodes.exception.UnhandledEnumBugException;
import org.refcodes.exception.UnhandledValueBugException;
import org.refcodes.logger.LogPriority;
import org.refcodes.logger.RuntimeLogger;
import org.refcodes.logger.RuntimeLoggerAccessor;
import org.slf4j.Logger;
import org.slf4j.Marker;
import org.slf4j.event.Level;
import org.slf4j.helpers.LegacyAbstractLogger;
import org.slf4j.spi.LocationAwareLogger;

/**
 * A tailored wrapper for the {@link RuntimeLogger} in conformity with the
 * {@link Logger} interface.
 */
public final class RuntimeLoggerAdapter extends LegacyAbstractLogger implements LocationAwareLogger, RuntimeLoggerAccessor {

	private static final long serialVersionUID = 1L;

	// /////////////////////////////////////////////////////////////////////////
	// CONSTANTS:
	// /////////////////////////////////////////////////////////////////////////

	// final static String FQCN = RuntimeLoggerAdapter.class.getName();

	// /////////////////////////////////////////////////////////////////////////
	// VARIABLES:
	// /////////////////////////////////////////////////////////////////////////

	final transient RuntimeLogger logger;

	// /////////////////////////////////////////////////////////////////////////
	// CONSTRUCTORS:
	// /////////////////////////////////////////////////////////////////////////

	/**
	 * Instantiates a new runtime logger adapter.
	 *
	 * @param logger the logger
	 */
	public RuntimeLoggerAdapter( RuntimeLogger logger ) {
		this.logger = logger;
		this.name = logger.getName();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean isTraceEnabled() {
		return logger.isLogTrace();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void trace( String msg ) {
		logger.trace( msg );
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void trace( String format, Object arg ) {
		logger.trace( format, arg );
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void trace( String format, Object arg1, Object arg2 ) {
		logger.trace( format, arg1, arg2 );
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void trace( String format, Object... args ) {
		logger.trace( format, args );
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void trace( String msg, Throwable t ) {
		logger.trace( msg, t );
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean isDebugEnabled() {
		return logger.isLogDebug();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void debug( String msg ) {
		logger.debug( msg );
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void debug( String format, Object arg ) {
		logger.debug( format, arg );
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void debug( String format, Object arg1, Object arg2 ) {
		logger.debug( format, arg1, arg2 );
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void debug( String format, Object... args ) {
		logger.debug( format, args );
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void debug( String msg, Throwable t ) {
		logger.debug( msg, t );
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean isInfoEnabled() {
		return logger.isLogInfo();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void info( String msg ) {
		logger.info( msg );
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void info( String format, Object arg ) {
		logger.info( format, arg );
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void info( String format, Object arg1, Object arg2 ) {
		logger.info( format, arg1, arg2 );
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void info( String format, Object... args ) {
		logger.info( format, args );
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void info( String msg, Throwable t ) {
		logger.info( msg, t );
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean isWarnEnabled() {
		return logger.isLogWarn();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void warn( String msg ) {
		logger.warn( msg );
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void warn( String format, Object arg ) {
		logger.warn( format, arg );
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void warn( String format, Object arg1, Object arg2 ) {
		logger.warn( format, arg1, arg2 );
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void warn( String format, Object... args ) {
		logger.warn( format, args );
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void warn( String msg, Throwable t ) {
		logger.warn( msg, t );
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public boolean isErrorEnabled() {
		return logger.isLogError();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void error( String msg ) {
		logger.error( msg );
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void error( String format, Object arg ) {
		logger.error( format, arg );
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void error( String format, Object arg1, Object arg2 ) {
		logger.error( format, arg1, arg2 );
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void error( String format, Object... args ) {
		logger.error( format, args );
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void error( String msg, Throwable t ) {
		logger.error( msg, t );
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void log( Marker aMarker, String aCallerFQCN, int aLevel, String aMessage, Object[] aArgs, Throwable aThrowable ) {
		final LogPriority thePriority;
		switch ( aLevel ) {
		case LocationAwareLogger.TRACE_INT -> thePriority = LogPriority.TRACE;
		case LocationAwareLogger.DEBUG_INT -> thePriority = LogPriority.DEBUG;
		case LocationAwareLogger.INFO_INT -> thePriority = LogPriority.INFO;
		case LocationAwareLogger.WARN_INT -> thePriority = LogPriority.WARN;
		case LocationAwareLogger.ERROR_INT -> thePriority = LogPriority.ERROR;
		default -> throw new UnhandledValueBugException( aLevel );
		}
		logger.log( thePriority, aMessage, aArgs, aThrowable );
	}

	/**
	 * In case the Slf4jRuntimeLogger (as created by the
	 * Slf4jRuntimeLoggerFactorySingleton) detects that SLF4J has
	 * bound a {@link RuntimeLoggerAdapter} (i.e. the REFCODES.ORG SLF4J
	 * binding), it directly delegates its method calls to the wrapped
	 * {@link RuntimeLogger} instead of marshaling a log request through this
	 * {@link RuntimeLoggerAdapter}; as marshaling would mean consolidating of
	 * various detailed {@link LogPriority} levels to a single SLF4J log level.
	 * 
	 * @return The wrapped {@link RuntimeLogger} used by the
	 *         Slf4jRuntimeLogger when possible.
	 */
	@Override
	public RuntimeLogger getRuntimeLogger() {
		return logger;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected String getFullyQualifiedCallerName() {
		return logger.getName();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void handleNormalizedLoggingCall( Level aLevel, Marker aMarker, String aMsg, Object[] aArguments, Throwable aThrowable ) {
		final LogPriority thePriority;
		switch ( aLevel ) {
		case DEBUG -> thePriority = LogPriority.DEBUG;
		case ERROR -> thePriority = LogPriority.ERROR;
		case INFO -> thePriority = LogPriority.INFO;
		case TRACE -> thePriority = LogPriority.TRACE;
		case WARN -> thePriority = LogPriority.WARN;
		default -> throw new UnhandledEnumBugException( aLevel );
		}
		logger.log( thePriority, aMsg, aThrowable, aArguments );
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy