log_structured/
log_structured.rs

1use std::{env, time::Duration};
2
3use log::{info, error};
4use logscale_log::{logscale_structured_logger::LogScaleStructuredLogger, options::LoggerOptions};
5
6#[derive(serde::Serialize)]
7struct Nested {
8    x: i32,
9    y: String
10}
11
12#[tokio::main]
13async fn main() {
14    let args: Vec<String> = env::args().collect();
15
16    let ingest_token = args
17        .get(1)
18        .expect("Missing '--ingest-token' parameter.")
19        .replace("--ingest-token=", "");
20
21    LogScaleStructuredLogger::init(
22        String::from("https://cloud.community.humio.com"),
23        ingest_token,
24        LoggerOptions::default()
25    )
26    .unwrap();
27
28    log::set_max_level(log::LevelFilter::Trace);
29
30    let nested = Nested {x: 42, y: String::from("some string parameter.")};
31    let mut count = 0;
32    
33    // Loop to let the background sync task have time to do its thing.
34    loop {
35        info!("Plain log only with text, no variables");
36
37        error!("You can of course also use any other log level, like error.");
38
39        info!(count = format!("{}", count); "You can also add structured values to your logs, which will automatically be included as attributes in your LogScale logs.");
40
41        info!(target: "api",
42            method = "GET", 
43            path = "/hello", 
44            status = 200_u16, 
45            elapsed = 10_u64, 
46            nested = log::as_serde!(nested); 
47            "You can also create logs with multiple structured values, and even nested JSON values.",);
48
49        std::thread::sleep(Duration::from_secs(1));
50        
51        count += 1;
52    }
53}