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
21#[cfg(feature = "traces")]
22#[macro_use]
23pub mod macros;
24
25mod config;
26mod subscriber;
27
28#[cfg(feature = "json-logger")]
29pub mod json;
30#[cfg(feature = "traces")]
31pub mod layer;
32#[cfg(feature = "traces")]
33pub mod resources;
34#[cfg(feature = "traces")]
35pub mod telemetry;
36
37pub use crate::config::{
38    builder, Country, Environment, EnvironmentParseError, SubscriberConfig, SubscriberConfigBuilder,
39};
40pub use crate::subscriber::{
41    configure_subscriber, init_subscriber, ContextInfo, EventFormatter, Tracing, Uninstall,
42};
43pub use tracing;
44
45/// Create a tracing error event, casting the error to &dyn [std::error::Error] for [layer::ErrorLayer],
46/// and adding the type name as error.kind.
47///
48/// Usage:
49/// ```
50/// use prima_tracing::report_error;
51///
52/// let error = "not a number".parse::<usize>().unwrap_err();
53/// report_error!(error, "Parsing error!");
54/// ```
55///
56/// You can also add use add attributes and do things, just like with a regular [tracing::error]
57/// macro call
58/// ```
59/// use prima_tracing::report_error;
60/// # let input = "not a number";
61/// # let uid = "1223";
62///
63/// let error = input.parse::<usize>().unwrap_err();
64/// report_error!(error, input, user=uid, "Parsing error: {}", error);
65/// ```
66#[macro_export]
67macro_rules! report_error {
68    ($error:expr, $($args:tt)*) => {
69        {
70          let kind = ::std::any::type_name_of_val(&$error);
71          $crate::tracing::error!(error.kind = kind, error = &$error as &dyn ::std::error::Error, $($args)+)
72        }
73    };
74}