Skip to content

Commit

Permalink
feat: allow changing the casing of the Level field
Browse files Browse the repository at this point in the history
Users can now choose between `Lowercase` and `Uppercase` for the
casing of the Level fields value.

Example output (pretty-printed for readability):

`Casing::Uppercase`

```json
{
  "severity": "ERROR",
  "ts": 1696892235,
  "target": "tracing_ndjson::tests",
  "fields": {
    "msg": "hello world from error"
  }
}
```

The default remains `Casing::Lowercase`.
  • Loading branch information
cmackenzie1 committed Oct 9, 2023
1 parent e78bb7c commit d855fc7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::{visitor, Error};

const DEFAULT_TIMESTAMP_FORMAT: crate::TimestampFormat = crate::TimestampFormat::Rfc3339;
const DEFAULT_LEVEL_NAME: &str = "level";
const DEFAULT_LEVEL_VALUE_CASING: crate::Casing = crate::Casing::Lowercase;
const DEFAULT_MESSAGE_NAME: &str = "message";
const DEFAULT_TARGET_NAME: &str = "target";
const DEFAULT_TIMESTAMP_NAME: &str = "timestamp";
Expand All @@ -21,6 +22,7 @@ const DEFAULT_FLATTEN_FIELDS: bool = true;
/// This is used to format the event field in the JSON output.
pub struct JsonEventFormatter {
level_name: &'static str,
level_value_casing: crate::Casing,
message_name: &'static str,
target_name: &'static str,
timestamp_name: &'static str,
Expand All @@ -32,6 +34,7 @@ impl Default for JsonEventFormatter {
fn default() -> Self {
Self {
level_name: DEFAULT_LEVEL_NAME,
level_value_casing: DEFAULT_LEVEL_VALUE_CASING,
message_name: DEFAULT_MESSAGE_NAME,
target_name: DEFAULT_TARGET_NAME,
timestamp_name: DEFAULT_TIMESTAMP_NAME,
Expand All @@ -51,6 +54,11 @@ impl JsonEventFormatter {
self
}

pub fn with_level_value_casing(mut self, level_value_casing: crate::Casing) -> Self {
self.level_value_casing = level_value_casing;
self
}

pub fn with_message_name(mut self, message_name: &'static str) -> Self {
self.message_name = message_name;
self
Expand Down Expand Up @@ -93,11 +101,13 @@ where
let mut binding = serde_json::Serializer::new(Vec::new());
let mut serializer = binding.serialize_map(None).map_err(Error::Serde)?;

let level_str = match self.level_value_casing {
crate::Casing::Lowercase => event.metadata().level().to_string().to_lowercase(),
crate::Casing::Uppercase => event.metadata().level().to_string().to_uppercase(),
};

serializer
.serialize_entry(
self.level_name,
&event.metadata().level().to_string().to_lowercase(),
)
.serialize_entry(self.level_name, &level_str)
.map_err(Error::Serde)?;

if matches!(
Expand Down
15 changes: 15 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ impl TimestampFormat {
}
}

pub enum Casing {
Lowercase,
Uppercase,
}

#[derive(Debug, thiserror::Error)]
enum Error {
#[error("fmt error: {0}")]
Expand All @@ -110,6 +115,7 @@ impl From<Error> for std::fmt::Error {
/// This is used to configure the JSON formatter.
/// The default configuration is:
/// * level_name: "level"
/// * level_value_casing: Casing::Lowercase
/// * message_name: "message"
/// * target_name: "target"
/// * timestamp_name: "timestamp"
Expand All @@ -125,6 +131,7 @@ impl From<Error> for std::fmt::Error {
/// .with(
/// tracing_ndjson::Builder::default()
/// .with_level_name("severity")
/// .with_level_value_casing(tracing_ndjson::Casing::Uppercase)
/// .with_message_name("msg")
/// .with_timestamp_name("ts")
/// .with_timestamp_format(tracing_ndjson::TimestampFormat::Unix)
Expand Down Expand Up @@ -166,6 +173,13 @@ impl Builder {
self
}

/// Set the casing for the level field value.
/// The default is Casing::Lowercase.
pub fn with_level_value_casing(mut self, casing: Casing) -> Self {
self.events = self.events.with_level_value_casing(casing);
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 {
Expand Down Expand Up @@ -277,6 +291,7 @@ mod tests {
let subscriber = tracing_subscriber::registry().with(
builder()
.with_level_name("severity")
.with_level_value_casing(Casing::Uppercase)
.with_message_name("msg")
.with_timestamp_name("ts")
.with_timestamp_format(TimestampFormat::Unix)
Expand Down

0 comments on commit d855fc7

Please sign in to comment.