CentralSlf4jLogger.java
/*
** Module : CentralSlf4jLogger.java
** Abstract : Implementation of slf4j logger that proxies to CentralLogger.
**
** Copyright (c) 2023-2024, Golden Code Development Corporation.
**
** -#- -I- --Date-- ----------------------------Description-----------------------------
** 001 GBB 20230327 Initial setup
** 002 GBB 20240422 Implementation of api 2.0
*/
/*
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
**
** You may find a copy of the GNU Affero GPL version 3 at the following
** location: https://www.gnu.org/licenses/agpl-3.0.en.html
**
** Additional terms under GNU Affero GPL version 3 section 7:
**
** Under Section 7 of the GNU Affero GPL version 3, the following additional
** terms apply to the works covered under the License. These additional terms
** are non-permissive additional terms allowed under Section 7 of the GNU
** Affero GPL version 3 and may not be removed by you.
**
** 0. Attribution Requirement.
**
** You must preserve all legal notices or author attributions in the covered
** work or Appropriate Legal Notices displayed by works containing the covered
** work. You may not remove from the covered work any author or developer
** credit already included within the covered work.
**
** 1. No License To Use Trademarks.
**
** This license does not grant any license or rights to use the trademarks
** Golden Code, FWD, any Golden Code or FWD logo, or any other trademarks
** of Golden Code Development Corporation. You are not authorized to use the
** name Golden Code, FWD, or the names of any author or contributor, for
** publicity purposes without written authorization.
**
** 2. No Misrepresentation of Affiliation.
**
** You may not represent yourself as Golden Code Development Corporation or FWD.
**
** You may not represent yourself for publicity purposes as associated with
** Golden Code Development Corporation, FWD, or any author or contributor to
** the covered work, without written authorization.
**
** 3. No Misrepresentation of Source or Origin.
**
** You may not represent the covered work as solely your work. All modified
** versions of the covered work must be marked in a reasonable way to make it
** clear that the modified work is not originating from Golden Code Development
** Corporation or FWD. All modified versions must contain the notices of
** attribution required in this license.
*/
package com.goldencode.p2j.util.logging;
import org.slf4j.*;
import org.slf4j.helpers.*;
import org.slf4j.spi.*;
import java.util.*;
import java.util.logging.*;
/**
* Implementation of slf4j logger that proxies to CentralLogger.
* <p>
* Slf4j levels correspond to java.util.logging.Level in this implementation as follows:
* <table>
* <tr>
* <td>Trace</td> <td>FINER</td>
* </tr>
* <tr>
* <td>Debug</td> <td>FINE</td>
* </tr>
* <tr>
* <td>Info</td> <td>INFO</td>
* </tr>
* <tr>
* <td>Warn</td> <td>WARNING</td>
* </tr>
* <tr>
* <td>Error</td> <td>SEVERE</td>
* </tr>
* </table>
*/
public class CentralSlf4jLogger
extends LegacyAbstractLogger
implements LocationAwareLogger
{
/** Map of slf4j levels to java logging levels. */
private static final Map<org.slf4j.event.Level, java.util.logging.Level> slf4jToJavaLevelMap =
new HashMap<org.slf4j.event.Level, java.util.logging.Level>() {{
put(org.slf4j.event.Level.ERROR, Level.SEVERE);
put(org.slf4j.event.Level.WARN, Level.WARNING);
put(org.slf4j.event.Level.INFO, Level.INFO);
put(org.slf4j.event.Level.DEBUG, Level.FINE);
put(org.slf4j.event.Level.TRACE, Level.FINER);
}};
/** The instance of CentralLogger to forward logs to. */
private final CentralLogger logger;
/**
* Package-private constructor for CentralSlf4jLogger.
*
* @param name
* The logger name.
*/
CentralSlf4jLogger(String name)
{
logger = CentralLogger.get(name);
}
/**
* Getter for the logger name.
*
* @return The logger name.
*/
@Override
public String getName()
{
return logger.getLoggerName();
}
/**
* Returns if the slf4j trace level is loggable.
*
* @return <code>true</code> if slf4j trace level is loggable, <code>false</code> otherwise.
*/
@Override
public boolean isTraceEnabled()
{
return logger.isLoggable(Level.FINER);
}
/**
* Logs a message with slf4j level trace.
*
* @param msg
* The message.
*/
@Override
public void trace(String msg)
{
logger.log(Level.FINER, msg);
}
/**
* Logs a message with slf4j level trace.
*
* @param format
* The msg format.
* @param arg
* The formatting argument.
*/
@Override
public void trace(String format, Object arg)
{
logger.log(Level.FINER, format(format, arg));
}
/**
* Logs a message with slf4j level trace.
*
* @param format
* The msg format.
* @param arg1
* The first formatting argument.
* @param arg2
* The second formatting argument.
*/
@Override
public void trace(String format, Object arg1, Object arg2)
{
logger.log(Level.FINER, format(format, arg1, arg2));
}
/**
* Logs a message with slf4j level trace.
*
* @param format
* The msg format.
* @param arguments
* The formatting arguments.
*/
@Override
public void trace(String format, Object... arguments)
{
logger.log(Level.FINER, format(format, arguments));
}
/**
* Logs a message with slf4j level trace.
*
* @param msg
* The message.
* @param t
* Throwable.
*/
@Override
public void trace(String msg, Throwable t)
{
logger.log(Level.FINER, msg, t);
}
/**
* Returns if the slf4j trace level is loggable.
*
* @return <code>true</code> if slf4j trace level is loggable, <code>false</code> otherwise.
*/
@Override
public boolean isTraceEnabled(Marker marker)
{
return logger.isLoggable(Level.FINER);
}
/**
* Logs a message with slf4j level trace.
*
* @param marker
* slf4j named object enriching the context of the log. Ignored.
* @param msg
* The message.
*/
@Override
public void trace(Marker marker, String msg)
{
trace(msg);
}
/**
* Logs a message with slf4j level trace.
*
* @param marker
* slf4j named object enriching the context of the log. Ignored.
* @param format
* The msg format.
* @param arg
* The formatting argument.
*/
@Override
public void trace(Marker marker, String format, Object arg)
{
trace(format(format, arg));
}
/**
* Logs a message with slf4j level trace.
*
* @param marker
* slf4j named object enriching the context of the log. Ignored.
* @param format
* The msg format.
* @param arg1
* The first formatting argument.
* @param arg2
* The second formatting argument.
*/
@Override
public void trace(Marker marker, String format, Object arg1, Object arg2)
{
trace(format(format, arg1, arg2));
}
/**
* Logs a message with slf4j level trace.
*
* @param marker
* slf4j named object enriching the context of the log. Ignored.
* @param format
* The msg format.
* @param arguments
* The first formatting arguments.
*/
@Override
public void trace(Marker marker, String format, Object... arguments)
{
trace(format(format, arguments));
}
/**
* Logs a message with slf4j level trace.
*
* @param marker
* slf4j named object enriching the context of the log. Ignored.
* @param msg
* The message.
* @param t
* Throwable.
*/
@Override
public void trace(Marker marker, String msg, Throwable t)
{
trace(msg, t);
}
/**
* Returns if the slf4j debug level is loggable.
*
* @return <code>true</code> if slf4j debug level is loggable, <code>false</code> otherwise.
*/
@Override
public boolean isDebugEnabled()
{
return logger.isLoggable(Level.FINE);
}
/**
* Logs a message with slf4j level debug.
*
* @param msg
* The message.
*/
@Override
public void debug(String msg)
{
logger.log(Level.FINE, msg);
}
/**
* Logs a message with slf4j level debug.
*
* @param format
* The msg format.
* @param arg
* The formatting argument.
*/
@Override
public void debug(String format, Object arg)
{
logger.log(Level.FINE, format(format, arg));
}
/**
* Logs a message with slf4j level debug.
*
* @param format
* The msg format.
* @param arg1
* The first formatting argument.
* @param arg2
* The second formatting argument.
*/
@Override
public void debug(String format, Object arg1, Object arg2)
{
logger.log(Level.FINE, format(format, arg1, arg2));
}
/**
* Logs a message with slf4j level debug.
*
* @param format
* The msg format.
* @param arguments
* The formatting arguments.
*/
@Override
public void debug(String format, Object... arguments)
{
logger.log(Level.FINE, format(format, arguments));
}
/**
* Logs a message with slf4j level debug.
*
* @param msg
* The message.
* @param t
* Throwable.
*/
@Override
public void debug(String msg, Throwable t)
{
logger.log(Level.FINE, msg, t);
}
/**
* Returns if the slf4j debug level is loggable.
*
* @return <code>true</code> if slf4j debug level is loggable, <code>false</code> otherwise.
*/
@Override
public boolean isDebugEnabled(Marker marker)
{
return logger.isLoggable(Level.FINE);
}
/**
* Logs a message with slf4j level debug.
*
* @param marker
* slf4j named object enriching the context of the log. Ignored.
* @param msg
* The message.
*/
@Override
public void debug(Marker marker, String msg)
{
debug(msg);
}
/**
* Logs a message with slf4j level debug.
*
* @param marker
* slf4j named object enriching the context of the log. Ignored.
* @param format
* The msg format.
* @param arg
* The formatting argument.
*/
@Override
public void debug(Marker marker, String format, Object arg)
{
debug(format(format, arg));
}
/**
* Logs a message with slf4j level debug.
*
* @param marker
* slf4j named object enriching the context of the log. Ignored.
* @param format
* The msg format.
* @param arg1
* The first formatting argument.
* @param arg2
* The second formatting argument.
*/
@Override
public void debug(Marker marker, String format, Object arg1, Object arg2)
{
debug(format(format, arg1, arg2));
}
/**
* Logs a message with slf4j level debug.
*
* @param marker
* slf4j named object enriching the context of the log. Ignored.
* @param format
* The msg format.
* @param arguments
* The first formatting arguments.
*/
@Override
public void debug(Marker marker, String format, Object... arguments)
{
debug(format(format, arguments));
}
/**
* Logs a message with slf4j level debug.
*
* @param marker
* slf4j named object enriching the context of the log. Ignored.
* @param msg
* The message.
* @param t
* Throwable.
*/
@Override
public void debug(Marker marker, String msg, Throwable t)
{
debug(msg, t);
}
/**
* Returns if the slf4j info level is loggable.
*
* @return <code>true</code> if slf4j info level is loggable, <code>false</code> otherwise.
*/
@Override
public boolean isInfoEnabled()
{
return logger.isLoggable(Level.INFO);
}
/**
* Logs a message with slf4j level info.
*
* @param msg
* The message.
*/
@Override
public void info(String msg)
{
logger.log(Level.INFO, msg);
}
/**
* Logs a message with slf4j level info.
*
* @param format
* The msg format.
* @param arg
* The formatting argument.
*/
@Override
public void info(String format, Object arg)
{
logger.log(Level.INFO, format(format, arg));
}
/**
* Logs a message with slf4j level info.
*
* @param format
* The msg format.
* @param arg1
* The first formatting argument.
* @param arg2
* The second formatting argument.
*/
@Override
public void info(String format, Object arg1, Object arg2)
{
logger.log(Level.INFO, format(format, arg1, arg2));
}
/**
* Logs a message with slf4j level info.
*
* @param format
* The msg format.
* @param arguments
* The formatting arguments.
*/
@Override
public void info(String format, Object... arguments)
{
logger.log(Level.INFO, format(format, arguments));
}
/**
* Logs a message with slf4j level info.
*
* @param msg
* The message.
* @param t
* Throwable.
*/
@Override
public void info(String msg, Throwable t)
{
logger.log(Level.INFO, msg, t);
}
/**
* Returns if the slf4j info level is loggable.
*
* @return <code>true</code> if slf4j info level is loggable, <code>false</code> otherwise.
*/
@Override
public boolean isInfoEnabled(Marker marker)
{
return logger.isLoggable(Level.INFO);
}
/**
* Logs a message with slf4j level info.
*
* @param marker
* slf4j named object enriching the context of the log. Ignored.
* @param msg
* The message.
*/
@Override
public void info(Marker marker, String msg)
{
info(msg);
}
/**
* Logs a message with slf4j level info.
*
* @param marker
* slf4j named object enriching the context of the log. Ignored.
* @param format
* The msg format.
* @param arg
* The formatting argument.
*/
@Override
public void info(Marker marker, String format, Object arg)
{
info(format(format, arg));
}
/**
* Logs a message with slf4j level info.
*
* @param marker
* slf4j named object enriching the context of the log. Ignored.
* @param format
* The msg format.
* @param arg1
* The first formatting argument.
* @param arg2
* The second formatting argument.
*/
@Override
public void info(Marker marker, String format, Object arg1, Object arg2)
{
info(format(format, arg1, arg2));
}
/**
* Logs a message with slf4j level info.
*
* @param marker
* slf4j named object enriching the context of the log. Ignored.
* @param format
* The msg format.
* @param arguments
* The first formatting arguments.
*/
@Override
public void info(Marker marker, String format, Object... arguments)
{
info(format(format, arguments));
}
/**
* Logs a message with slf4j level info.
*
* @param marker
* slf4j named object enriching the context of the log. Ignored.
* @param msg
* The message.
* @param t
* Throwable.
*/
@Override
public void info(Marker marker, String msg, Throwable t)
{
info(msg, t);
}
/**
* Returns if the slf4j warn level is loggable.
*
* @return <code>true</code> if slf4j warn level is loggable, <code>false</code> otherwise.
*/
@Override
public boolean isWarnEnabled()
{
return logger.isLoggable(Level.WARNING);
}
/**
* Logs a message with slf4j level warn.
*
* @param msg
* The message.
*/
@Override
public void warn(String msg)
{
logger.log(Level.WARNING, msg);
}
/**
* Logs a message with slf4j level warn.
*
* @param format
* The msg format.
* @param arg
* The formatting argument.
*/
@Override
public void warn(String format, Object arg)
{
logger.log(Level.WARNING, format(format, arg));
}
/**
* Logs a message with slf4j level warn.
*
* @param format
* The msg format.
* @param arguments
* The formatting arguments.
*/
@Override
public void warn(String format, Object... arguments)
{
logger.log(Level.WARNING, format(format, arguments));
}
/**
* Logs a message with slf4j level warn.
*
* @param format
* The msg format.
* @param arg1
* The first formatting argument.
* @param arg2
* The second formatting argument.
*/
@Override
public void warn(String format, Object arg1, Object arg2)
{
logger.log(Level.WARNING, format(format, arg1, arg2));
}
/**
* Logs a message with slf4j level warn.
*
* @param msg
* The message.
* @param t
* Throwable.
*/
@Override
public void warn(String msg, Throwable t)
{
logger.log(Level.WARNING, msg, t);
}
/**
* Returns if the slf4j warn level is loggable.
*
* @return <code>true</code> if slf4j warn level is loggable, <code>false</code> otherwise.
*/
@Override
public boolean isWarnEnabled(Marker marker)
{
return logger.isLoggable(Level.WARNING);
}
/**
* Logs a message with slf4j level warn.
*
* @param marker
* slf4j named object enriching the context of the log. Ignored.
* @param msg
* The message.
*/
@Override
public void warn(Marker marker, String msg)
{
warn(msg);
}
/**
* Logs a message with slf4j level warn.
*
* @param marker
* slf4j named object enriching the context of the log. Ignored.
* @param format
* The msg format.
* @param arg
* The formatting argument.
*/
@Override
public void warn(Marker marker, String format, Object arg)
{
warn(format(format, arg));
}
/**
* Logs a message with slf4j level warn.
*
* @param marker
* slf4j named object enriching the context of the log. Ignored.
* @param format
* The msg format.
* @param arg1
* The first formatting argument.
* @param arg2
* The second formatting argument.
*/
@Override
public void warn(Marker marker, String format, Object arg1, Object arg2)
{
warn(format(format, arg1, arg2));
}
/**
* Logs a message with slf4j level warn.
*
* @param marker
* slf4j named object enriching the context of the log. Ignored.
* @param format
* The msg format.
* @param arguments
* The first formatting arguments.
*/
@Override
public void warn(Marker marker, String format, Object... arguments)
{
warn(format(format, arguments));
}
/**
* Logs a message with slf4j level warn.
*
* @param marker
* slf4j named object enriching the context of the log. Ignored.
* @param msg
* The message.
* @param t
* Throwable.
*/
@Override
public void warn(Marker marker, String msg, Throwable t)
{
warn(msg, t);
}
/**
* Returns if the slf4j error level is loggable.
*
* @return <code>true</code> if slf4j error level is loggable, <code>false</code> otherwise.
*/
@Override
public boolean isErrorEnabled()
{
return logger.isLoggable(Level.SEVERE);
}
/**
* Logs a message with slf4j level error.
*
* @param msg
* The message.
*/
@Override
public void error(String msg)
{
logger.log(Level.SEVERE, msg);
}
/**
* Logs a message with slf4j level error.
*
* @param format
* The msg format.
* @param arg
* The formatting argument.
*/
@Override
public void error(String format, Object arg)
{
logger.log(Level.SEVERE, format(format, arg));
}
/**
* Logs a message with slf4j level error.
*
* @param format
* The msg format.
* @param arg1
* The first formatting argument.
* @param arg2
* The second formatting argument.
*/
@Override
public void error(String format, Object arg1, Object arg2)
{
logger.log(Level.SEVERE, format(format, arg1, arg2));
}
/**
* Logs a message with slf4j level error.
*
* @param format
* The msg format.
* @param arguments
* The formatting arguments.
*/
@Override
public void error(String format, Object... arguments)
{
logger.log(Level.SEVERE, format(format, arguments));
}
/**
* Logs a message with slf4j level error.
*
* @param msg
* The message.
* @param t
* Throwable.
*/
@Override
public void error(String msg, Throwable t)
{
logger.log(Level.SEVERE, msg, t);
}
/**
* Returns if the slf4j error level is loggable.
*
* @return <code>true</code> if slf4j error level is loggable, <code>false</code> otherwise.
*/
@Override
public boolean isErrorEnabled(Marker marker)
{
return logger.isLoggable(Level.SEVERE);
}
/**
* Logs a message with slf4j level error.
*
* @param marker
* slf4j named object enriching the context of the log. Ignored.
* @param msg
* The message.
*/
@Override
public void error(Marker marker, String msg)
{
error(msg);
}
/**
* Logs a message with slf4j level error.
*
* @param marker
* slf4j named object enriching the context of the log. Ignored.
* @param format
* The msg format.
* @param arg
* The formatting argument.
*/
@Override
public void error(Marker marker, String format, Object arg)
{
error(format(format, arg));
}
/**
* Logs a message with slf4j level error.
*
* @param marker
* slf4j named object enriching the context of the log. Ignored.
* @param format
* The msg format.
* @param arg1
* The first formatting argument.
* @param arg2
* The second formatting argument.
*/
@Override
public void error(Marker marker, String format, Object arg1, Object arg2)
{
error(format(format, arg1, arg2));
}
/**
* Logs a message with slf4j level error.
*
* @param marker
* slf4j named object enriching the context of the log. Ignored.
* @param format
* The msg format.
* @param arguments
* The first formatting arguments.
*/
@Override
public void error(Marker marker, String format, Object... arguments)
{
error(format(format, arguments));
}
/**
* Logs a message with slf4j level error.
*
* @param marker
* slf4j named object enriching the context of the log. Ignored.
* @param msg
* The message.
* @param t
* Throwable.
*/
@Override
public void error(Marker marker, String msg, Throwable t)
{
error(msg, t);
}
/**
* Printing method with support for location information.
*
* @param marker
* The marker to be used for this event, may be null.
* @param fqcn
* The fully qualified class name of the logger instance, typically the logger class.
* @param slf4jLevelInt
* One of the level integers defined in this interface.
* @param format
* The message for the log event.
* @param args
* The arguments for the message format.
* @param t
* Throwable associated with the log event, may be null.
*/
@Override
public void log(Marker marker, String fqcn, int slf4jLevelInt, String format, Object[] args, Throwable t)
{
handleNormalizedLoggingCall(org.slf4j.event.Level.intToLevel(slf4jLevelInt), marker, format, args, t);
}
/**
* Unknown. No slf4j doc, but from examples it seems to be the slf4j implementation class name.
*
* @return See above.
*/
@Override
protected String getFullyQualifiedCallerName()
{
return CentralSlf4jLogger.class.getName();
}
/**
* Given various arguments passed as parameters, perform actual logging. This method assumes that the
* separation of the args array into actual objects and a throwable has been already operated.
*
* @param slf4jLevel
* the SLF4J level for this event
* @param marker
* The marker to be used for this event, may be null.
* @param format
* The message pattern which will be parsed and formatted
* @param args
* The array of arguments to be formatted, may be null
* @param t
* The exception whose stack trace should be logged, may be null
*/
@Override
protected void handleNormalizedLoggingCall(org.slf4j.event.Level slf4jLevel,
Marker marker,
String format,
Object[] args,
Throwable t)
{
Level logLevel = slf4jToJavaLevelMap.get(slf4jLevel);
if (logLevel == null)
{
throw new IllegalStateException("Unexpected value: " + slf4jLevel);
}
logger.log(logLevel, format(format, args), t);
}
/**
* Returns the message formatted according to org.eclipse.jetty.util.log.JettyAwareLogger - an slf4j
* formatting method compatible with Jetty's logs.
*
* @param format
* The message format in slf4j style.
* @param args
* The formatting arguments.
*
* @return The message format in CentralLogger style.
*/
private String format(String format, Object... args)
{
return MessageFormatter.arrayFormat(format, args).getMessage();
}
}