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
//! Trait for tagging events with custom messages and levels.
//!
//! # Why use tags
//!
//! Using tags in your application can improve readability by distinguishing
//! between different kinds of trace data such as requests, internal state,
//! or special operations. An error during a network request could mean a
//! timeout occurred, while an error in the internal state could mean
//! corruption. Both are errors, but one should be treated more seriously than
//! the other, and therefore should be easily distinguishable.
//!
//! # Custom macros for applications
//!
//! The first step to using custom tags is to call [`tracing_forest::declare_tags!`]
//! at the root level of your crate for macro hygiene purposes. Then define an
//! `enum` type with variants for each possible log, and finally [deriving] the
//! [`Tag`] trait. Ensure that the visibility is `pub(crate)` so any generated
//! macros have access to it.
//!
//! [`tracing_forest::declare_tags!`]: crate::declare_tags!
//! ```
//! // lib.rs
//! tracing_forest::declare_tags! {
//! use tracing_forest::Tag;
//!
//! #[derive(Tag)]
//! pub(crate) enum MyTag {
//! #[tag(lvl = "trace", msg = "simple")]
//! Simple,
//! #[tag(
//! lvl = "info",
//! msg = "all.features",
//! icon = '🔐',
//! macro = "all_features"
//! )]
//! AllFeatures,
//! }
//! }
//! ```
//! In `enum` types, each variant must be a unit type, and have the `#[tag(..)]`
//! attribute. Similarly, `struct` types must be unit types and have the
//! `#[tag(..)]` attribute above their declaration.
//! The attribute has four arguments that it takes:
//! * `lvl`: The log level at which the log occurs at, like `"trace"` or
//! `"warn"`. This is used to determine the default icon and the log level if
//! a macro is derived.
//! * `msg`: A minimalistic message displayed with the log.
//! * `icon`: An optional character displayed during pretty formatting. Defaults
//! do the icon associated with the level.
//! * `macro`: An optional identifier used to declare a macro that can write
//! logs with the specified tag kind. If not provided, then no macro is
//! generated.
//!
//! If you generate macros, then they can be used throughout your application
//! ```
//! # tracing_forest::declare_tags! {
//! # use tracing_forest::Tag;
//! #
//! # #[derive(Tag)]
//! # pub(crate) enum MyTag {
//! # #[tag(lvl = "trace", msg = "simple")]
//! # Simple,
//! # #[tag(
//! # lvl = "info",
//! # msg = "all.features",
//! # icon = '🔐',
//! # macro = "all_features"
//! # )]
//! # AllFeatures,
//! # }
//! # }
//! #[tracing_forest::main(tag = "MyTag")]
//! fn main() {
//! use tracing_forest::Tag;
//! tracing::trace!(__event_tag = crate::tracing_forest_tag::MyTag::Simple.as_field(), "a simple log");
//! all_features!("all the features wow");
//! }
//! ```
//! ```log
//! TRACE 📍 [simple]: a simple log
//! INFO 🔐 [all.features]: all the features wow
//! ```
//!
//! Tagging works by passing in a field-value pair to [`tracing`]s log macros
//! with the field name `__event_tag`, meaning that this is a reserved name that
//! must not be used for other values, and may panic otherwise.
//!
//! ## Note:
//!
//! Although the [`Tag`] trait is unsafe to implement, it is guaranteed that
//! `Tag::as_field` will retain the same name and input parameter `&self`.
//!
//! [deriving]: tracing_forest_macros::Tag
//!
//! # Example
//!
//! ```
//! tracing_forest::declare_tags! {
//! use tracing_forest::Tag;
//!
//! #[derive(Tag)]
//! pub(crate) enum KanidmTag {
//! #[tag(lvl = "info", msg = "admin.info", macro = "admin_info")]
//! AdminInfo,
//! #[tag(lvl = "warn", msg = "admin.warn", macro = "admin_warn")]
//! AdminWarn,
//! #[tag(lvl = "error", msg = "admin.error", macro = "admin_error")]
//! AdminError,
//! #[tag(lvl = "trace", msg = "request.trace", macro = "request_trace")]
//! RequestTrace,
//! #[tag(lvl = "info", msg = "request.info", macro = "request_info")]
//! RequestInfo,
//! #[tag(lvl = "warn", msg = "request.warn", macro = "request_warn")]
//! RequestWarn,
//! #[tag(lvl = "error", msg = "request.error", macro = "request_error")]
//! RequestError,
//! #[tag(lvl = "trace", msg = "security.access", icon = '🔓', macro = "security_access")]
//! SecurityAccess,
//! #[tag(lvl = "info", msg = "security.info", icon = '🔒', macro = "security_info")]
//! SecurityInfo,
//! #[tag(lvl = "error", msg = "security.critical", icon = '🔐', macro = "security_critical")]
//! SecurityCritical,
//! #[tag(lvl = "trace", msg = "filter.trace", macro = "filter_trace")]
//! FilterTrace,
//! #[tag(lvl = "info", msg = "filter.info", macro = "filter_info")]
//! FilterInfo,
//! #[tag(lvl = "warn", msg = "filter.warn", macro = "filter_warn")]
//! FilterWarn,
//! #[tag(lvl = "error", msg = "filter.error", macro = "filter_error")]
//! FilterError,
//! }
//! }
//! ```
use crate::;
use Level;
/// A type that can tag events with custom messages.
///
/// # Safety
///
/// This trait is unsafe to implement as the method signatures are subject to
/// change. Instead, [derive][tracing_forest_macros::Tag] it to ensure a correct
/// implementation.
///
/// See [module level documentation][self] for how to use [`Tag`]s.
pub unsafe
pub type TagParser = fn ;
/// The type that all tags resolve to once collected.
cfg_json!
/// The absense of a [`Tag`].
unsafe
cfg_derive!