Skip to content

Commit

Permalink
add logs folder config
Browse files Browse the repository at this point in the history
  • Loading branch information
lyang2821 committed Aug 14, 2024
1 parent 24e523c commit d5ceede
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 33 deletions.
47 changes: 46 additions & 1 deletion lapdev-api/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,25 @@ struct Cli {
/// The config file path
#[clap(short, long, action, value_hint = clap::ValueHint::AnyPath)]
config_file: Option<PathBuf>,
/// The folder for putting logs
#[clap(short, long, action, value_hint = clap::ValueHint::AnyPath)]
logs_folder: Option<PathBuf>,
}

pub async fn run(additional_router: Option<Router<CoreState>>) -> Result<()> {
pub async fn start(additional_router: Option<Router<CoreState>>) {
let cli = Cli::parse();

let _result = setup_log(&cli).await;

if let Err(e) = run(&cli, additional_router).await {
tracing::error!("lapdev api start server error: {e:#}");
}
}

async fn run(cli: &Cli, additional_router: Option<Router<CoreState>>) -> Result<()> {
let config_file = cli
.config_file
.clone()
.unwrap_or_else(|| PathBuf::from("/etc/lapdev.conf"));
let config_content = tokio::fs::read_to_string(&config_file)
.await
Expand Down Expand Up @@ -145,3 +158,35 @@ pub async fn run(additional_router: Option<Router<CoreState>>) -> Result<()> {
});
}
}

async fn setup_log(
cli: &Cli,
) -> Result<tracing_appender::non_blocking::WorkerGuard, anyhow::Error> {
let folder = cli
.logs_folder
.clone()
.unwrap_or_else(|| PathBuf::from("/var/lib/lapdev/logs"));
tokio::fs::create_dir_all(&folder).await?;
let file_appender = tracing_appender::rolling::Builder::new()
.max_log_files(30)
.rotation(tracing_appender::rolling::Rotation::DAILY)
.filename_prefix("lapdev.log")
.build(folder)?;
let (non_blocking, guard) = tracing_appender::non_blocking(file_appender);
let filter = tracing_subscriber::EnvFilter::default()
.add_directive("lapdev=info".parse()?)
.add_directive("lapdev_api=info".parse()?)
.add_directive("lapdev_conductor=info".parse()?)
.add_directive("lapdev_rpc=info".parse()?)
.add_directive("lapdev_common=info".parse()?)
.add_directive("lapdev_db=info".parse()?)
.add_directive("lapdev_enterprise=info".parse()?)
.add_directive("lapdev_proxy_ssh=info".parse()?)
.add_directive("lapdev_proxy_http=info".parse()?);
tracing_subscriber::fmt()
.with_ansi(false)
.with_env_filter(filter)
.with_writer(non_blocking)
.init();
Ok(guard)
}
33 changes: 1 addition & 32 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,4 @@
#[tokio::main]
pub async fn main() {
let _result = setup_log().await;

if let Err(e) = lapdev_api::server::run(None).await {
tracing::error!("lapdev api start server error: {e:#}");
}
}

async fn setup_log() -> Result<tracing_appender::non_blocking::WorkerGuard, anyhow::Error> {
let folder = "/var/lib/lapdev/logs";
tokio::fs::create_dir_all(folder).await?;
let file_appender = tracing_appender::rolling::Builder::new()
.max_log_files(30)
.rotation(tracing_appender::rolling::Rotation::DAILY)
.filename_prefix("lapdev.log")
.build(folder)?;
let (non_blocking, guard) = tracing_appender::non_blocking(file_appender);
let filter = tracing_subscriber::EnvFilter::default()
.add_directive("lapdev=info".parse()?)
.add_directive("lapdev_api=info".parse()?)
.add_directive("lapdev_conductor=info".parse()?)
.add_directive("lapdev_rpc=info".parse()?)
.add_directive("lapdev_common=info".parse()?)
.add_directive("lapdev_db=info".parse()?)
.add_directive("lapdev_enterprise=info".parse()?)
.add_directive("lapdev_proxy_ssh=info".parse()?)
.add_directive("lapdev_proxy_http=info".parse()?);
tracing_subscriber::fmt()
.with_ansi(false)
.with_env_filter(filter)
.with_writer(non_blocking)
.init();
Ok(guard)
lapdev_api::server::start(None).await;
}

0 comments on commit d5ceede

Please sign in to comment.