2 unstable releases

0.2.1 Sep 21, 2023
0.1.1 Sep 20, 2023
0.1.0 Sep 20, 2023

#1838 in Encoding

Download history 1/week @ 2024-07-16 25/week @ 2024-07-23 49/week @ 2024-07-30 22/week @ 2024-08-06 4/week @ 2024-08-20 50/week @ 2024-08-27 13/week @ 2024-09-03 4/week @ 2024-09-10 1/week @ 2024-09-17 31/week @ 2024-09-24 17/week @ 2024-10-01 156/week @ 2024-10-08 30/week @ 2024-10-15 1/week @ 2024-10-22

197 downloads per month

MIT license

11KB
234 lines

Logger with Json Format

json_log helpers you to print logs in json format.

Usage

First, add these in your Cargo.toml:

[dependencies]
log = { version = "0.4" }
json_log = { version = "0.1" }

serde = { version = "1", features = ["derive"] } # if you want to log your struct...

You need to choose the logging level after the program starts. For example:

use log::LevelFilter;

fn main() {
    json_log::init_with_level(LevelFilter::Info).unwrap(); // use the `INFO` level.
}

Or, you can set environment variable RUST_LOG:

fn main() {
    // Note: set environment variable `RUST_LOG` to one of these (case insensitive):
    let accepted_RUST_LOG = ["Trace", "Debug", "Info", "Warn", "Error"];
    
    json_log::init_env().unwrap();
}

Simple Text Logging

json_log is an adapter of the log trait. You can use all the log features in your code.

For example:

use log::LevelFilter;
use log::{debug, info, warn, error};

fn main() {
    json_log::init_with_level(LevelFilter::Debug).unwrap();
    
    let a = 1 + 2 + 3;
    
    debug!("debug");
    info!("a = {a}");
    if a == 6 {
        warn!("a is {}", a);
    } else {
        error!("unknown error: a should be {}, but got {a}", 6);
    }
}

And here is the output:

{"level":"Debug","ts":1695181245461,"msg":"debug"}
{"level":"Info","ts":1695181245461,"msg":"a = 6"}
{"level":"Warn","ts":1695181245461,"msg":"a is 6"}

Log your struct

If you have already defined your own logging struct, you can log it by make it Serialize. For example:

use serde::Serialize;

#[derive(Serialize)]
struct MyLoggingStruct {
    pub status: u16,
    pub username: String,
    pub method: String,
    pub headers: std::collections::HashMap<String, String>,
}

// Or, ...
#[derive(Serialize)]
struct MyLoggingStructV2<'a> {
    pub status: u16,
    pub username: &'a str,
    pub method: &'a str,
    pub headers: std::collections::HashMap<&'a str, &'a str>,
}

Then, do the log:

use std::collections::HashMap;
use log::LevelFilter;

// A static reference, you can define it everywhere.
static MY_LOGGER: &json_log::JsonLogger = json_log::get_default_logger();

fn main() {
    json_log::init_with_level(LevelFilter::Debug).unwrap(); // set to debug level

    let data = MyLoggingStruct {
        status: 404,
        username: "My name is Jack".to_string(),
        method: "ListMyMoney".to_string(),
        headers: std::collections::HashMap::new(),
    };

    MY_LOGGER.info(&data);
}

For more examples, see ./examples.

Dependencies

~0.7–1.7MB
~36K SLoC