prima_tracing/
lib.rs

1//! `prima_tracing` provide an handy way for configuring the [`tracing`] crate with
2//! support for JSON output formatter and integration with opentelemetry
3//! # Usage
4//!
5//! ```rust
6//!
7//! use prima_tracing::{builder, configure_subscriber, Country, Environment, init_subscriber};
8//! # #[cfg(not(feature = "traces"))]
9//! # {
10//! let subscriber = configure_subscriber(
11//!   builder("ping")
12//!     .with_country(Country::Common)
13//!     .with_env(Environment::Dev)
14//!     .build()
15//! );
16//!
17//! let _guard = init_subscriber(subscriber);
18//! # }
19//! ```
20
21mod config;
22
23mod subscriber;
24
25#[cfg(feature = "json-logger")]
26pub mod json;
27#[cfg(feature = "traces")]
28pub mod layer;
29#[cfg(feature = "traces")]
30pub mod telemetry;
31
32pub use crate::config::{
33    builder, Country, Environment, EnvironmentParseError, SubscriberConfig, SubscriberConfigBuilder,
34};
35pub use crate::subscriber::{
36    configure_subscriber, init_subscriber, ContextInfo, EventFormatter, Tracing, Uninstall,
37};
38pub use tracing;
39
40/// Create a tracing error event, casting the error to &dyn [std::error::Error] for [layer::ErrorLayer],
41/// and adding the type name as error.kind.
42///
43/// Usage:
44/// ```
45/// use prima_tracing::report_error;
46///
47/// let error = "not a number".parse::<usize>().unwrap_err();
48/// report_error!(error, "Parsing error!");
49/// ```
50///
51/// You can also add use add attributes and do things, just like with a regular [tracing::error]
52/// macro call
53/// ```
54/// use prima_tracing::report_error;
55/// # let input = "not a number";
56/// # let uid = "1223";
57///
58/// let error = input.parse::<usize>().unwrap_err();
59/// report_error!(error, input, user=uid, "Parsing error: {}", error);
60/// ```
61#[macro_export]
62macro_rules! report_error {
63    ($error:expr, $($args:tt)*) => {
64        {
65          let kind = ::std::any::type_name_of_val(&$error);
66          $crate::tracing::error!(error.kind = kind, error = &$error as &dyn ::std::error::Error, $($args)+)
67        }
68    };
69}