1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
//! # tracing-ndjson
//!
//! [](https://github.com/cmackenzie1/tracing-ndjson/actions/workflows/rust.yml)
//!
//! A simple library for tracing in new-line delimited JSON format. This library is meant to be used with [tracing](https://github.com/tokio-rs/tracing) as an alternative to the `tracing_subscriber::fmt::json` formatter.
//!
//! ## Features
//!
//! - Configurable field names for `target`, `message`, `level`, and `timestamp`.
//! - Configurable timestamp formats such as RFC3339, UNIX timestamp, or any custom chrono format.
//! - Captures all span attributes and event fields in the root of the JSON object.
//!
//! ## Usage
//!
//! Add this to your `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! tracing = "0.1"
//! tracing-ndjson = "0.1"
//! ```
//!
//! ```rust
//! use tracing_subscriber::prelude::*;
//!
//! tracing_subscriber::registry()
//! .with(tracing_ndjson::builder().layer())
//! .init();
//! tracing::info!(life = 42, "Hello, world!");
//! // {"level":"info","timestamp":"2023-10-08T03:30:52Z","target":"default","message":"Hello, world!"}
//! let span = tracing::info_span!("hello", "request.uri" = "https://example.com");
//! span.in_scope(|| {
//! tracing::info!("Hello, world!");
//! // {"level":"info","timestamp":"2023-10-08T03:34:33Z","target":"defaults","message":"Hello, world!","request.uri":"https://example.com"}
//! });
//! ```
//!
//! ### Examples
//!
//! See the [examples](./examples) directory for more examples.
//!
//! ## License
//!
//! Licensed under MIT license [LICENSE](./LICENSE)
mod formatter;
use tracing_core::Subscriber;
use tracing_subscriber::fmt::{Layer, SubscriberBuilder};
use tracing_subscriber::registry::LookupSpan;
/// A timestamp format for the JSON formatter.
/// This is used to format the timestamp field in the JSON output.
/// The default is RFC3339.
#[derive(Debug)]
pub enum TimestampFormat {
/// Seconds since UNIX_EPOCH
Unix,
/// Milliseconds since UNIX_EPOCH
UnixMillis,
/// RFC3339
Rfc3339,
/// RFC3339 with nanoseconds
Rfc3339Nanos,
/// Custom format string. This should be a valid format string for chrono.
Custom(String),
}
impl TimestampFormat {
fn format_string(&self, now: &chrono::DateTime<chrono::Utc>) -> String {
match self {
TimestampFormat::Unix => now.timestamp().to_string(),
TimestampFormat::UnixMillis => now.timestamp_millis().to_string(),
TimestampFormat::Rfc3339 => now.to_rfc3339_opts(chrono::SecondsFormat::Secs, true),
TimestampFormat::Rfc3339Nanos => {
now.to_rfc3339_opts(chrono::SecondsFormat::Nanos, true)
}
TimestampFormat::Custom(format) => now.format(format).to_string(),
}
}
fn format_number(&self, now: &chrono::DateTime<chrono::Utc>) -> u64 {
match self {
TimestampFormat::Unix => now.timestamp() as u64,
TimestampFormat::UnixMillis => now.timestamp_millis() as u64,
TimestampFormat::Rfc3339 => unreachable!("rfc3339 is not a number"),
TimestampFormat::Rfc3339Nanos => unreachable!("rfc3339_nanos is not a number"),
TimestampFormat::Custom(_) => unreachable!("custom is not a number"),
}
}
}
#[derive(Debug, thiserror::Error)]
enum Error {
#[error("fmt error: {0}")]
Format(#[from] std::fmt::Error),
#[error("json error: {0}")]
Serde(#[from] serde_json::Error),
#[error("utf8 error: {0}")]
Utf8(#[from] std::str::Utf8Error),
#[error("unknown error")]
Unknown,
}
impl From<Error> for std::fmt::Error {
fn from(_: Error) -> Self {
Self
}
}
/// A builder for the JSON formatter.
/// This is used to configure the JSON formatter.
/// The default configuration is:
/// * level_name: "level"
/// * message_name: "message"
/// * target_name: "target"
/// * timestamp_name: "timestamp"
/// * timestamp_format: TimestampFormat::Rfc3339
/// * flatten_fields: true
///
/// # Examples
///
/// ```rust
/// use tracing_subscriber::prelude::*;
///
/// tracing_subscriber::registry()
/// .with(
/// tracing_ndjson::Builder::default()
/// .with_level_name("severity")
/// .with_message_name("msg")
/// .with_timestamp_name("ts")
/// .with_timestamp_format(tracing_ndjson::TimestampFormat::Unix)
/// .layer(),
/// ).init();
///
/// tracing::info!(life = 42, "Hello, world!");
pub struct Builder {
events: formatter::JsonEventFormatter,
fields: formatter::FieldsFormatter,
}
impl Builder {
pub fn new() -> Self {
Self {
events: formatter::JsonEventFormatter::new(),
fields: formatter::FieldsFormatter::new(),
}
}
}
impl Default for Builder {
fn default() -> Self {
Self::new()
}
}
/// Alias for `Builder::default()`.
/// This is used to configure the JSON formatter.
pub fn builder() -> Builder {
Builder::default()
}
impl Builder {
/// Set the field name for the level field.
/// The default is "level".
pub fn with_level_name(mut self, level_name: &'static str) -> Self {
self.events = self.events.with_level_name(level_name);
self
}
/// Set the field name for the message field.
/// The default is "message".
pub fn with_message_name(mut self, message_name: &'static str) -> Self {
self.events = self.events.with_message_name(message_name);
self
}
/// Set the field name for the target field.
/// The default is "target".
pub fn with_target_name(mut self, target_name: &'static str) -> Self {
self.events = self.events.with_target_name(target_name);
self
}
/// Set the field name for the timestamp field.
/// The default is "timestamp".
pub fn with_timestamp_name(mut self, timestamp_name: &'static str) -> Self {
self.events = self.events.with_timestamp_name(timestamp_name);
self
}
/// Set the timestamp format for the timestamp field.
/// The default is TimestampFormat::Rfc3339.
pub fn with_timestamp_format(mut self, timestamp_format: TimestampFormat) -> Self {
self.events = self.events.with_timestamp_format(timestamp_format);
self
}
/// Set whether to flatten fields.
/// The default is true. If false, fields will be nested under a "fields" object.
pub fn with_flatten_fields(mut self, flatten_fields: bool) -> Self {
self.events = self.events.with_flatten_fields(flatten_fields);
self
}
/// Return a `Layer` that subscribes to all spans and events using the defined formatter.
pub fn layer<S>(self) -> Layer<S, formatter::FieldsFormatter, formatter::JsonEventFormatter>
where
S: Subscriber + for<'a> LookupSpan<'a>,
{
tracing_subscriber::fmt::layer()
.event_format(self.events)
.fmt_fields(self.fields)
}
pub fn subscriber_builder(
self,
) -> SubscriberBuilder<formatter::FieldsFormatter, formatter::JsonEventFormatter> {
tracing_subscriber::fmt::Subscriber::builder()
.event_format(self.events)
.fmt_fields(self.fields)
}
}
/// Returns a `Layer` that subscribes to all spans and events using a JSON formatter.
/// This is used to configure the JSON formatter.
pub fn layer<S>() -> impl tracing_subscriber::Layer<S>
where
S: Subscriber + for<'a> LookupSpan<'a>,
{
crate::builder().layer()
}
#[cfg(test)]
mod tests {
use super::*;
use tracing::{debug, error, info, info_span, trace, warn};
use tracing_core::Level;
#[test]
fn test_json_event_formatter() {
let formatter = formatter::JsonEventFormatter::new();
let subscriber = tracing_subscriber::fmt()
.fmt_fields(formatter::FieldsFormatter::new())
.event_format(formatter)
.with_max_level(Level::TRACE)
.finish();
tracing::subscriber::with_default(subscriber, || {
trace!(a = "b", "hello world from trace");
debug!("hello world from debug");
info!("hello world from info");
warn!("hello world from warn");
error!("hello world from error");
let span = info_span!("test_span", b = "b", d = "d", later = tracing::field::Empty,);
span.in_scope(|| {
info!("some message from inside a info_span");
});
});
let formatter = formatter::JsonEventFormatter::new()
.with_level_name("severity")
.with_message_name("msg")
.with_timestamp_name("ts")
.with_timestamp_format(TimestampFormat::Unix);
let subscriber = tracing_subscriber::fmt()
.fmt_fields(formatter::FieldsFormatter::new())
.event_format(formatter)
.with_max_level(Level::TRACE)
.finish();
tracing::subscriber::with_default(subscriber, || {
trace!(a = "b", "hello world from trace");
debug!("hello world from debug");
info!("hello world from info");
warn!("hello world from warn");
error!("hello world from error");
let span = info_span!(
"test_span",
person.firstname = "cole",
person.lastname = "mackenzie",
later = tracing::field::Empty,
);
span.in_scope(|| {
info!("some message from inside a info_span");
let inner = info_span!("inner_span", a = "b", c = "d");
inner.in_scope(|| {
info!("some message from inside a info_span");
});
});
});
}
}