Skip to content

Commit

Permalink
feat: Add --log-level option to wws
Browse files Browse the repository at this point in the history
  • Loading branch information
sea-grass committed Mar 20, 2024
1 parent 6985faa commit b44a742
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ path = "src/main.rs"
[dependencies]
actix-web = { workspace = true }
anyhow = "1.0.66"
log = "0.4.21"
env_logger = "0.10.0"
clap = { version = "4.0.10", features = ["derive"] }
prettytable-rs = "0.10.0"
Expand Down
28 changes: 23 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ use commands::main::Main;
use commands::runtimes::RuntimesCommands;
use std::io::{Error, ErrorKind};
use std::path::PathBuf;
use std::str::FromStr;
use std::process::exit;
use wws_config::Config;
use wws_project::{identify_type, prepare_project, ProjectType};
use wws_router::Routes;
use wws_server::{serve, ServeOptions};
use log::LevelFilter;

// Arguments
#[derive(Parser, Debug)]
Expand Down Expand Up @@ -45,6 +47,9 @@ pub struct Args {
#[arg(long, short)]
install_runtimes: bool,

#[arg(short, long, value_parser = LevelFilter::from_str, default_value = "info")]
log_level: LevelFilter,

/// Set the commit when using a git repository as project
#[arg(long)]
git_commit: Option<String>,
Expand Down Expand Up @@ -78,7 +83,20 @@ pub struct Args {
async fn main() -> std::io::Result<()> {
let args = Args::parse();

std::env::set_var("RUST_LOG", "actix_web=info");
match std::env::var("RUST_LOG") {
Ok(_) => {},
Err(_) => {
match args.log_level {
LevelFilter::Off => std::env::set_var("RUST_LOG", ""),
LevelFilter::Error => std::env::set_var("RUST_LOG", "actix_web=error"),
LevelFilter::Warn => std::env::set_var("RUST_LOG", "actix_web=warn"),
LevelFilter::Info => std::env::set_var("RUST_LOG", "actix_web=info"),
LevelFilter::Debug => std::env::set_var("RUST_LOG", "actix_web=debug"),
LevelFilter::Trace => std::env::set_var("RUST_LOG", "actix_web=trace"),
}
},
}

env_logger::init();

// Check the given subcommand
Expand Down Expand Up @@ -185,14 +203,14 @@ async fn main() -> std::io::Result<()> {
args.port,
route.path,
route.handler.display()
);
);
}

if args.enable_panel {
println!(
"🎛️ The admin panel is available at http://{}:{}/_panel/",
&args.hostname, args.port
);
);
}

let server = serve(ServeOptions {
Expand All @@ -204,12 +222,12 @@ async fn main() -> std::io::Result<()> {
cors_origins: args.cors,
})
.await
.map_err(|err| Error::new(ErrorKind::AddrInUse, err))?;
.map_err(|err| Error::new(ErrorKind::AddrInUse, err))?;

println!(
"🚀 Start serving requests at http://{}:{}\n",
args.hostname, args.port
);
);

// Run the server
server.await
Expand Down

0 comments on commit b44a742

Please sign in to comment.