Added a default simple logger

This commit is contained in:
Franklin 2022-08-29 11:22:25 -04:00
parent 5919ac340c
commit 6bf7401cbf
8 changed files with 51 additions and 18 deletions

2
Cargo.lock generated
View File

@ -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]]

View File

@ -11,4 +11,5 @@ license = "MIT"
[dependencies]
actix-web = "4.1.0"
serde_json = { version = "1" }
serde = { version = "1.0", features = ["derive"] }
serde = { version = "1.0", features = ["derive"] }
log = { version = "0.4", features = ["serde"] }

17
src/extensions/logger.rs Normal file
View File

@ -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) {}
}

View File

@ -1 +1,2 @@
pub mod typed_response;
pub mod typed_response;
pub mod logger;

View File

@ -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<i32>) -> TypedHttpResponse<String> {
/// 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<i32>) -> TypedHttpResponse<String> {
/// 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<B: Serialize = String> {
pub response: HttpResponse<Option<web::Json<Result<B, Vec<MessageResource>>>>>,
}

View File

@ -1,2 +1,3 @@
pub mod extensions;
pub mod dtos;
pub mod utils;

10
src/utils/logger_util.rs Normal file
View File

@ -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))
}

1
src/utils/mod.rs Normal file
View File

@ -0,0 +1 @@
pub mod logger_util;