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
#[cfg(feature = "prima-logger-json")]
use crate::json::formatter::DefaultEventFormatter;
use std::error::Error;
use std::fmt::{Debug, Display, Formatter};
use std::str::FromStr;
#[cfg(not(feature = "prima-logger-json"))]
use crate::subscriber::NopEventFormatter;
pub struct SubscriberConfig<T> {
pub env: Environment,
pub telemetry: Option<TelemetryConfig>,
pub service: String,
pub version: Option<String>,
pub json_formatter: T,
}
impl<T> SubscriberConfig<T> {
pub(crate) fn new(
service: String,
version: Option<String>,
json_formatter: T,
) -> SubscriberConfig<T> {
SubscriberConfig {
env: Environment::Dev,
telemetry: None,
service,
version,
json_formatter,
}
}
}
#[cfg(not(feature = "prima-logger-json"))]
pub fn builder(service: &str) -> SubscriberConfigBuilder<NopEventFormatter> {
SubscriberConfigBuilder(SubscriberConfig::new(
service.to_owned(),
None,
NopEventFormatter {},
))
}
#[cfg(feature = "prima-logger-json")]
pub fn builder(service: &str) -> SubscriberConfigBuilder<DefaultEventFormatter> {
SubscriberConfigBuilder(SubscriberConfig::new(
service.to_owned(),
None,
DefaultEventFormatter {},
))
}
pub struct TelemetryConfig {
pub collector_url: String,
pub service_name: String,
}
#[derive(PartialEq, Debug, Clone, Copy)]
pub enum Environment {
Dev,
Qa,
Staging,
Production,
}
impl FromStr for Environment {
type Err = EnvironmentParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"dev" => Ok(Self::Dev),
"qa" => Ok(Self::Qa),
"staging" => Ok(Self::Staging),
"production" => Ok(Self::Production),
_ => Err(EnvironmentParseError(s.to_string())),
}
}
}
impl Display for Environment {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let str = match self {
Self::Dev => "dev",
Self::Qa => "qa",
Self::Staging => "staging",
Self::Production => "production",
};
f.write_str(str)
}
}
#[derive(Debug)]
pub struct EnvironmentParseError(String);
impl Display for EnvironmentParseError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!(
"{} is not a valid environment string. Allowed strings are 'dev', 'qa', 'staging' and 'production'.",
&self.0
))
}
}
impl Error for EnvironmentParseError {}
pub struct SubscriberConfigBuilder<T>(SubscriberConfig<T>);
impl<T> SubscriberConfigBuilder<T> {
pub fn build(self) -> SubscriberConfig<T> {
self.0
}
pub fn with_env(mut self, env: Environment) -> Self {
self.0.env = env;
self
}
pub fn with_version(mut self, version: String) -> Self {
self.0.version = Some(version);
self
}
pub fn with_telemetry(mut self, collector_url: String, service_name: String) -> Self {
self.0.telemetry = Some(TelemetryConfig {
collector_url,
service_name,
});
self
}
pub fn with_custom_json_formatter<F>(self, formatter: F) -> SubscriberConfigBuilder<F> {
SubscriberConfigBuilder(SubscriberConfig {
json_formatter: formatter,
env: self.0.env,
service: self.0.service,
version: self.0.version,
telemetry: self.0.telemetry,
})
}
}