pub fn layer(
loki_url: Url,
labels: HashMap<String, String>,
extra_fields: HashMap<String, String>,
) -> Result<(Layer, BackgroundTask), Error>Expand description
Construct a Layer and its corresponding BackgroundTask.
The Layer needs to be registered with a
tracing_subscriber::Registry, and the BackgroundTask needs to be
tokio::spawned.
Note that unlike the Builder::build_url function, this function
strips off the path component of loki_url before appending
/loki/api/v1/push.
See builder() and this crate’s root documentation for a more flexible
method.
§Example
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;
use url::Url;
#[tokio::main]
async fn main() -> Result<(), tracing_loki::Error> {
let (layer, task) = tracing_loki::layer(
Url::parse("http://127.0.0.1:3100").unwrap(),
vec![("host".into(), "mine".into())].into_iter().collect(),
vec![].into_iter().collect(),
)?;
// 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(())
}