|  | 
| 2 | 2 | 
 | 
| 3 | 3 | This guide explains the structured logging implementation in the Torrust Tracker Deployer project, which uses hierarchical structured logging. | 
| 4 | 4 | 
 | 
|  | 5 | +## Main Application Logging | 
|  | 6 | + | 
|  | 7 | +The main CLI application (`src/main.rs` → `src/app.rs`) initializes logging at startup with user-configurable options. This provides a consistent logging infrastructure for all operations. | 
|  | 8 | + | 
|  | 9 | +### Application Logging Setup | 
|  | 10 | + | 
|  | 11 | +The main application uses `LoggingBuilder` with CLI arguments for configuration: | 
|  | 12 | + | 
|  | 13 | +```rust | 
|  | 14 | +// src/app.rs | 
|  | 15 | +use torrust_tracker_deployer_lib::logging::{LogFormat, LogOutput, LoggingBuilder}; | 
|  | 16 | + | 
|  | 17 | +pub fn run() { | 
|  | 18 | +    let cli = Cli::parse(); | 
|  | 19 | + | 
|  | 20 | +    // Initialize logging FIRST before any other logic | 
|  | 21 | +    LoggingBuilder::new(&cli.log_dir) | 
|  | 22 | +        .with_format(cli.log_format) | 
|  | 23 | +        .with_output(cli.log_output) | 
|  | 24 | +        .init(); | 
|  | 25 | + | 
|  | 26 | +    // Log startup with context | 
|  | 27 | +    info!( | 
|  | 28 | +        app = "torrust-tracker-deployer", | 
|  | 29 | +        version = env!("CARGO_PKG_VERSION"), | 
|  | 30 | +        log_dir = %cli.log_dir.display(), | 
|  | 31 | +        log_format = ?cli.log_format, | 
|  | 32 | +        log_output = ?cli.log_output, | 
|  | 33 | +        "Application started" | 
|  | 34 | +    ); | 
|  | 35 | + | 
|  | 36 | +    // ... application logic ... | 
|  | 37 | + | 
|  | 38 | +    info!("Application finished"); | 
|  | 39 | +} | 
|  | 40 | +``` | 
|  | 41 | + | 
|  | 42 | +### User-Facing Configuration | 
|  | 43 | + | 
|  | 44 | +Users can configure logging via CLI arguments: | 
|  | 45 | + | 
|  | 46 | +```bash | 
|  | 47 | +# Default (production): file-only, compact format | 
|  | 48 | +torrust-tracker-deployer | 
|  | 49 | + | 
|  | 50 | +# Development: stderr output, pretty format | 
|  | 51 | +torrust-tracker-deployer --log-format pretty --log-output file-and-stderr | 
|  | 52 | + | 
|  | 53 | +# Custom log directory | 
|  | 54 | +torrust-tracker-deployer --log-dir /var/log/deployer | 
|  | 55 | + | 
|  | 56 | +# JSON format for log aggregation | 
|  | 57 | +torrust-tracker-deployer --log-format json | 
|  | 58 | +``` | 
|  | 59 | + | 
|  | 60 | +See [User Guide: Logging](../user-guide/logging.md) for complete user documentation. | 
|  | 61 | + | 
| 5 | 62 | ## JSON Output Format | 
| 6 | 63 | 
 | 
| 7 | 64 | When using `logging::init_json()` or `LogFormat::Json`, logs are output in JSON format suitable for log aggregation: | 
|  | 
0 commit comments