Crate loki_logger[][src]

Expand description

A loki logger for the log facade. One event is written and send to loki per log call. Each event contain the time in nano second it was scheduled to be sent, in most cases, when the logging occured.

Examples

You simply need to specify your loki push URL and the minimum log level to start the logger.

use log::LevelFilter;

loki_logger::init(
    "http://loki:3100/loki/api/v1/push",
    log::LevelFilter::Info,
).unwrap();

log::info!("Logged into Loki !");

Or specify static labels to use in your loki streams. Those labels are overwriten by event-specific label, if any.

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

let initial_labels = HashMap::from_iter([
    ("application".to_string(), "loki_logger".to_string()),
    ("environment".to_string(), "development".to_string()),
]);

loki_logger::init_with_labels(
    "http://loki:3100/loki/api/v1/push",
    log::LevelFilter::Info,
    initial_labels,
).unwrap();

log::info!("Logged into Loki !");

Log format

Each and every log event sent to loki will contain at least the level of the event as well as the time in nanoseconds of the event scheduling.

Notice on extra labels

Starting from 0.4.7, the log crate started introducing the new key/value system for structured logging.

The loki_logger crate makes heavy use of such system as to create and send custom loki labels.

If you want to use the key:value tag system, you have to use the git version of the log crate and enable the kv_unstable feature:

[dependencies.log]
git = "https://github.com/rust-lang/log.git"
branch = "kv_macro"
features = ["kv_unstable"]

The ability to use the key:value system with the log crate’s macros should come up with the 0.4.15 release or afterwards.

The kv_unstable feature allows you to use the log facade as such:

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


loki_logger::init(
    "http://loki:3100/loki/api/v1/push",
    log::LevelFilter::Info,
).unwrap();

// Due to stabilization issue, this is still unstable,
// the log macros needs to have at least one formatting parameter for this to work.
log::info!(foo = "bar"; "Logged into Loki !{}", "");

Notice on asynchronous execution

The loki_logger crate ships with asynchronous execution, orchestrated with tokio, by default.

This means that for the logging operations to work, you need to be in the scope of a asynchronous runtime first.

Otherwise, you can activate the blocking feature of The loki_logger crate to use a blocking client.

THIS IS NOT RECOMMENDED FOR PRODUCTIONS WORKLOAD.

Re-exports

pub use log;

Functions

Configure the log facade to log to loki.

Configure the log facade to log to loki.