From 0dd2abe62348d68e09311a427dc715dcf459d282 Mon Sep 17 00:00:00 2001 From: Jose Quintana Date: Mon, 13 Apr 2020 14:26:33 +0200 Subject: [PATCH] feat: configurable logging levels (resolves #16) --- .gitignore | 5 +++++ Makefile | 10 ++++++---- README.md | 2 ++ Tasks.Dev.toml | 3 +++ src/config.rs | 3 +++ src/logger.rs | 33 +++++++++++++++++++++++++++++++++ src/main.rs | 42 +++++++++--------------------------------- 7 files changed, 61 insertions(+), 37 deletions(-) diff --git a/.gitignore b/.gitignore index 5e01fdd..baba3f0 100644 --- a/.gitignore +++ b/.gitignore @@ -6,5 +6,10 @@ **/*.bin **/*.iml **/.idea +**/*.txt +**/*.zip +**/*.tar.gz +**/*.tar +**/*.gz **/.DS_Store release diff --git a/Makefile b/Makefile index 72f5cff..8a7f80e 100644 --- a/Makefile +++ b/Makefile @@ -25,12 +25,14 @@ install: .PHONY: install run: + @rustc -vV @cargo make --makefile Tasks.Dev.toml run .PHONY: run -watch: +dev: + @rustc -vV @cargo make --makefile Tasks.Dev.toml watch -.PHONY: watch +.PHONY: dev build: @rustc -vV @@ -214,8 +216,8 @@ promote: .PHONY: promote loadtest: - @echo "GET http://localhost:1234" | \ - vegeta -cpus=12 attack -workers=10 -duration=5s -connections=10000 -rate=200 -http2=false > results.bin + @echo "GET http://localhost:8787" | \ + vegeta -cpus=12 attack -workers=10 -duration=60s -connections=10000 -rate=200 -http2=false > results.bin @cat results.bin | vegeta report -type='hist[0,2ms,4ms,6ms]' @cat results.bin | vegeta plot > plot.html .PHONY: loadtest diff --git a/README.md b/README.md index 48d8b1a..11ee157 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,7 @@ Server can be configured either via environment variables or their equivalent co | `SERVER_ERROR_PAGE_404` | HTML file path for 404 errors. | If path is not specified or simply don't exists then server will use a generic HTML error message. Default `./public/404.html`. | `SERVER_ERROR_PAGE_50X` | HTML file path for 50x errors. | If path is not specified or simply don't exists then server will use a generic HTML error message. Default `./public/50x.html` | | `SERVER_TLS` | Enables TLS/SSL support. Make sure also to adjust current server port. | Default `false` | +| `SERVER_LOG_LEVEL` | Specify a logging level in lower case (see [log::LevelFilter](https://docs.rs/log/0.4.10/log/enum.LevelFilter.html)). | Default `error` | | `SERVER_TLS_PKCS12` | A cryptographic identity [PKCS #12](https://docs.rs/native-tls/0.2.3/native_tls/struct.Identity.html#method.from_pkcs12) bundle file path containing a [X509 certificate](https://en.wikipedia.org/wiki/X.509) along with its corresponding private key and chain of certificates to a trusted root. | Default empty | | `SERVER_TLS_PKCS12_PASSWD` | A specified password to decrypt the private key. | Default empty | @@ -64,6 +65,7 @@ OPTIONS: Assets directory path for add cache headers functionality [env: SERVER_ASSETS=] [default: ./assets] --host Host address (E.g 127.0.0.1) [env: SERVER_HOST=] [default: [::]] + --log-level Specify a logging level in lower case [env: SERVER_LOG_LEVEL=] [default: error] --name Name for server [env: SERVER_NAME=] [default: my-static-server] --page404 HTML file path for 404 errors. If path is not specified or simply don't exists then server will use a diff --git a/Tasks.Dev.toml b/Tasks.Dev.toml index 622af66..625d7a7 100644 --- a/Tasks.Dev.toml +++ b/Tasks.Dev.toml @@ -1,6 +1,9 @@ [env] E_ARGS = "--port=8787" E_URL = "http://locahost" +SERVER_LOG_LEVEL = "trace" +SERVER_ROOT = "./public" +SERVER_ASSETS = "./assets" [tasks.watch] command = "cargo" diff --git a/src/config.rs b/src/config.rs index b571c33..848364b 100644 --- a/src/config.rs +++ b/src/config.rs @@ -41,4 +41,7 @@ pub struct Options { #[structopt(long, default_value = "", env = "SERVER_TLS_PKCS12_PASSWD")] /// A specified password to decrypt the private key. pub tls_pkcs12_passwd: String, + #[structopt(long, default_value = "error", env = "SERVER_LOG_LEVEL")] + /// Specify a logging level in lower case. + pub log_level: String, } diff --git a/src/logger.rs b/src/logger.rs index 707544a..07eb2f3 100644 --- a/src/logger.rs +++ b/src/logger.rs @@ -1,5 +1,38 @@ +use chrono::Local; +use env_logger::Builder; use iron::prelude::*; use iron::AfterMiddleware; +use log::LevelFilter; +use std::io::Write; + +/// Initialize logging builder and format +pub fn init(log_level_str: &str) { + let log_level = match log_level_str { + "off" => LevelFilter::Off, + "error" => LevelFilter::Error, + "warn" => LevelFilter::Warn, + "info" => LevelFilter::Info, + "debug" => LevelFilter::Debug, + "trace" => LevelFilter::Trace, + _ => { + println!("Log level \"{}\" is not supported", log_level_str); + std::process::exit(1); + } + }; + + Builder::new() + .filter_level(log_level) + .format(|buf, record| { + writeln!( + buf, + "{} [{}] - {}", + Local::now().format("%Y-%m-%dT%H:%M:%S"), + record.level(), + record.args() + ) + }) + .init(); +} pub struct Logger; diff --git a/src/main.rs b/src/main.rs index 08c6c61..c649cdd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,15 +1,11 @@ -extern crate chrono; -extern crate env_logger; -extern crate flate2; -extern crate hyper_native_tls; -extern crate iron; -extern crate iron_staticfile_middleware; -extern crate nix; -extern crate signal; - #[macro_use] extern crate log; -extern crate structopt; + +use crate::config::Options; +use hyper_native_tls::NativeTlsServer; +use iron::prelude::*; +use staticfiles::*; +use structopt::StructOpt; mod config; mod error_page; @@ -18,16 +14,6 @@ mod logger; mod signal_manager; mod staticfiles; -use crate::config::Options; -use chrono::Local; -use env_logger::Builder; -use hyper_native_tls::NativeTlsServer; -use iron::prelude::*; -use log::LevelFilter; -use staticfiles::*; -use std::io::Write; -use structopt::StructOpt; - fn on_server_running(server_name: &str, proto: &str, addr: &str) { // Notify when server is running info!( @@ -46,20 +32,10 @@ fn on_server_running(server_name: &str, proto: &str, addr: &str) { } fn main() { - Builder::new() - .format(|buf, record| { - writeln!( - buf, - "{} [{}] - {}", - Local::now().format("%Y-%m-%dT%H:%M:%S"), - record.level(), - record.args() - ) - }) - .filter(None, LevelFilter::Info) - .init(); - let opts = Options::from_args(); + + logger::init(&opts.log_level); + let addr = &format!("{}{}{}", opts.host.to_string(), ":", opts.port.to_string()); let proto = if opts.tls { "HTTPS" } else { "HTTP" }; -- libgit2 1.7.2