Package com.log4rich
Class Log4Rich
java.lang.Object
com.log4rich.Log4Rich
Main entry point and facade for the log4Rich logging framework.
This class provides static methods for easy logger access and configuration.
-
Method Summary
Modifier and TypeMethodDescriptionstatic ConsoleAppendercreateConsoleAppender(String name, String target, String pattern) Creates a new console appender.static RollingFileAppendercreateRollingFileAppender(String filePath) Creates a simple rolling file appender with default settings.static RollingFileAppendercreateRollingFileAppender(String name, String filePath, String maxSize, int maxBackups, String pattern) Creates a new rolling file appender.static StringGets the current configuration file path.static ConfigurationGets the current configuration.static LoggerGets a logger for the specified class.static LoggerGets a logger for the specified name.static AppendergetManagedAppender(String name) Gets a managed appender by name.static LoggerGets the root logger.getStats()Gets configuration statistics.static voidMain method for testing and demonstration.static voidReloads configuration from the original source.static AppenderremoveManagedAppender(String name) Removes a managed appender.static voidsetCompression(boolean enabled, String program, String args) Sets compression settings for rolled log files.static voidsetConfigPath(String path) Sets the configuration file path and reloads the configuration.static voidsetConsoleEnabled(boolean enabled) Enables or disables console logging.static voidsetConsolePattern(String pattern) Sets the console logging pattern.static voidsetConsoleTarget(String target) Sets the console target stream.static voidsetFileEnabled(boolean enabled) Enables or disables file logging.static voidsetFilePath(String filePath) Sets the file path for logging.static voidsetFilePattern(String pattern) Sets the file logging pattern.static voidsetLocationCapture(boolean enabled) Sets the location capture setting globally.static voidsetLoggerLevel(String loggerName, LogLevel level) Sets a specific logger's level.static voidsetMaxBackups(int maxBackups) Sets the maximum number of backup files.static voidsetMaxFileSize(String maxSize) Sets the maximum file size for rolling.static voidsetRootLevel(LogLevel level) Sets the root logger level.static voidshutdown()Shuts down the logging framework.
-
Method Details
-
getLogger
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
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
Gets the root logger. The root logger is the parent of all loggers in the hierarchy.- Returns:
- the root logger instance
-
setConfigPath
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
Gets the current configuration file path.- Returns:
- the configuration file path, or null if not set
-
setRootLevel
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
Sets a specific logger's level. This overrides the root level for the specified logger.- Parameters:
loggerName- the name of the logger to configurelevel- 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
Sets the console target stream.- Parameters:
target- the target stream ("STDOUT" or "STDERR")
-
setConsolePattern
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
Sets the file path for logging. This changes where log messages are written.- Parameters:
filePath- the path to the log file
-
setFilePattern
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
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
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 disableprogram- the compression program to use (e.g., "gzip", "bzip2")args- the arguments for the compression program
-
createConsoleAppender
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 appendertarget- 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 appenderfilePath- the path to the log filemaxSize- the maximum file size (e.g., "10M", "100K")maxBackups- the maximum number of backup filespattern- the pattern to use for formatting, or null for default- Returns:
- the created rolling file appender
-
createRollingFileAppender
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
Gets a managed appender by name.- Parameters:
name- the name of the appender- Returns:
- the appender, or null if not found
-
removeManagedAppender
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
Reloads configuration from the original source. This re-reads the configuration file and applies any changes.- Throws:
Exception- if configuration cannot be reloaded
-
getCurrentConfiguration
Gets the current configuration.- Returns:
- the current configuration object
-
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
Main method for testing and demonstration. This method demonstrates the basic usage of the log4Rich framework.- Parameters:
args- command line arguments (not used)
-