-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathlogging.rs
More file actions
173 lines (156 loc) · 4.7 KB
/
logging.rs
File metadata and controls
173 lines (156 loc) · 4.7 KB
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
use std::fmt::{Display, Formatter};
use std::str::FromStr;
use tracing::Metadata;
use tracing::subscriber::Interest;
use tracing_subscriber::filter::LevelFilter;
use tracing_subscriber::layer::{Context, Filter, SubscriberExt};
use tracing_subscriber::util::SubscriberInitExt;
use tracing_subscriber::{Layer, registry};
pub fn setup_cli_subscriber(level: LoggingLevel, kind: LoggingKind) {
if level == LoggingLevel::None {
return;
}
let format = tracing_subscriber::fmt::layer()
.with_level(true)
.with_target(false)
.with_thread_names(true)
.with_file(true)
.with_ansi(true);
match kind {
LoggingKind::Pretty => {
let format = format.pretty();
registry()
.with(format.with_filter(LoggingFilter { level }))
.init()
}
LoggingKind::Compact => {
let format = format.compact();
registry()
.with(format.with_filter(LoggingFilter { level }))
.init()
}
LoggingKind::Json => {
let format = format.json().flatten_event(true);
registry()
.with(format.with_filter(LoggingFilter { level }))
.init()
}
};
}
#[derive(Copy, Debug, Default, Clone, Ord, PartialOrd, Eq, PartialEq)]
pub enum LoggingLevel {
/// No logs should be shown
#[default]
None,
Debug,
Info,
Warn,
Error,
}
impl LoggingLevel {
fn to_filter_level(self) -> Option<LevelFilter> {
match self {
LoggingLevel::None => None,
LoggingLevel::Info => Some(LevelFilter::INFO),
LoggingLevel::Warn => Some(LevelFilter::WARN),
LoggingLevel::Error => Some(LevelFilter::ERROR),
LoggingLevel::Debug => Some(LevelFilter::DEBUG),
}
}
}
impl FromStr for LoggingLevel {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"none" => Ok(Self::None),
"info" => Ok(Self::Info),
"warn" => Ok(Self::Warn),
"error" => Ok(Self::Error),
"debug" => Ok(Self::Debug),
_ => Err("Unexpected value".to_string()),
}
}
}
impl Display for LoggingLevel {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
LoggingLevel::None => write!(f, "none"),
LoggingLevel::Debug => write!(f, "debug"),
LoggingLevel::Info => write!(f, "info"),
LoggingLevel::Warn => write!(f, "warn"),
LoggingLevel::Error => write!(f, "error"),
}
}
}
/// Tracing filter enabling:
/// - All spans and events at level info or higher
/// - All spans and events at level debug in crates whose name starts with `pglt`
struct LoggingFilter {
level: LoggingLevel,
}
/// Tracing filter used for spans emitted by `pglt*` crates
const SELF_FILTER: LevelFilter = if cfg!(debug_assertions) {
LevelFilter::TRACE
} else {
LevelFilter::DEBUG
};
impl LoggingFilter {
fn is_enabled(&self, meta: &Metadata<'_>) -> bool {
let filter = if meta.target().starts_with("pglt") {
if let Some(level) = self.level.to_filter_level() {
level
} else {
return false;
}
} else {
LevelFilter::INFO
};
meta.level() <= &filter
}
}
impl<S> Filter<S> for LoggingFilter {
fn enabled(&self, meta: &Metadata<'_>, _cx: &Context<'_, S>) -> bool {
self.is_enabled(meta)
}
fn callsite_enabled(&self, meta: &'static Metadata<'static>) -> Interest {
if self.is_enabled(meta) {
Interest::always()
} else {
Interest::never()
}
}
fn max_level_hint(&self) -> Option<LevelFilter> {
Some(SELF_FILTER)
}
}
/// The kind of logging
#[derive(Copy, Debug, Default, Clone, Eq, PartialEq)]
pub enum LoggingKind {
/// A pretty log on multiple lines with nice colours
#[default]
Pretty,
/// A more cluttered logging
Compact,
/// Logs are emitted in JSON format
Json,
}
impl Display for LoggingKind {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
LoggingKind::Pretty => write!(f, "pretty"),
LoggingKind::Compact => write!(f, "compact"),
LoggingKind::Json => write!(f, "json"),
}
}
}
impl FromStr for LoggingKind {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"compact" => Ok(Self::Compact),
"pretty" => Ok(Self::Pretty),
"json" => Ok(Self::Json),
_ => Err("This log kind doesn't exist".to_string()),
}
}
}