From 6bf7401cbfd5dde5fb9533c2efa0d97d4fb3e490 Mon Sep 17 00:00:00 2001 From: Franklin Date: Mon, 29 Aug 2022 11:22:25 -0400 Subject: [PATCH] Added a default simple logger --- Cargo.lock | 2 ++ Cargo.toml | 3 ++- src/extensions/logger.rs | 17 +++++++++++++++++ src/extensions/mod.rs | 3 ++- src/extensions/typed_response.rs | 32 ++++++++++++++++---------------- src/lib.rs | 1 + src/utils/logger_util.rs | 10 ++++++++++ src/utils/mod.rs | 1 + 8 files changed, 51 insertions(+), 18 deletions(-) create mode 100644 src/extensions/logger.rs create mode 100644 src/utils/logger_util.rs create mode 100644 src/utils/mod.rs diff --git a/Cargo.lock b/Cargo.lock index e86a0a0..1c87324 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -186,6 +186,7 @@ name = "actix-web-utils" version = "0.1.2" dependencies = [ "actix-web", + "log", "serde", "serde_json", ] @@ -609,6 +610,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" dependencies = [ "cfg-if", + "serde", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 8b12d73..a32451f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,4 +11,5 @@ license = "MIT" [dependencies] actix-web = "4.1.0" serde_json = { version = "1" } -serde = { version = "1.0", features = ["derive"] } \ No newline at end of file +serde = { version = "1.0", features = ["derive"] } +log = { version = "0.4", features = ["serde"] } diff --git a/src/extensions/logger.rs b/src/extensions/logger.rs new file mode 100644 index 0000000..e0029fe --- /dev/null +++ b/src/extensions/logger.rs @@ -0,0 +1,17 @@ +use log::{Record, Level, Metadata}; + +pub struct SimpleLogger; + +impl log::Log for SimpleLogger { + fn enabled(&self, metadata: &Metadata) -> bool { + metadata.level() <= Level::Info + } + + fn log(&self, record: &Record) { + if self.enabled(record.metadata()) { + println!("{} - {}", record.level(), record.args()); + } + } + + fn flush(&self) {} +} \ No newline at end of file diff --git a/src/extensions/mod.rs b/src/extensions/mod.rs index ddfd02f..e4d56db 100644 --- a/src/extensions/mod.rs +++ b/src/extensions/mod.rs @@ -1 +1,2 @@ -pub mod typed_response; \ No newline at end of file +pub mod typed_response; +pub mod logger; \ No newline at end of file diff --git a/src/extensions/typed_response.rs b/src/extensions/typed_response.rs index 109e8d7..b2e23d8 100644 --- a/src/extensions/typed_response.rs +++ b/src/extensions/typed_response.rs @@ -3,22 +3,22 @@ use serde::Serialize; use crate::dtos::message::MessageResource; -/// -/// ``` -/// -/// use actix_web::{web::{Path}, http::StatusCode}; -/// use actix_web_utils::extensions::typed_response::TypedHttpResponse; -/// use actix_web_utils::dtos::message::MessageResource; -/// -/// //Sample route -/// pub async fn testroute(number: Path) -> TypedHttpResponse { -/// if(*number > 0){ -/// return TypedHttpResponse::return_standard_response(StatusCode::OK, String::from("This is my test response!")); -/// } -/// TypedHttpResponse::return_empty_response(StatusCode::BAD_REQUEST) -/// } -/// -/// ``` + /// This is what it takes [MessageResource](crate::dtos::message) this is what it returns [MessageResource](crate::dtos::message) + /// ``` + /// + /// use actix_web::{web::{Path}, http::StatusCode}; + /// use actix_web_utils::extensions::typed_response::TypedHttpResponse; + /// use actix_web_utils::dtos::message::MessageResource; + /// + /// //Sample route + /// pub async fn testroute(number: Path) -> TypedHttpResponse { + /// if(*number > 0){ + /// return TypedHttpResponse::return_standard_response(StatusCode::OK, String::from("This is my test response!")); + /// } + /// TypedHttpResponse::return_empty_response(StatusCode::BAD_REQUEST) + /// } + /// + /// ``` pub struct TypedHttpResponse { pub response: HttpResponse>>>>, } diff --git a/src/lib.rs b/src/lib.rs index db510a3..d32b9d0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,2 +1,3 @@ pub mod extensions; pub mod dtos; +pub mod utils; \ No newline at end of file diff --git a/src/utils/logger_util.rs b/src/utils/logger_util.rs new file mode 100644 index 0000000..8bd490b --- /dev/null +++ b/src/utils/logger_util.rs @@ -0,0 +1,10 @@ +use log::{SetLoggerError, LevelFilter}; + +use crate::extensions::logger::SimpleLogger; + +static LOGGER: SimpleLogger = SimpleLogger; + +pub fn init() -> Result<(), SetLoggerError> { + log::set_logger(&LOGGER) + .map(|()| log::set_max_level(LevelFilter::Info)) +} \ No newline at end of file diff --git a/src/utils/mod.rs b/src/utils/mod.rs new file mode 100644 index 0000000..7517331 --- /dev/null +++ b/src/utils/mod.rs @@ -0,0 +1 @@ +pub mod logger_util; \ No newline at end of file