CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-slf4j--slf4j-api

Simple Logging Facade for Java (SLF4J) API - a facade/abstraction layer for various logging frameworks.

Overview
Eval results
Files

basic-logging.mddocs/

Basic Logging

Traditional SLF4J logging interface providing methods for all standard log levels. This API has been stable since SLF4J 1.0 and remains the foundation of SLF4J logging.

Capabilities

Logger Factory

Static factory for obtaining Logger instances by name or class.

/**
 * Static factory class for obtaining Logger instances
 */
public final class LoggerFactory {
    /**
     * Return a logger named according to the name parameter
     * @param name The name of the logger
     * @return logger instance
     */
    public static Logger getLogger(String name);
    
    /**
     * Return a logger named corresponding to the class passed as parameter
     * @param clazz The returned logger will be named after clazz
     * @return logger instance
     */
    public static Logger getLogger(Class<?> clazz);
    
    /**
     * Return the ILoggerFactory instance in use
     * @return the ILoggerFactory instance in use
     */
    public static ILoggerFactory getILoggerFactory();
}

Usage Examples:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MyService {
    // Get logger by class - recommended approach
    private static final Logger logger = LoggerFactory.getLogger(MyService.class);
    
    // Get logger by name
    private static final Logger namedLogger = LoggerFactory.getLogger("com.example.custom");
    
    public void processData() {
        logger.info("Processing started");
        // ... processing logic
        logger.info("Processing completed");
    }
}

Logger Interface

Main logging interface with methods for all log levels.

/**
 * The main user entry point of SLF4J API
 */
public interface Logger {
    /**
     * Return the name of this Logger instance
     * @return name of this logger instance
     */
    String getName();
    
    // Level checking methods
    boolean isTraceEnabled();
    boolean isDebugEnabled();
    boolean isInfoEnabled();
    boolean isWarnEnabled();
    boolean isErrorEnabled();
}

TRACE Level Logging

Finest level of logging, typically used for detailed diagnostic information.

/**
 * Is the logger instance enabled for the TRACE level?
 * @return True if this Logger is enabled for the TRACE level, false otherwise
 */
boolean isTraceEnabled();

/**
 * Log a message at the TRACE level
 * @param msg the message string to be logged
 */
void trace(String msg);

/**
 * Log a message at the TRACE level according to the specified format and argument
 * @param format the format string
 * @param arg the argument
 */
void trace(String format, Object arg);

/**
 * Log a message at the TRACE level according to the specified format and arguments
 * @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 the TRACE level according to the specified format and arguments
 * @param format the format string
 * @param arguments a list of 3 or more arguments
 */
void trace(String format, Object... arguments);

/**
 * Log an exception (throwable) at the TRACE level with an accompanying message
 * @param msg the message accompanying the exception
 * @param t the exception (throwable) to log
 */
void trace(String msg, Throwable t);

DEBUG Level Logging

Detailed information for diagnosing problems, typically used during development.

/**
 * Is the logger instance enabled for the DEBUG level?
 * @return True if this Logger is enabled for the DEBUG level, false otherwise
 */
boolean isDebugEnabled();

/**
 * Log a message at the DEBUG level
 * @param msg the message string to be logged
 */
void debug(String msg);

/**
 * Log a message at the DEBUG level according to the specified format and argument
 * @param format the format string
 * @param arg the argument
 */
void debug(String format, Object arg);

/**
 * Log a message at the DEBUG level according to the specified format and arguments
 * @param format the format string
 * @param arg1 the first argument
 * @param arg2 the second argument
 */
void debug(String format, Object arg1, Object arg2);

/**
 * Log a message at the DEBUG level according to the specified format and arguments
 * @param format the format string
 * @param arguments a list of 3 or more arguments
 */
void debug(String format, Object... arguments);

/**
 * Log an exception (throwable) at the DEBUG level with an accompanying message
 * @param msg the message accompanying the exception
 * @param t the exception (throwable) to log
 */
void debug(String msg, Throwable t);

INFO Level Logging

General informational messages about application flow.

/**
 * Is the logger instance enabled for the INFO level?
 * @return True if this Logger is enabled for the INFO level, false otherwise
 */
boolean isInfoEnabled();

/**
 * Log a message at the INFO level
 * @param msg the message string to be logged
 */
void info(String msg);

/**
 * Log a message at the INFO level according to the specified format and argument
 * @param format the format string
 * @param arg the argument
 */
void info(String format, Object arg);

/**
 * Log a message at the INFO level according to the specified format and arguments
 * @param format the format string
 * @param arg1 the first argument
 * @param arg2 the second argument
 */
void info(String format, Object arg1, Object arg2);

/**
 * Log a message at the INFO level according to the specified format and arguments
 * @param format the format string
 * @param arguments a list of 3 or more arguments
 */
void info(String format, Object... arguments);

/**
 * Log an exception (throwable) at the INFO level with an accompanying message
 * @param msg the message accompanying the exception
 * @param t the exception (throwable) to log
 */
void info(String msg, Throwable t);

WARN Level Logging

Potentially harmful situations or deprecated API usage.

/**
 * Is the logger instance enabled for the WARN level?
 * @return True if this Logger is enabled for the WARN level, false otherwise
 */
boolean isWarnEnabled();

/**
 * Log a message at the WARN level
 * @param msg the message string to be logged
 */
void warn(String msg);

/**
 * Log a message at the WARN level according to the specified format and argument
 * @param format the format string
 * @param arg the argument
 */
void warn(String format, Object arg);

/**
 * Log a message at the WARN level according to the specified format and arguments
 * @param format the format string
 * @param arg1 the first argument
 * @param arg2 the second argument
 */
void warn(String format, Object arg1, Object arg2);

/**
 * Log a message at the WARN level according to the specified format and arguments
 * @param format the format string
 * @param arguments a list of 3 or more arguments
 */
void warn(String format, Object... arguments);

/**
 * Log an exception (throwable) at the WARN level with an accompanying message
 * @param msg the message accompanying the exception
 * @param t the exception (throwable) to log
 */
void warn(String msg, Throwable t);

ERROR Level Logging

Error events that might still allow the application to continue running.

/**
 * Is the logger instance enabled for the ERROR level?
 * @return True if this Logger is enabled for the ERROR level, false otherwise
 */
boolean isErrorEnabled();

/**
 * Log a message at the ERROR level
 * @param msg the message string to be logged
 */
void error(String msg);

/**
 * Log a message at the ERROR level according to the specified format and argument
 * @param format the format string
 * @param arg the argument
 */
void error(String format, Object arg);

/**
 * Log a message at the ERROR level according to the specified format and arguments
 * @param format the format string
 * @param arg1 the first argument
 * @param arg2 the second argument
 */
void error(String format, Object arg1, Object arg2);

/**
 * Log a message at the ERROR level according to the specified format and arguments
 * @param format the format string
 * @param arguments a list of 3 or more arguments
 */
void error(String format, Object... arguments);

/**
 * Log an exception (throwable) at the ERROR level with an accompanying message
 * @param msg the message accompanying the exception
 * @param t the exception (throwable) to log
 */
void error(String msg, Throwable t);

Usage Examples:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class UserService {
    private static final Logger logger = LoggerFactory.getLogger(UserService.class);
    
    public User findUser(String userId) {
        // Check if debug logging is enabled before expensive operations
        if (logger.isDebugEnabled()) {
            logger.debug("Searching for user with ID: {}", userId);
        }
        
        try {
            User user = userRepository.findById(userId);
            if (user == null) {
                logger.warn("User not found: {}", userId);
                return null;
            }
            
            logger.info("User retrieved successfully: {}", user.getName());
            return user;
            
        } catch (DatabaseException e) {
            logger.error("Database error while retrieving user {}", userId, e);
            throw new ServiceException("Failed to retrieve user", e);
        }
    }
    
    public void processUsers(List<String> userIds) {
        logger.info("Processing {} users", userIds.size());
        
        for (int i = 0; i < userIds.size(); i++) {
            String userId = userIds.get(i);
            logger.trace("Processing user {} of {}: {}", i + 1, userIds.size(), userId);
            // ... process user
        }
        
        logger.info("User processing completed");
    }
}

Parameter Substitution

SLF4J uses {} as placeholder for parameters, which is safer and more efficient than string concatenation:

// Efficient - parameters only evaluated if logging level is enabled
logger.debug("User {} has {} active sessions", user.getName(), sessionCount);

// Inefficient - string concatenation always performed
logger.debug("User " + user.getName() + " has " + sessionCount + " active sessions");

// Multiple parameters
logger.info("Transaction {} processed: amount={}, status={}", 
    transaction.getId(), 
    transaction.getAmount(), 
    transaction.getStatus());

// Exception logging - throwable should be the last parameter
logger.error("Failed to process transaction {}", transactionId, exception);

Level Hierarchy

SLF4J log levels follow this hierarchy (from most severe to least severe):

  • ERROR: Error events that might still allow the application to continue
  • WARN: Potentially harmful situations
  • INFO: Informational messages that highlight application progress
  • DEBUG: Fine-grained informational events most useful for debugging
  • TRACE: Finer-grained informational events than DEBUG

When a logger is configured for a specific level, it will log messages at that level and all higher levels. For example, a logger configured for INFO level will log ERROR, WARN, and INFO messages, but not DEBUG or TRACE messages.

Install with Tessl CLI

npx tessl i tessl/maven-org-slf4j--slf4j-api

docs

basic-logging.md

fluent-logging.md

index.md

markers.md

mdc.md

service-providers.md

tile.json