#tracing #loki

tracing-loki

A tracing layer for shipping logs to Grafana Loki

7 releases

0.2.4 Aug 1, 2023
0.2.3 Jun 16, 2023
0.2.2 Mar 8, 2023
0.2.1 Jul 2, 2022
0.0.0 Mar 19, 2022

#1 in #loki

Download history 8878/week @ 2023-11-01 7415/week @ 2023-11-08 11344/week @ 2023-11-15 9903/week @ 2023-11-22 9726/week @ 2023-11-29 9875/week @ 2023-12-06 9257/week @ 2023-12-13 4962/week @ 2023-12-20 4468/week @ 2023-12-27 7828/week @ 2024-01-03 8289/week @ 2024-01-10 11069/week @ 2024-01-17 9374/week @ 2024-01-24 6509/week @ 2024-01-31 6110/week @ 2024-02-07 6312/week @ 2024-02-14

30,906 downloads per month
Used in 3 crates (2 directly)

MIT/Apache

57KB
1K SLoC

tracing-loki

A tracing layer for Grafana Loki.

Build status

Documentation

https://docs.rs/tracing-loki

Usage

Add this to your Cargo.toml:

[dependencies]
tracing-loki = "0.1"

Example

use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;
use std::process;
use url::Url;

#[tokio::main]
async fn main() -> Result<(), tracing_loki::Error> {
    let (layer, task) = tracing_loki::builder()
        .label("host", "mine")?
        .extra_field("pid", format!("{}", process::id()))?
        .build_url(Url::parse("http://127.0.0.1:3100").unwrap())?;

    // We need to register our layer with `tracing`.
    tracing_subscriber::registry()
        .with(layer)
        // One could add more layers here, for example logging to stdout:
        // .with(tracing_subscriber::fmt::Layer::new())
        .init();

    // The background task needs to be spawned so the logs actually get
    // delivered.
    tokio::spawn(task);

    tracing::info!(
        task = "tracing_setup",
        result = "success",
        "tracing successfully set up",
    );

    Ok(())
}

Dependencies

~9–22MB
~332K SLoC