Package com.log4rich

Class Log4Rich

java.lang.Object
com.log4rich.Log4Rich

public class Log4Rich extends Object
Main entry point and facade for the log4Rich logging framework. This class provides static methods for easy logger access and configuration.
  • Method Details

    • getLogger

      public static Logger getLogger(Class<?> clazz)
      Gets a logger for the specified class. This is the preferred method for getting loggers in most cases.
      Parameters:
      clazz - the class to get logger for
      Returns:
      logger instance for the specified class
    • getLogger

      public static Logger getLogger(String name)
      Gets a logger for the specified name. Use this method when you need a logger with a specific name.
      Parameters:
      name - the logger name
      Returns:
      logger instance for the specified name
    • getRootLogger

      public static Logger getRootLogger()
      Gets the root logger. The root logger is the parent of all loggers in the hierarchy.
      Returns:
      the root logger instance
    • setConfigPath

      public static void setConfigPath(String path) throws Exception
      Sets the configuration file path and reloads the configuration. This method allows you to specify a custom configuration file location and immediately load the configuration from that file.
      Parameters:
      path - the path to the configuration file, or null to use default search
      Throws:
      Exception - if the configuration cannot be loaded from the specified path
    • getConfigPath

      public static String getConfigPath()
      Gets the current configuration file path.
      Returns:
      the configuration file path, or null if not set
    • setRootLevel

      public static void setRootLevel(LogLevel level)
      Sets the root logger level. This affects all loggers that don't have specific levels configured. Changes take effect immediately.
      Parameters:
      level - the new root level to set
    • setLocationCapture

      public static void setLocationCapture(boolean enabled)
      Sets the location capture setting globally. When enabled, the framework will capture and include class name, method name, and line number information in log events.
      Parameters:
      enabled - true to enable location capture, false to disable
    • setLoggerLevel

      public static void setLoggerLevel(String loggerName, LogLevel level)
      Sets a specific logger's level. This overrides the root level for the specified logger.
      Parameters:
      loggerName - the name of the logger to configure
      level - the level to set for this logger
    • setConsoleEnabled

      public static void setConsoleEnabled(boolean enabled)
      Enables or disables console logging. When enabled, log messages will be written to the console.
      Parameters:
      enabled - true to enable console logging, false to disable
    • setConsoleTarget

      public static void setConsoleTarget(String target)
      Sets the console target stream.
      Parameters:
      target - the target stream ("STDOUT" or "STDERR")
    • setConsolePattern

      public static void setConsolePattern(String pattern)
      Sets the console logging pattern. The pattern defines how log messages are formatted for console output.
      Parameters:
      pattern - the pattern string (e.g., "[%level] %date %message%n")
    • setFileEnabled

      public static void setFileEnabled(boolean enabled)
      Enables or disables file logging. When enabled, log messages will be written to the configured file.
      Parameters:
      enabled - true to enable file logging, false to disable
    • setFilePath

      public static void setFilePath(String filePath)
      Sets the file path for logging. This changes where log messages are written.
      Parameters:
      filePath - the path to the log file
    • setFilePattern

      public static void setFilePattern(String pattern)
      Sets the file logging pattern. The pattern defines how log messages are formatted for file output.
      Parameters:
      pattern - the pattern string (e.g., "[%level] %date %message%n")
    • setMaxFileSize

      public static void setMaxFileSize(String maxSize)
      Sets the maximum file size for rolling. When the log file reaches this size, it will be rolled over.
      Parameters:
      maxSize - the maximum size (e.g., "10M", "100K", "1G")
    • setMaxBackups

      public static void setMaxBackups(int maxBackups)
      Sets the maximum number of backup files. When rolling over, this many backup files will be kept.
      Parameters:
      maxBackups - the maximum number of backup files to keep
    • setCompression

      public static void setCompression(boolean enabled, String program, String args)
      Sets compression settings for rolled log files. When enabled, rolled log files will be compressed using the specified program.
      Parameters:
      enabled - true to enable compression, false to disable
      program - the compression program to use (e.g., "gzip", "bzip2")
      args - the arguments for the compression program
    • createConsoleAppender

      public static ConsoleAppender createConsoleAppender(String name, String target, String pattern)
      Creates a new console appender. The appender will be managed by the framework and can be retrieved by name.
      Parameters:
      name - the name of the appender
      target - the target stream ("STDOUT" or "STDERR")
      pattern - the pattern to use for formatting, or null for default
      Returns:
      the created console appender
    • createRollingFileAppender

      public static RollingFileAppender createRollingFileAppender(String name, String filePath, String maxSize, int maxBackups, String pattern)
      Creates a new rolling file appender. The appender will be managed by the framework and can be retrieved by name.
      Parameters:
      name - the name of the appender
      filePath - the path to the log file
      maxSize - the maximum file size (e.g., "10M", "100K")
      maxBackups - the maximum number of backup files
      pattern - the pattern to use for formatting, or null for default
      Returns:
      the created rolling file appender
    • createRollingFileAppender

      public static RollingFileAppender createRollingFileAppender(String filePath)
      Creates a simple rolling file appender with default settings. Uses default settings: 10MB max size, 10 backup files, default pattern.
      Parameters:
      filePath - the path to the log file
      Returns:
      the created rolling file appender
    • getManagedAppender

      public static Appender getManagedAppender(String name)
      Gets a managed appender by name.
      Parameters:
      name - the name of the appender
      Returns:
      the appender, or null if not found
    • removeManagedAppender

      public static Appender removeManagedAppender(String name)
      Removes a managed appender. The appender will be closed and can no longer be used.
      Parameters:
      name - the name of the appender to remove
      Returns:
      the removed appender, or null if not found
    • reloadConfiguration

      public static void reloadConfiguration() throws Exception
      Reloads configuration from the original source. This re-reads the configuration file and applies any changes.
      Throws:
      Exception - if configuration cannot be reloaded
    • getCurrentConfiguration

      public static Configuration getCurrentConfiguration()
      Gets the current configuration.
      Returns:
      the current configuration object
    • getStats

      public static ConfigurationManager.ConfigurationStats getStats()
      Gets configuration statistics. Provides information about the current state of the logging system.
      Returns:
      configuration statistics including logger and appender counts
    • shutdown

      public static void shutdown()
      Shuts down the logging framework. This closes all loggers and their appenders, ensuring proper resource cleanup. After shutdown, the framework can be reinitialized on the next logging call.
    • main

      public static void main(String[] args)
      Main method for testing and demonstration. This method demonstrates the basic usage of the log4Rich framework.
      Parameters:
      args - command line arguments (not used)