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
use crate::{counters::STRUCT_LOG_COUNT, Event, Metadata};
use once_cell::sync::OnceCell;
use std::sync::Arc;
use tracing_subscriber::Layer;
static LOGGER: OnceCell<Arc<dyn Logger>> = OnceCell::new();
pub trait Logger: Sync + Send + 'static {
fn enabled(&self, metadata: &Metadata) -> bool;
fn record(&self, event: &Event);
fn flush(&self);
}
pub(crate) fn dispatch(event: &Event) {
if let Some(logger) = LOGGER.get() {
STRUCT_LOG_COUNT.inc();
logger.record(event)
}
}
pub(crate) fn enabled(metadata: &Metadata) -> bool {
LOGGER
.get()
.map(|logger| logger.enabled(metadata))
.unwrap_or(false)
}
pub fn set_global_logger(logger: Arc<dyn Logger>) {
if LOGGER.set(logger).is_err() {
eprintln!("Global logger has already been set");
}
let _ = tracing::subscriber::set_global_default(
crate::tracing_adapter::TracingToDiemLoggerLayer
.with_subscriber(tracing_subscriber::Registry::default()),
);
}
pub fn flush() {
if let Some(logger) = LOGGER.get() {
logger.flush();
}
}