SLF4J 2 provider that bridges SLF4J 2 logging calls to the Apache Log4j API
—
Core SLF4J logging functionality providing all standard log levels with parameterized message support and automatic formatting.
Get logger instances using the standard SLF4J LoggerFactory.
/**
* Get a logger instance for the specified class
* @param clazz The class for which to create a logger
* @return Logger instance
*/
Logger getLogger(Class<?> clazz);
/**
* Get a logger instance for the specified name
* @param name The logger name
* @return Logger instance
*/
Logger getLogger(String name);Usage Example:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class MyService {
// Class-based logger (recommended)
private static final Logger logger = LoggerFactory.getLogger(MyService.class);
// Name-based logger
private static final Logger namedLogger = LoggerFactory.getLogger("com.example.custom");
}Finest granularity logging for detailed troubleshooting information.
/**
* Log a message at TRACE level
* @param msg The message string
*/
void trace(String msg);
/**
* Log a message at TRACE level with one parameter
* @param format The format string
* @param arg The argument
*/
void trace(String format, Object arg);
/**
* Log a message at TRACE level with two parameters
* @param format The format string
* @param arg1 The first argument
* @param arg2 The second argument
*/
void trace(String format, Object arg1, Object arg2);
/**
* Log a message at TRACE level with multiple parameters
* @param format The format string
* @param arguments The arguments
*/
void trace(String format, Object... arguments);
/**
* Log a message at TRACE level with an exception
* @param msg The message string
* @param t The exception
*/
void trace(String msg, Throwable t);
/**
* Check if TRACE level is enabled
* @return true if TRACE level is enabled
*/
boolean isTraceEnabled();Usage Examples:
// Simple message
logger.trace("Entering method processData()");
// Parameterized message
logger.trace("Processing item {} of {}", currentIndex, totalItems);
// Multiple parameters
logger.trace("User {} accessing resource {} with permissions {}", userId, resourceId, permissions);
// With exception
try {
// some operation
} catch (Exception e) {
logger.trace("Failed to process data", e);
}
// Conditional logging
if (logger.isTraceEnabled()) {
logger.trace("Expensive operation result: {}", computeExpensiveValue());
}Diagnostic information for development and troubleshooting.
void debug(String msg);
void debug(String format, Object arg);
void debug(String format, Object arg1, Object arg2);
void debug(String format, Object... arguments);
void debug(String msg, Throwable t);
boolean isDebugEnabled();Usage Examples:
logger.debug("Database connection established");
logger.debug("Query executed in {} ms", executionTime);
logger.debug("Cache hit ratio: {}, total requests: {}", hitRatio, totalRequests);General informational messages about application flow.
void info(String msg);
void info(String format, Object arg);
void info(String format, Object arg1, Object arg2);
void info(String format, Object... arguments);
void info(String msg, Throwable t);
boolean isInfoEnabled();Usage Examples:
logger.info("Application started successfully");
logger.info("User {} logged in from {}", username, ipAddress);
logger.info("Processed {} records in {} seconds", recordCount, duration);Warning messages for potentially problematic situations.
void warn(String msg);
void warn(String format, Object arg);
void warn(String format, Object arg1, Object arg2);
void warn(String format, Object... arguments);
void warn(String msg, Throwable t);
boolean isWarnEnabled();Usage Examples:
logger.warn("Configuration file not found, using defaults");
logger.warn("High memory usage detected: {}%", memoryUsage);
logger.warn("Deprecated API call from {}", callerClass);Error conditions that may still allow the application to continue running.
void error(String msg);
void error(String format, Object arg);
void error(String format, Object arg1, Object arg2);
void error(String format, Object... arguments);
void error(String msg, Throwable t);
boolean isErrorEnabled();Usage Examples:
logger.error("Failed to save user data");
logger.error("Database connection failed after {} attempts", retryCount);
logger.error("Critical error in payment processing", exception);Advanced logging method for frameworks requiring precise caller location information.
/**
* Log with location awareness (LocationAwareLogger interface)
* @param marker The marker (can be null)
* @param fqcn Fully qualified class name of the caller
* @param level The log level as integer
* @param message The message
* @param params Message parameters
* @param throwable Exception (can be null)
*/
void log(Marker marker, String fqcn, int level, String message, Object[] params, Throwable throwable);Usage Example:
import org.slf4j.spi.LocationAwareLogger;
LocationAwareLogger locationLogger = (LocationAwareLogger) logger;
locationLogger.log(null, MyClass.class.getName(),
LocationAwareLogger.INFO_INT, "Custom location message", null, null);Access logger metadata and configuration.
/**
* Get the logger name
* @return The logger name
*/
String getName();
/**
* Check if a specific level is enabled
* @param level The SLF4J level to check
* @return true if the level is enabled
*/
boolean isEnabledForLevel(org.slf4j.event.Level level);Usage Examples:
String loggerName = logger.getName();
logger.info("Logger name: {}", loggerName);
// Check specific level
if (logger.isEnabledForLevel(org.slf4j.event.Level.DEBUG)) {
// Perform debug-specific operations
}All logging methods support SLF4J's parameterized message format using {} placeholders:
// Single parameter
logger.info("User {} logged in", username);
// Multiple parameters
logger.info("Transfer of ${} from {} to {} completed", amount, fromAccount, toAccount);
// Arrays are automatically converted
String[] items = {"apple", "banana", "orange"};
logger.debug("Processing items: {}", items);// Efficient
if (logger.isDebugEnabled()) {
logger.debug("Complex calculation result: {}", expensiveCalculation());
}
// Less efficient - calculation always performed
logger.debug("Complex calculation result: {}", expensiveCalculation());Install with Tessl CLI
npx tessl i tessl/maven-org-apache-logging-log4j--log4j-slf4j2-impl