Package com.log4rich.config
Class ConfigurationManager
java.lang.Object
com.log4rich.config.ConfigurationManager
Manages runtime configuration changes for the log4Rich framework.
Provides thread-safe methods to modify configuration at runtime.
This class serves as the central point for all configuration management
and automatically applies changes to the logging system.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classConfiguration statistics class. -
Method Summary
Modifier and TypeMethodDescriptionstatic voidManually applies the current configuration.static ConsoleAppendercreateConsoleAppender(String name, String target, String pattern) Creates a new console appender with the specified settings.static RollingFileAppendercreateRollingFileAppender(String name, String filePath, String maxSize, int maxBackups, String pattern) Creates a new rolling file appender with the specified settings.static ConfigurationGets the current configuration.static AppendergetManagedAppender(String name) Gets a managed appender by name.getStats()Gets statistics about the current configuration.static voidinitialize(Configuration config) Initializes the configuration manager with the current configuration.static voidReloads configuration from the original source.static AppenderremoveManagedAppender(String name) Removes a managed appender.static voidsetAutoApplyConfiguration(boolean autoApply) Sets whether configuration changes should be automatically applied.static voidsetCompression(boolean enabled, String program, String args) Sets compression settings for rolled log files.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 configuration manager and cleans up all managed appenders.
-
Method Details
-
initialize
Initializes the configuration manager with the current configuration. This method sets up the configuration and applies it to all loggers.- Parameters:
config- the configuration to use
-
getCurrentConfiguration
Gets the current configuration. This method is thread-safe and returns a reference to the current configuration.- Returns:
- the current configuration, or null if not initialized
-
setRootLevel
Sets the root logger level. This affects all loggers that don't have specific levels configured. Changes take effect immediately if auto-apply is enabled.- 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 with the specified settings. 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 with the specified settings. 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
-
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
-
setAutoApplyConfiguration
public static void setAutoApplyConfiguration(boolean autoApply) Sets whether configuration changes should be automatically applied. When disabled, changes must be applied manually using applyCurrentConfiguration().- Parameters:
autoApply- true to automatically apply configuration changes, false otherwise
-
applyCurrentConfiguration
public static void applyCurrentConfiguration()Manually applies the current configuration. This method is useful when auto-apply is disabled. -
getStats
Gets statistics about the current configuration. 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 configuration manager and cleans up all managed appenders. This method closes all managed appenders and clears the configuration.
-