Skip to content

Commit ca110b6

Browse files
authored
Use a visitor pattern instead of FormattedFields (#12)
This changes a lot of the insides of the crate, but the API is mostly the same. While the visitor pattern is a bit more verbose, it's also more flexible and allows for more customization in the future.
1 parent b1dd04d commit ca110b6

File tree

14 files changed

+386
-681
lines changed

14 files changed

+386
-681
lines changed

.github/workflows/rust.yml

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,20 @@ name: Rust
22

33
on:
44
push:
5-
branches: [ "main" ]
5+
branches: ["main"]
66
pull_request:
7-
branches: [ "main" ]
7+
branches: ["main"]
88

99
env:
1010
CARGO_TERM_COLOR: always
1111

1212
jobs:
1313
build:
14-
1514
runs-on: ubuntu-latest
1615

1716
steps:
18-
- uses: actions/checkout@v3
19-
- name: Build
20-
run: cargo build --verbose
21-
- name: Run tests
22-
run: cargo test --verbose
17+
- uses: actions/checkout@v3
18+
- name: Build
19+
run: cargo build --verbose
20+
- name: Run tests
21+
run: cargo test --verbose

.rustfmt.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# rustup install nightly
2+
group_imports = "StdExternalCrate"
3+
format_code_in_doc_comments = true

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "tracing-ndjson"
3-
version = "0.1.3"
3+
version = "0.2.0"
44
edition = "2021"
55
license = "MIT"
66
authors = ["Cole Mackenzie"]

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
fmt:
2-
@cargo fmt
2+
@cargo +nightly fmt || cargo fmt
33

44
build:
55
@cargo build
@@ -8,7 +8,7 @@ clean:
88
@cargo clean
99

1010
test:
11-
@cargo test
11+
@cargo test -- --nocapture
1212

1313
lint: fmt
1414
@cargo clippy --all-targets -- -D warnings

README.md

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,25 @@
44
[![Rust](https://github.com/cmackenzie1/tracing-ndjson/actions/workflows/rust.yml/badge.svg)](https://github.com/cmackenzie1/tracing-ndjson/actions/workflows/rust.yml)
55
[![docs.rs](https://img.shields.io/docsrs/tracing-ndjson)](https://docs.rs/tracing-ndjson/latest/tracing_ndjson)
66

7-
87
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.
98

9+
The goal of this crate is to provide a flattend JSON event, comprising of fields from the span attributes and event fields, with customizeable field names and timestamp formats.
10+
1011
## Features
1112

1213
- Configurable field names for `target`, `message`, `level`, and `timestamp`.
13-
- Configurable timestamp formats such as RFC3339, UNIX timestamp, or any custom chrono format.
14-
- Captures all span attributes and event fields in the root of the JSON object.
14+
- Configurable timestamp formats
15+
- RFC3339 (`2023-10-08T03:30:52Z`),
16+
- RFC339Nanos (`2023-10-08T03:30:52.123456789Z`)
17+
- Unix timestamp (`1672535452`)
18+
- UnixMills (`1672535452123`)
19+
- Captures all span attributes and event fields in the root of the JSON object. Collisions will result in overwriting the existing field.
20+
21+
## Limitations
22+
23+
- When flattening span attributes and event fields, the library will overwrite any existing fields with the same name, including the built-in fields such as `target`, `message`, `level`, `timestamp`, `file`, and `line`.
24+
- Non-determistic ordering of fields in the JSON object. ([JSON objects are unordered](https://www.json.org/json-en.html))
25+
- Currently only logs to stdout. (PRs welcome!)
1526

1627
## Usage
1728

@@ -20,24 +31,24 @@ Add this to your `Cargo.toml`:
2031
```toml
2132
[dependencies]
2233
tracing = "0.1"
23-
tracing-ndjson = "0.1"
34+
tracing-ndjson = "0.2"
2435
```
2536

2637
```rust
2738
use tracing_subscriber::prelude::*;
2839

2940
fn main() {
30-
tracing_subscriber::registry()
31-
.with(tracing_ndjson::builder().layer())
32-
.init();
41+
let subscriber = tracing_subscriber::registry().with(tracing_ndjson::layer());
42+
43+
tracing::subscriber::set_global_default(subscriber).unwrap();
3344

3445
tracing::info!(life = 42, "Hello, world!");
35-
// {"level":"info","timestamp":"2023-10-10T20:35:26Z","target":"defaults","message":"Hello, world!","life":42}
46+
// {"level":"info","target":"default","life":42,"timestamp":"2023-10-20T21:17:49Z","message":"Hello, world!"}
3647

3748
let span = tracing::info_span!("hello", "request.uri" = "https://example.com");
3849
span.in_scope(|| {
3950
tracing::info!("Hello, world!");
40-
// {"level":"info","timestamp":"2023-10-10T20:35:26Z","target":"defaults","message":"Hello, world!","request.uri":"https://example.com"}
51+
// {"message":"Hello, world!","request.uri":"https://example.com","level":"info","target":"default","timestamp":"2023-10-20T21:17:49Z"}
4152
});
4253
}
4354
```

examples/README.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,11 @@
33
## Defaults
44

55
```bash
6-
cargo run --example defaults
7-
8-
{"level":"info","timestamp":"2023-10-08T03:35:11Z","target":"defaults","message":"Hello, world!","life":42}
9-
{"level":"info","timestamp":"2023-10-08T03:35:11Z","target":"defaults","message":"Hello, world!","request.uri":"https://example.com"}
6+
cargo run --example default
107
```
118

129
## Customized
1310

1411
```bash
1512
cargo run --example customize
16-
17-
{"severity":"info","ts":1696736208,"target":"customize","message":"Hello, world!","life":42}
18-
{"severity":"info","ts":1696736208,"target":"customize","message":"Hello, world!","request.uri":"https://example.com"}
1913
```

examples/customize.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
use tracing_subscriber::prelude::*;
2-
32
fn main() {
4-
tracing_subscriber::registry()
5-
.with(
6-
tracing_ndjson::builder()
7-
.with_level_name("severity")
8-
.with_message_name("msg")
9-
.with_timestamp_name("ts")
10-
.with_timestamp_format(tracing_ndjson::TimestampFormat::Unix)
11-
.layer(),
12-
)
13-
.init();
3+
let subscriber = tracing_subscriber::registry().with(
4+
tracing_ndjson::builder()
5+
.with_level_name("severity")
6+
.with_level_value_casing(tracing_ndjson::Casing::Uppercase)
7+
.with_timestamp_name("ts")
8+
.with_timestamp_format(tracing_ndjson::TimestampFormat::UnixMillis)
9+
.with_message_name("msg")
10+
.with_line_numbers(true)
11+
.with_file_names(true)
12+
.layer(),
13+
);
14+
15+
tracing::subscriber::set_global_default(subscriber).unwrap();
1416

1517
tracing::info!(life = 42, "Hello, world!");
16-
// {"level":"info","timestamp":"2023-10-08T03:30:52Z","target":"default","message":"Hello, world!"}
18+
// {"life":42,"msg":"Hello, world!","target":"customize","ts":1697836630814,"file":"examples/customize.rs","line":17,"severity":"INFO"}
1719

1820
let span = tracing::info_span!("hello", "request.uri" = "https://example.com");
1921
span.in_scope(|| {
2022
tracing::info!("Hello, world!");
21-
// {"level":"info","timestamp":"2023-10-08T03:34:33Z","target":"defaults","message":"Hello, world!","request.uri":"https://example.com"}
23+
// {"severity":"INFO","target":"customize","file":"examples/customize.rs","msg":"Hello, world!","ts":1697836630814,"line":22,"request.uri":"https://example.com"}
2224
});
2325
}

examples/default.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use tracing_subscriber::prelude::*;
2+
fn main() {
3+
let subscriber = tracing_subscriber::registry().with(tracing_ndjson::layer());
4+
5+
tracing::subscriber::set_global_default(subscriber).unwrap();
6+
7+
tracing::info!(life = 42, "Hello, world!");
8+
// {"level":"info","target":"default","life":42,"timestamp":"2023-10-20T21:17:49Z","message":"Hello, world!"}
9+
10+
let span = tracing::info_span!("hello", "request.uri" = "https://example.com");
11+
span.in_scope(|| {
12+
tracing::info!("Hello, world!");
13+
// {"message":"Hello, world!","request.uri":"https://example.com","level":"info","target":"default","timestamp":"2023-10-20T21:17:49Z"}
14+
});
15+
}

examples/defaults.rs

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)