-
Notifications
You must be signed in to change notification settings - Fork 275
/
Copy pathtext.rs
271 lines (241 loc) · 8.39 KB
/
text.rs
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
use std::fmt;
use nu_ansi_term::Color;
use nu_ansi_term::Style;
use opentelemetry::trace::TraceContextExt;
use tracing_core::Event;
use tracing_core::Level;
use tracing_core::Subscriber;
use tracing_subscriber::field;
use tracing_subscriber::field::Visit;
use tracing_subscriber::fmt::format::DefaultVisitor;
use tracing_subscriber::fmt::format::FormatEvent;
use tracing_subscriber::fmt::format::FormatFields;
use tracing_subscriber::fmt::format::Writer;
use tracing_subscriber::fmt::time::FormatTime;
use tracing_subscriber::fmt::time::SystemTime;
use tracing_subscriber::fmt::FmtContext;
use tracing_subscriber::registry::LookupSpan;
#[derive(Debug, Clone)]
pub(crate) struct TextFormatter {
pub(crate) timer: SystemTime,
display_target: bool,
display_filename: bool,
display_line: bool,
}
impl Default for TextFormatter {
fn default() -> Self {
Self::new()
}
}
impl TextFormatter {
const TRACE_STR: &'static str = "TRACE";
const DEBUG_STR: &'static str = "DEBUG";
const INFO_STR: &'static str = "INFO";
const WARN_STR: &'static str = "WARN";
const ERROR_STR: &'static str = "ERROR";
pub(crate) fn new() -> Self {
Self {
timer: Default::default(),
display_target: false,
display_filename: false,
display_line: false,
}
}
pub(crate) fn with_target(self, display_target: bool) -> Self {
Self {
display_target,
..self
}
}
pub(crate) fn with_filename(self, display_filename: bool) -> Self {
Self {
display_filename,
..self
}
}
pub(crate) fn with_line(self, display_line: bool) -> Self {
Self {
display_line,
..self
}
}
#[inline]
fn format_level(&self, level: &Level, writer: &mut Writer<'_>) -> fmt::Result {
if writer.has_ansi_escapes() {
match *level {
Level::TRACE => write!(writer, "{}", Color::Purple.paint(TextFormatter::TRACE_STR)),
Level::DEBUG => write!(writer, "{}", Color::Blue.paint(TextFormatter::DEBUG_STR)),
Level::INFO => write!(writer, "{}", Color::Green.paint(TextFormatter::INFO_STR)),
Level::WARN => write!(writer, "{}", Color::Yellow.paint(TextFormatter::WARN_STR)),
Level::ERROR => write!(writer, "{}", Color::Red.paint(TextFormatter::ERROR_STR)),
}?;
} else {
match *level {
Level::TRACE => write!(writer, "{}", TextFormatter::TRACE_STR),
Level::DEBUG => write!(writer, "{}", TextFormatter::DEBUG_STR),
Level::INFO => write!(writer, "{}", TextFormatter::INFO_STR),
Level::WARN => write!(writer, "{}", TextFormatter::WARN_STR),
Level::ERROR => write!(writer, "{}", TextFormatter::ERROR_STR),
}?;
}
writer.write_char(' ')
}
#[inline]
fn format_timestamp(&self, writer: &mut Writer<'_>) -> fmt::Result {
if writer.has_ansi_escapes() {
let style = Style::new().dimmed();
write!(writer, "{}", style.prefix())?;
// If getting the timestamp failed, don't bail --- only bail on
// formatting errors.
if self.timer.format_time(writer).is_err() {
writer.write_str("<unknown time>")?;
}
write!(writer, "{}", style.suffix())?;
} else {
// If getting the timestamp failed, don't bail --- only bail on
// formatting errors.
if self.timer.format_time(writer).is_err() {
writer.write_str("<unknown time>")?;
}
}
writer.write_char(' ')
}
#[inline]
fn format_location(&self, event: &Event<'_>, writer: &mut Writer<'_>) -> fmt::Result {
if let (Some(filename), Some(line)) = (event.metadata().file(), event.metadata().line()) {
if writer.has_ansi_escapes() {
let style = Style::new().dimmed();
write!(writer, "{}", style.prefix())?;
if self.display_filename {
write!(writer, "{filename}")?;
}
if self.display_filename && self.display_line {
write!(writer, ":")?;
}
if self.display_line {
write!(writer, "{line}")?;
}
write!(writer, "{}", style.suffix())?;
} else {
write!(writer, "{filename}:{line}")?;
}
writer.write_char(' ')?;
}
Ok(())
}
#[inline]
fn format_target(&self, target: &str, writer: &mut Writer<'_>) -> fmt::Result {
if writer.has_ansi_escapes() {
let style = Style::new().dimmed();
write!(writer, "{}", style.prefix())?;
write!(writer, "{target}:")?;
write!(writer, "{}", style.suffix())?;
} else {
write!(writer, "{target}:")?;
}
writer.write_char(' ')
}
#[inline]
fn format_request_id<S, N>(
&self,
ctx: &FmtContext<'_, S, N>,
writer: &mut Writer<'_>,
event: &Event<'_>,
) -> fmt::Result
where
S: Subscriber + for<'a> LookupSpan<'a>,
N: for<'a> FormatFields<'a> + 'static,
{
let span = event
.parent()
.and_then(|id| ctx.span(id))
.or_else(|| ctx.lookup_current());
if let Some(span) = span {
let ext = span.extensions();
match &ext.get::<tracing_opentelemetry::OtelData>() {
Some(otel_data) => {
let trace_id = otel_data
.builder
.trace_id
.unwrap_or_else(|| otel_data.parent_cx.span().span_context().trace_id());
if writer.has_ansi_escapes() {
let style = Style::new().dimmed();
write!(writer, "{}", style.prefix())?;
write!(writer, "[trace_id={trace_id}]")?;
write!(writer, "{}", style.suffix())?;
} else {
write!(writer, "[trace_id={trace_id}]")?;
}
writer.write_char(' ')?;
}
None => eprintln!("Unable to find OtelData in extensions; this is a bug"),
}
}
Ok(())
}
}
impl<S, N> FormatEvent<S, N> for TextFormatter
where
S: Subscriber + for<'a> LookupSpan<'a>,
N: for<'a> FormatFields<'a> + 'static,
{
fn format_event(
&self,
ctx: &FmtContext<'_, S, N>,
mut writer: Writer<'_>,
event: &Event<'_>,
) -> fmt::Result {
let meta = event.metadata();
self.format_timestamp(&mut writer)?;
self.format_location(event, &mut writer)?;
self.format_level(meta.level(), &mut writer)?;
self.format_request_id(ctx, &mut writer, event)?;
if self.display_target {
self.format_target(meta.target(), &mut writer)?;
}
let mut visitor = CustomVisitor::new(DefaultVisitor::new(writer.by_ref(), true));
event.record(&mut visitor);
writeln!(writer)
}
}
struct CustomVisitor<N>(N);
impl<N> CustomVisitor<N>
where
N: field::Visit,
{
#[allow(dead_code)]
fn new(inner: N) -> Self {
Self(inner)
}
}
// TODO we are now able to filter fields here, for now it's just a passthrough
impl<N> Visit for CustomVisitor<N>
where
N: Visit,
{
fn record_debug(&mut self, field: &tracing_core::Field, value: &dyn fmt::Debug) {
self.0.record_debug(field, value)
}
fn record_str(&mut self, field: &tracing_core::Field, value: &str) {
self.0.record_str(field, value)
}
fn record_error(
&mut self,
field: &tracing_core::Field,
value: &(dyn std::error::Error + 'static),
) {
self.0.record_error(field, value)
}
fn record_f64(&mut self, field: &tracing_core::Field, value: f64) {
self.0.record_f64(field, value)
}
fn record_i64(&mut self, field: &tracing_core::Field, value: i64) {
self.0.record_i64(field, value)
}
fn record_u64(&mut self, field: &tracing_core::Field, value: u64) {
self.0.record_u64(field, value)
}
fn record_bool(&mut self, field: &tracing_core::Field, value: bool) {
self.0.record_bool(field, value)
}
}