Skip to content
This repository has been archived by the owner on Nov 2, 2023. It is now read-only.

Print logs in serizelable format e.g. JSON #5

Open
genofire opened this issue Dec 4, 2022 · 3 comments
Open

Print logs in serizelable format e.g. JSON #5

genofire opened this issue Dec 4, 2022 · 3 comments

Comments

@genofire
Copy link
Contributor

genofire commented Dec 4, 2022

No description provided.

@p1gp1g
Copy link
Member

p1gp1g commented Dec 4, 2022

With tracing_subscriber for instance ?

cf. https://users.rust-lang.org/t/best-way-to-log-with-json/83385

@genofire
Copy link
Contributor Author

Locks good to me. For me it would be importend, that no extra information like id`s or ip addresses are in the normal message (should be in extra fields).

for anonymisierung and better storing/searching in logs.

@genofire
Copy link
Contributor Author

genofire commented Apr 20, 2023

that is excatly what i try to use on my privat application:

here my config setup:

#[derive(Deserialize)]
pub struct Logging {
    pub json: bool,
}

impl Default for Logging {
    fn default() -> Self {
        Self { json: false }
    }
}

impl Logging {
    fn init(&self) {
        let builder = tracing_subscriber::fmt::SubscriberBuilder::default();
        if self.json {
            builder.json().init();
        } else {
            builder.init();
        }
    }
}

but afterwards i use there macros, to enrich the logs with fields

code:

use tracing::info;

fn main() {
    let config = Config::build().unwrap();

    config.setup();

    info!(
        oauth2.client_id = config.oauth2.client_id,
        oauth2.redirect_url = config.oauth2.redirect_url,
        oauth2.issuer_url = config.oauth2.issuer_url,
        "OAuth Configuration"
    );
}

normal output (color not coppied ...):

Apr 20 09:32:24.172  INFO test: OAuth Configuration oauth2.client_id="lola" oauth2.redirect_url="" oauth2.issuer_url=""

output:

{"timestamp":"Apr 20 00:34:59.648","level":"INFO","fields":{"message":"OAuth Configuration","oauth2.client_id":"lola","oauth2.redirect_url":"","oauth2.issuer_url":""},"target":"test"}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants