Skip to content

Commit

Permalink
feat: Use the calling crate's name and version for init_logging() (#461)
Browse files Browse the repository at this point in the history
Closes: SYNC-3951
  • Loading branch information
jrconlin authored Oct 4, 2023
1 parent aaa4713 commit d3bd4c0
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
7 changes: 6 additions & 1 deletion autoconnect/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ async fn main() -> Result<()> {
}
let settings =
Settings::with_env_and_config_files(&filenames).map_err(ApcErrorKind::ConfigError)?;
logging::init_logging(!settings.human_logs).expect("Logging failed to initialize");
logging::init_logging(
!settings.human_logs,
env!("CARGO_PKG_NAME"),
env!("CARGO_PKG_VERSION"),
)
.expect("Logging failed to initialize");
debug!("Starting up autoconnect...");

// Sentry requires the environment variable "SENTRY_DSN".
Expand Down
7 changes: 6 additions & 1 deletion autoendpoint/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ async fn main() -> Result<(), Box<dyn Error>> {
.unwrap_or_else(|e| e.exit());
let settings = settings::Settings::with_env_and_config_file(&args.flag_config)?;
let host_port = format!("{}:{}", &settings.host, &settings.port);
logging::init_logging(!settings.human_logs).expect("Logging failed to initialize");
logging::init_logging(
!settings.human_logs,
env!("CARGO_PKG_NAME"),
env!("CARGO_PKG_VERSION"),
)
.expect("Logging failed to initialize");
debug!("Starting up autoendpoint...");

let _sentry = sentry::init(sentry::ClientOptions {
Expand Down
10 changes: 3 additions & 7 deletions autopush-common/src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,15 @@ use crate::errors::Result;

static EC2_INSTANCE_ID: OnceLock<Option<String>> = OnceLock::new();

pub fn init_logging(json: bool) -> Result<()> {
pub fn init_logging(json: bool, name: &str, version: &str) -> Result<()> {
let logger = if json {
let ec2_instance_id = EC2_INSTANCE_ID.get_or_init(|| get_ec2_instance_id().ok());
let hostname = ec2_instance_id
.clone()
.unwrap_or_else(|| gethostname().to_string_lossy().to_string());
let drain = MozLogJson::new(io::stdout())
.logger_name(format!(
"{}-{}",
env!("CARGO_PKG_NAME"),
env!("CARGO_PKG_VERSION")
))
.msg_type(format!("{}:log", env!("CARGO_PKG_NAME")))
.logger_name(format!("{}-{}", name, version))
.msg_type(format!("{}:log", name))
.hostname(hostname)
.build()
.fuse();
Expand Down
7 changes: 6 additions & 1 deletion autopush/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,12 @@ impl AutopushServer {
}

pub fn start(&self) {
logging::init_logging(!self.app_state.human_logs).expect("init_logging failed");
logging::init_logging(
!self.app_state.human_logs,
env!("CARGO_PKG_NAME"),
env!("CARGO_PKG_VERSION"),
)
.expect("init_logging failed");
let handles = Server::start(&self.app_state).expect("failed to start server");
self.shutdown_handles.set(Some(handles));
}
Expand Down

0 comments on commit d3bd4c0

Please sign in to comment.