1#![deny(warnings)]
2#![warn(unused_extern_crates)]
3#![allow(non_snake_case)]
4use std::{fmt::Display, str::FromStr};
5
6use num_enum::{IntoPrimitive, TryFromPrimitive};
7use serde::Deserialize;
8use tracing_forest::{printer::TestCapturePrinter, tag::NoTag, util::*, Tag};
9use tracing_subscriber::{filter::Directive, prelude::*};
10
11pub mod macros;
12pub mod pipeline;
13
14pub use tracing;
15pub use tracing_forest;
16pub use tracing_subscriber;
17
18#[derive(Clone, Copy, Eq, PartialEq, Debug)]
19pub enum LoggerType {
20 TracingForest,
21 OpenTelemetry,
22}
23
24impl LoggerType {
25 pub fn status_code_field(self) -> &'static str {
26 match self {
27 LoggerType::TracingForest => "status_code",
28 LoggerType::OpenTelemetry => "http.response.status_code",
29 }
30 }
31}
32
33pub fn test_init() {
35 let filter = EnvFilter::builder()
36 .with_default_directive(LevelFilter::INFO.into())
38 .from_env_lossy()
39 .add_directive(
41 "escargot=ERROR"
42 .parse()
43 .expect("failed to generate log filter"),
44 )
45 .add_directive("hyper=INFO".parse().expect("failed to generate log filter"));
47
48 let _ = tracing_subscriber::Registry::default()
50 .with(ForestLayer::new(TestCapturePrinter::new(), NoTag).with_filter(filter))
51 .try_init();
52}
53
54pub fn event_tagger(_event: &Event) -> Option<Tag> {
56 None
57}
58
59#[derive(Debug, Clone, Copy, IntoPrimitive, TryFromPrimitive)]
60#[repr(u64)]
61pub enum EventTag {
62 AdminDebug,
63 AdminError,
64 AdminWarn,
65 AdminInfo,
66 RequestError,
67 RequestWarn,
68 RequestInfo,
69 RequestTrace,
70 SecurityCritical,
71 SecurityDebug,
72 SecurityInfo,
73 SecurityAccess,
74 SecurityError,
75 FilterError,
76 FilterWarn,
77 FilterInfo,
78 FilterTrace,
79 PerfTrace,
80}
81
82impl EventTag {
83 pub fn pretty(self) -> &'static str {
84 match self {
85 EventTag::AdminDebug => "admin.debug",
86 EventTag::AdminError => "admin.error",
87 EventTag::AdminWarn => "admin.warn",
88 EventTag::AdminInfo => "admin.info",
89 EventTag::RequestError => "request.error",
90 EventTag::RequestWarn => "request.warn",
91 EventTag::RequestInfo => "request.info",
92 EventTag::RequestTrace => "request.trace",
93 EventTag::SecurityCritical => "security.critical",
94 EventTag::SecurityDebug => "security.debug",
95 EventTag::SecurityInfo => "security.info",
96 EventTag::SecurityAccess => "security.access",
97 EventTag::SecurityError => "security.error",
98 EventTag::FilterError => "filter.error",
99 EventTag::FilterWarn => "filter.warn",
100 EventTag::FilterInfo => "filter.info",
101 EventTag::FilterTrace => "filter.trace",
102 EventTag::PerfTrace => "perf.trace",
103 }
104 }
105
106 pub fn emoji(self) -> &'static str {
107 use EventTag::*;
108 match self {
109 AdminDebug | SecurityDebug => "đ",
110 AdminError | FilterError | RequestError | SecurityError => "đ¨",
111 AdminWarn | FilterWarn | RequestWarn => "â ī¸",
112 AdminInfo | FilterInfo | RequestInfo | SecurityInfo => "âšī¸",
113 RequestTrace | FilterTrace | PerfTrace => "đ",
114 SecurityCritical => "đ",
115 SecurityAccess => "đ",
116 }
117 }
118}
119
120#[derive(Clone, Copy, Deserialize, Debug, Default)]
121pub enum LogLevel {
122 #[default]
123 #[serde(rename = "info")]
124 Info,
125 #[serde(rename = "debug")]
126 Debug,
127 #[serde(rename = "trace")]
128 Trace,
129}
130
131impl FromStr for LogLevel {
132 type Err = &'static str;
133
134 fn from_str(s: &str) -> Result<Self, Self::Err> {
135 match s.to_ascii_lowercase().as_str() {
136 "info" => Ok(LogLevel::Info),
137 "debug" => Ok(LogLevel::Debug),
138 "trace" => Ok(LogLevel::Trace),
139 _ => Err("Must be one of info, debug, trace"),
140 }
141 }
142}
143
144impl Display for LogLevel {
145 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
146 f.write_str(match self {
147 LogLevel::Info => "info",
148 LogLevel::Debug => "debug",
149 LogLevel::Trace => "trace",
150 })
151 }
152}
153
154impl From<LogLevel> for Directive {
155 fn from(value: LogLevel) -> Self {
156 match value {
157 LogLevel::Info => Directive::from(Level::INFO),
158 LogLevel::Debug => Directive::from(Level::DEBUG),
159 LogLevel::Trace => Directive::from(Level::TRACE),
160 }
161 }
162}