Added a default simple logger
This commit is contained in:
parent
5919ac340c
commit
6bf7401cbf
|
@ -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]]
|
||||
|
|
|
@ -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"] }
|
||||
|
|
|
@ -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) {}
|
||||
}
|
|
@ -1 +1,2 @@
|
|||
pub mod typed_response;
|
||||
pub mod typed_response;
|
||||
pub mod logger;
|
|
@ -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>>>>>,
|
||||
}
|
||||
|
|
|
@ -1,2 +1,3 @@
|
|||
pub mod extensions;
|
||||
pub mod dtos;
|
||||
pub mod utils;
|
|
@ -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))
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
pub mod logger_util;
|
Loading…
Reference in New Issue