-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: split logger config and service
- Loading branch information
1 parent
057b7e2
commit 424562b
Showing
15 changed files
with
150 additions
and
112 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use ckb_app_config::{ExitCode, Setup}; | ||
use ckb_build_info::Version; | ||
use ckb_logger::info_target; | ||
use ckb_logger_service::{self, LoggerInitGuard}; | ||
|
||
pub struct SetupGuard { | ||
_logger_guard: LoggerInitGuard, | ||
_sentry_guard: Option<sentry::internals::ClientInitGuard>, | ||
} | ||
|
||
impl SetupGuard { | ||
pub(crate) fn from_setup(setup: &Setup, version: &Version) -> Result<Self, ExitCode> { | ||
// Initialization of logger must do before sentry, since `logger::init()` and | ||
// `sentry_config::init()` both registers custom panic hooks, but `logger::init()` | ||
// replaces all hooks previously registered. | ||
let mut logger_config = setup.config.logger().to_owned(); | ||
if logger_config.emit_sentry_breadcrumbs.is_none() { | ||
logger_config.emit_sentry_breadcrumbs = Some(setup.is_sentry_enabled); | ||
} | ||
let logger_guard = ckb_logger_service::init(logger_config)?; | ||
|
||
let sentry_guard = if setup.is_sentry_enabled { | ||
let sentry_config = setup.config.sentry(); | ||
|
||
info_target!( | ||
crate::LOG_TARGET_SENTRY, | ||
"**Notice**: \ | ||
The ckb process will send stack trace to sentry on Rust panics. \ | ||
This is enabled by default before mainnet, which can be opted out by setting \ | ||
the option `dsn` to empty in the config file. The DSN is now {}", | ||
sentry_config.dsn | ||
); | ||
|
||
let guard = sentry_config.init(&version); | ||
|
||
sentry::configure_scope(|scope| { | ||
scope.set_tag("subcommand", &setup.subcommand_name); | ||
}); | ||
|
||
Some(guard) | ||
} else { | ||
info_target!(crate::LOG_TARGET_SENTRY, "sentry is disabled"); | ||
None | ||
}; | ||
|
||
Ok(Self { | ||
_logger_guard: logger_guard, | ||
_sentry_guard: sentry_guard, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[package] | ||
name = "ckb-logger-config" | ||
version = "0.36.0-pre" | ||
license = "MIT" | ||
authors = ["Nervos <dev@nervos.org>"] | ||
edition = "2018" | ||
|
||
[dependencies] | ||
serde = { version = "1.0", features = ["derive"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use std::{collections::HashMap, path::PathBuf}; | ||
|
||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Clone, Debug, Serialize, Deserialize)] | ||
pub struct Config { | ||
pub filter: Option<String>, | ||
pub color: bool, | ||
#[serde(skip)] | ||
pub file: PathBuf, | ||
#[serde(skip)] | ||
pub log_dir: PathBuf, | ||
pub log_to_file: bool, | ||
pub log_to_stdout: bool, | ||
pub emit_sentry_breadcrumbs: Option<bool>, | ||
#[serde(default)] | ||
pub extra: HashMap<String, ExtraLoggerConfig>, | ||
} | ||
|
||
#[derive(Clone, Debug, Serialize, Deserialize)] | ||
pub struct ExtraLoggerConfig { | ||
pub filter: String, | ||
} | ||
|
||
impl Default for Config { | ||
fn default() -> Self { | ||
Config { | ||
filter: None, | ||
color: !cfg!(windows), | ||
file: Default::default(), | ||
log_dir: Default::default(), | ||
log_to_file: false, | ||
log_to_stdout: true, | ||
emit_sentry_breadcrumbs: None, | ||
extra: Default::default(), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.