Skip to content

Commit 8cace63

Browse files
committed
feat: use level specified in env
1 parent f67fd7d commit 8cace63

6 files changed

Lines changed: 47 additions & 19 deletions

File tree

src/config/mod.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
//! Reads configs.
22
3-
use std::path::PathBuf;
3+
use std::{fmt::Debug, path::PathBuf};
44

5+
use config_file::FromConfigFile;
56
use serde::Deserialize;
67

78
use crate::env::CONFIG_DIR;
@@ -29,9 +30,33 @@ impl ConfigFile {
2930
}
3031

3132
/// A trait for configs that can be deserialized.
32-
pub trait Config<'de>: Deserialize<'de> {
33+
pub trait Config<'de>: Deserialize<'de> + FromConfigFile {
3334
/// The [`ConfigFile`] this config corresponds to.
3435
fn file() -> ConfigFile;
36+
37+
/// Reads the config from the config file. This function wraps the error logging and returns
38+
/// `None` on failure.
39+
///
40+
/// See: [`FromConfigFile::from_config_file`]
41+
fn read() -> Option<Self>
42+
where
43+
Self: Debug,
44+
{
45+
match Self::from_config_file(Self::file().path()) {
46+
Ok(c) => {
47+
tracing::trace!("read config from file {:?}: {:#?}", Self::file().path(), c);
48+
Some(c)
49+
}
50+
Err(e) => {
51+
tracing::error!(
52+
"failed to read config from file {:?}: {:?}",
53+
Self::file().path(),
54+
e
55+
);
56+
None
57+
}
58+
}
59+
}
3560
}
3661

3762
/// The services config.

src/endpoint/health/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
use std::net::SocketAddr;
44

55
use axum::{extract::ConnectInfo, http::StatusCode, response::IntoResponse};
6-
use tracing::info;
76

87
/// Responds with [`StatusCode::OK`].
98
pub async fn get(ConnectInfo(addr): ConnectInfo<SocketAddr>) -> impl IntoResponse {
10-
info!(
9+
tracing::info!(
1110
"service {} is healthy. responding to {addr}…",
1211
clap::crate_name!()
1312
);

src/endpoint/root.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use api_framework::{
99
};
1010

1111
use axum::{Json, http::StatusCode, response::IntoResponse};
12-
use config_file::FromConfigFile as _;
1312
use serde::Deserialize;
1413

1514
use crate::{
@@ -37,13 +36,12 @@ pub struct PostPayload {
3736
///
3837
/// See: [`PostPayload`], [`post_transaction`]
3938
pub async fn post(Json(payload): Json<PostPayload>) -> impl IntoResponse {
40-
let services = match ServicesConfig::from_config_file(ServicesConfig::file().path()) {
41-
Ok(s) => s,
42-
Err(e) => {
43-
tracing::error!("failed to read services config: {e:?}");
39+
let services = match ServicesConfig::read() {
40+
Some(s) => s,
41+
None => {
4442
return (
4543
StatusCode::INTERNAL_SERVER_ERROR,
46-
format!("failed to read services config: {e:?}"),
44+
"failed to read services config",
4745
)
4846
.into_response();
4947
}

src/env.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use std::{env, path::PathBuf};
44

55
use api_framework::{parse_env, static_lazy_lock};
6+
use tracing::level_filters::LevelFilter;
67

78
/// Sets up environment variables.
89
pub fn setup() {
@@ -33,6 +34,11 @@ static_lazy_lock! {
3334
pub KTT_API_PASSWORD: String = env::var("KTT_API_PASSWORD").expect("KTT_API_PASSWORD not set in environment");
3435
}
3536

37+
static_lazy_lock! {
38+
/// The stderr level for tracing. Defaults to `INFO` if not specified.
39+
pub TRACING_STDERR_LEVEL: LevelFilter = parse_env!("TRACING_STDERR_LEVEL" => |s| s.parse::<LevelFilter>(); anyhow).unwrap_or(LevelFilter::INFO);
40+
}
41+
3642
static_lazy_lock! {
3743
/// The maximum file count to use for tracing.
3844
pub TRACING_MAX_FILES: usize = parse_env!("TRACING_MAX_FILES" => |s| s.parse::<usize>(); anyhow).unwrap_or(5);

src/main.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#![allow(clippy::future_not_send)]
44

55
use crate::env::{
6-
PORT,
6+
PORT, TRACING_STDERR_LEVEL,
77
info::{BUILD_TIMESTAMP, GIT_HASH},
88
};
99

@@ -12,7 +12,6 @@ use std::net::SocketAddr;
1212
use anyhow::{Error, anyhow};
1313
use axum::Router;
1414
use tokio::net::TcpListener;
15-
use tracing::{info, trace};
1615

1716
mod transactions;
1817

@@ -27,19 +26,20 @@ pub mod middleware;
2726
async fn main() {
2827
env::setup();
2928
trace::setup().unwrap();
30-
trace!("loaded environment: {:#?}", std::env::vars());
29+
tracing::info!("stderr is tracing on level {:?}", *TRACING_STDERR_LEVEL);
30+
tracing::trace!("loaded environment: {:#?}", std::env::vars());
3131

32-
info!(
32+
tracing::info!(
3333
"binary {} version {}",
3434
clap::crate_name!(),
3535
clap::crate_version!()
3636
);
37-
info!("compiled from commit {GIT_HASH} at {BUILD_TIMESTAMP}");
38-
info!("starting service on port {}…", *PORT);
37+
tracing::info!("compiled from commit {GIT_HASH} at {BUILD_TIMESTAMP}");
38+
tracing::info!("starting service on port {}…", *PORT);
3939

4040
serve().await.unwrap();
4141

42-
info!("stopping…");
42+
tracing::info!("stopping…");
4343
}
4444

4545
async fn serve() -> Result<(), Error> {

src/trace.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use tracing_subscriber::{
88
Layer as _, fmt::time::ChronoLocal, layer::SubscriberExt as _, util::SubscriberInitExt as _,
99
};
1010

11-
use crate::env::{TRACING_DIR, TRACING_MAX_FILES};
11+
use crate::env::{TRACING_DIR, TRACING_MAX_FILES, TRACING_STDERR_LEVEL};
1212

1313
/// Sets up the logging component, which contains a stderr layer, a rolling file layer, and a latest file layer.
1414
///
@@ -44,7 +44,7 @@ pub fn setup() -> Result<(), Error> {
4444
.with(
4545
stderr_layer
4646
.with_timer(ChronoLocal::rfc_3339())
47-
.with_filter(LevelFilter::INFO),
47+
.with_filter(*TRACING_STDERR_LEVEL),
4848
)
4949
.with(
5050
rolling_file_layer

0 commit comments

Comments
 (0)