Logging Example¶
Demonstrates logging support.
1"""
2Example demonstrating logging and diagnostic features.
3
4This example shows:
5- How to enable debug logging
6- How to use check-variables to inspect configuration
7- Source status tracking
8
9Run with:
10 python logging_example.py
11 python logging_example.py -cv # Check variables with detailed info
12"""
13
14import logging
15import os
16from dataclasses import dataclass, field
17
18from varlord import Config, set_log_level, sources
19
20# Enable debug logging to see configuration loading details
21# This shows merge operations, source loads, etc.
22set_log_level(logging.DEBUG)
23
24# Set some environment variables for demonstration
25os.environ["HOST"] = "0.0.0.0"
26os.environ["PORT"] = "9000"
27
28
29@dataclass(frozen=True)
30class AppConfig:
31 """Application configuration model."""
32
33 host: str = field(default="127.0.0.1", metadata={"description": "Server host address"})
34 port: int = field(default=8000, metadata={"description": "Server port number"})
35 debug: bool = field(default=False, metadata={"description": "Enable debug mode"})
36
37
38def main():
39 """Main function."""
40 cfg = Config(
41 model=AppConfig,
42 sources=[
43 sources.Env(), # Model defaults applied automatically, model auto-injected
44 sources.CLI(), # CLI arguments can override env vars
45 ],
46 )
47
48 # Handle CLI commands (including -cv for check-variables)
49 cfg.handle_cli_commands()
50
51 # Load configuration
52 # With DEBUG logging enabled, you'll see detailed logs about:
53 # - Source loading
54 # - Configuration merging
55 # - Value overrides
56 app = cfg.load()
57
58 print("\n✅ Configuration loaded successfully!")
59 print(f" Host: {app.host}")
60 print(f" Port: {app.port}")
61 print(f" Debug: {app.debug}")
62 print("\n💡 Tip: Run with -cv to see detailed source information and status")
63
64
65if __name__ == "__main__":
66 main()