context/
context.rs

1use hackerlog::*;
2
3fn scan_ports(host: &str) {
4    // Add context that will be included in all logs in this scope
5    let _host_ctx = logger().add_context("host", host);
6
7    info!("Starting port scan");
8
9    // Add another context
10    let _scan_ctx = logger().add_context("scan_type", "TCP");
11    warn!("Found open port 80"); // Will include both host and scan_type
12
13    // scan_ctx is dropped here, removing "scan_type" from context
14}
15
16fn main() {
17    let _req_id = logger().add_context("request_id", "12345");
18
19    info!("Starting application"); // Includes request_id
20
21    scan_ports("example.com"); // Includes request_id and temporarily host
22
23    info!("Finished!"); // Only includes request_id again
24}