ckb_logger_config/lib.rs
1//! CKB logger configurations.
2//!
3//! This crate is used to configure the [CKB logger and logging service].
4//!
5//! [CKB logger and logging service]: ../ckb_logger_service/index.html
6
7use std::{collections::HashMap, path::PathBuf};
8
9use serde::{Deserialize, Serialize};
10
11#[cfg(test)]
12mod tests;
13
14/// The whole CKB logger configuration.
15///
16/// This struct is used to build [`Logger`].
17///
18/// Include configurations of the main logger and any number of extra loggers.
19///
20/// [`Logger`]: ../ckb_logger_service/struct.Logger.html
21#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
22#[serde(deny_unknown_fields)]
23pub struct Config {
24 /// An optional string which is used to build [env_logger::Filter] for the main logger.
25 ///
26 /// If the value is `None`, no [env_logger::Filter] will be used.
27 ///
28 /// [env_logger::Filter]: https://docs.rs/env_logger/*/env_logger/filter/struct.Filter.html
29 pub filter: Option<String>,
30 /// Colorize the output which was written into the stdout.
31 #[serde(default = "default_values::color")]
32 pub color: bool,
33 /// The log file of the main logger.
34 #[serde(skip)]
35 pub file: PathBuf,
36 /// The directory where to store all log files.
37 #[serde(skip)]
38 pub log_dir: PathBuf,
39 /// Output the log records of the main logger into a file or not.
40 #[serde(default = "default_values::log_to_file")]
41 pub log_to_file: bool,
42 /// Output the log records of the main logger into the stdout or not.
43 #[serde(default = "default_values::log_to_stdout")]
44 pub log_to_stdout: bool,
45 /// An optional bool to control whether or not emit [Sentry Breadcrumbs].
46 ///
47 /// if the value is `None`, not emit [Sentry Breadcrumbs].
48 ///
49 /// [Sentry Breadcrumbs]: https://sentry.io/features/breadcrumbs/
50 pub emit_sentry_breadcrumbs: Option<bool>,
51 /// Add extra loggers.
52 #[serde(default)]
53 pub extra: HashMap<String, ExtraLoggerConfig>,
54}
55
56/// The configuration of an extra CKB logger.
57///
58/// This struct is used to build [`ExtraLogger`].
59///
60/// [`ExtraLogger`]: ../ckb_logger_service/struct.ExtraLogger.html
61#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
62pub struct ExtraLoggerConfig {
63 /// A string which is used to build [env_logger::Filter] for the extra logger.
64 ///
65 /// [env_logger::Filter]: https://docs.rs/env_logger/*/env_logger/filter/struct.Filter.html
66 pub filter: String,
67}
68
69impl Default for Config {
70 fn default() -> Self {
71 Config {
72 filter: None,
73 color: default_values::color(),
74 file: Default::default(),
75 log_dir: Default::default(),
76 log_to_file: default_values::log_to_file(),
77 log_to_stdout: default_values::log_to_stdout(),
78 emit_sentry_breadcrumbs: None,
79 extra: Default::default(),
80 }
81 }
82}
83
84pub(crate) mod default_values {
85 pub(crate) const fn color() -> bool {
86 !cfg!(windows)
87 }
88
89 pub(crate) const fn log_to_file() -> bool {
90 false
91 }
92
93 pub(crate) const fn log_to_stdout() -> bool {
94 true
95 }
96}