Created
February 16, 2021 13:51
-
-
Save imbolc/f75f924f66c129f0e1e910f59fb75c0d to your computer and use it in GitHub Desktop.
Rust logging into journald configured by the `RUST_LOG` environment variable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
anyhow = "1" | |
log = "0.4" | |
tracing = { git = "https://github.com/tokio-rs/tracing", rev = "f81426b" } | |
tracing-subscriber = { git = "https://github.com/tokio-rs/tracing", rev = "f81426b" } | |
tracing-journald = { git = "https://github.com/tokio-rs/tracing", rev = "f81426b" } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use anyhow::{Context, Result}; | |
pub fn init_journald_logging() -> Result<()> { | |
use tracing_subscriber::{prelude::*, registry, EnvFilter}; | |
registry() | |
.with(EnvFilter::try_from_default_env().context("couldn't parse `RUST_LOG` env var")?) | |
.with(tracing_journald::subscriber().context("couldn't connect to journald")?) | |
.init(); | |
Ok(()) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use anyhow::Result; | |
fn main() -> Result<()> { | |
lib::init_journald_logging()?; | |
log::info!("Tracing intercepts log events, so you can just continue using it"); | |
tracing::error!(foo = "bar", "Or you can use events to pass structural data"); | |
Ok(()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment