Skip to content

ConfigurationManager

Centralized configuration store that merges values from defaults, a JSON file, environment variables, and runtime updates.

Overview

ConfigurationManager handles all configuration for ezplog. It is created automatically by Ezpl during initialization and is accessible via Ezpl.get_config().

The manager tracks which keys were explicitly set (from file, environment, or runtime) versus which are simply falling back to defaults. This distinction is used by Ezpl.reload_config() to apply priority rules correctly.

You rarely need to use ConfigurationManager directly. Prefer Ezpl.configure() for runtime changes and environment variables for deployment-time configuration.

Usage

from ezplog import Ezpl

ezpl = Ezpl(log_file="app.log")
config = ezpl.get_config()

# Read current values
print(config.get_log_level())        # "INFO"
print(config.get_log_file())         # Path("app.log")
print(config.get_printer_level())    # "INFO"

# Check if a key was explicitly set (not just a default)
print(config.has_key("log-rotation"))  # False if not set explicitly

# Update at runtime
config.set("log-level", "DEBUG")
config.update({"printer-level": "WARNING", "indent-step": 4})

# Persist to ~/.ezpl/config.json
config.save()

# Reset to defaults (in-memory only)
config.reset_to_defaults()

# Export as environment variable script
config.export_to_script("export_env.sh", platform="unix")
config.export_to_script("export_env.bat", platform="windows")

Runtime Configuration via Ezpl

For most use cases, prefer Ezpl.configure() which applies changes to the live handlers:

from ezplog import Ezpl

ezpl = Ezpl(log_file="app.log")

# In-memory update (applies immediately to printer and logger)
ezpl.configure(printer_level="DEBUG", log_rotation="5 MB")

# Persist to disk
ezpl.configure(printer_level="DEBUG", persist=True)

# Change log file (reinitializes the logger)
ezpl.set_log_file("logs/app-v2.log")

Configuration Keys Reference

All configuration keys in the JSON file and in ConfigurationManager use hyphen-separated names.

Key Type Default Description
log-level str "INFO" Global level — applied to both printer and logger when specific levels are not set
log-file str "ezpl.log" Log file name (relative to log-dir if not absolute)
log-dir str platform-specific Base directory for relative log file paths
printer-level str "INFO" Console output minimum level
file-logger-level str "INFO" File output minimum level
indent-step int 3 Spaces per indentation level
indent-symbol str ">" Symbol appended to indented lines
base-indent-symbol str "~" Symbol shown at indent level 0
log-rotation str or null null Rotation trigger: "10 MB", "1 day", "12:00"
log-retention str or null null Retention period: "7 days", "1 month"
log-compression str or null null Compression format: "zip", "gz", "tar.gz"

Class Reference

ConfigurationManager

ConfigurationManager(config_file: Path | None = None)

Centralized configuration manager for Ezpl.

This class handles all configuration operations including loading, saving, and merging configuration from multiple sources.

Initialize the configuration manager.

Parameters:

Name Type Description Default
config_file Path | None

Optional path to configuration file. Defaults to ~/.ezpl/config.json

None

config_file property

config_file: Path

Return the path to the configuration file.

get

get(key: str, default: Any = None) -> Any

Get a configuration value.

Parameters:

Name Type Description Default
key str

Configuration key

required
default Any

Default value if key not found

None

Returns:

Type Description
Any

Configuration value or default

has_key

has_key(key: str) -> bool

Check if a configuration key is explicitly set (not just a default).

A key is considered explicit when it comes from a config file, an environment variable, or a direct call to configure()/set()/update(). Keys that are present only because of default values return False.

Parameters:

Name Type Description Default
key str

Configuration key to check

required

Returns:

Type Description
bool

True if the key was explicitly set, False if it is only a default.

get_log_level

get_log_level() -> str

Get the current log level.

get_log_file

get_log_file() -> Path

Get the current log file path.

get_printer_level

get_printer_level() -> str

Get the current printer level.

get_file_logger_level

get_file_logger_level() -> str

Get the current file logger level.

get_indent_step

get_indent_step() -> int

Get the current indent step.

get_indent_symbol

get_indent_symbol() -> str

Get the current indent symbol.

get_base_indent_symbol

get_base_indent_symbol() -> str

Get the current base indent symbol.

get_log_format

get_log_format() -> str

Get the current log format.

get_log_rotation

get_log_rotation() -> str | None

Get the current log rotation setting.

get_log_retention

get_log_retention() -> str | None

Get the current log retention setting.

get_log_compression

get_log_compression() -> str | None

Get the current log compression setting.

get_all

get_all() -> dict[str, Any]

Get all configuration values.

Returns:

Type Description
dict[str, Any]

Dictionary containing all configuration values

set

set(key: str, value: Any) -> None

Set a configuration value.

Parameters:

Name Type Description Default
key str

Configuration key

required
value Any

Configuration value

required

update

update(config_dict: dict[str, Any]) -> None

Update configuration with new values.

Parameters:

Name Type Description Default
config_dict dict[str, Any]

Dictionary of configuration values to update

required

save

save() -> None

Save current configuration to file.

Raises:

Type Description
FileOperationError

If unable to write to configuration file

reset_to_defaults

reset_to_defaults() -> None

Reset configuration to default values.

reload

reload() -> None

Reload configuration from file and environment variables.

This method reloads the configuration, useful when environment variables or the config file have changed after initialization.

export_to_script

export_to_script(output_file: str | Path, platform: str | None = None) -> None

Export configuration as environment variables script.

Parameters:

Name Type Description Default
output_file str | Path

Path to output script file

required
platform str | None

Target platform ('windows', 'unix', or None for auto-detect)

None

Raises:

Type Description
FileOperationError

If unable to write to output file

options: show_source: false show_root_heading: true show_root_full_path: false show_symbol_type_heading: true show_symbol_type_toc: true members_order: source group_by_category: true show_category_heading: true show_if_no_docstring: false show_signature_annotations: true separate_signature: true signature_crossrefs: true merge_init_into_class: true docstring_style: google docstring_section_style: table