Added a default simple logger
This commit is contained in:
parent
5919ac340c
commit
6bf7401cbf
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -186,6 +186,7 @@ name = "actix-web-utils"
|
|||||||
version = "0.1.2"
|
version = "0.1.2"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"actix-web",
|
"actix-web",
|
||||||
|
"log",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
]
|
]
|
||||||
@ -609,6 +610,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e"
|
checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"cfg-if",
|
"cfg-if",
|
||||||
|
"serde",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -11,4 +11,5 @@ license = "MIT"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
actix-web = "4.1.0"
|
actix-web = "4.1.0"
|
||||||
serde_json = { version = "1" }
|
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
17
src/extensions/logger.rs
Normal 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) {}
|
||||||
|
}
|
@ -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 crate::dtos::message::MessageResource;
|
||||||
|
|
||||||
///
|
/// 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::{web::{Path}, http::StatusCode};
|
||||||
/// use actix_web_utils::extensions::typed_response::TypedHttpResponse;
|
/// use actix_web_utils::extensions::typed_response::TypedHttpResponse;
|
||||||
/// use actix_web_utils::dtos::message::MessageResource;
|
/// use actix_web_utils::dtos::message::MessageResource;
|
||||||
///
|
///
|
||||||
/// //Sample route
|
/// //Sample route
|
||||||
/// pub async fn testroute(number: Path<i32>) -> TypedHttpResponse<String> {
|
/// pub async fn testroute(number: Path<i32>) -> TypedHttpResponse<String> {
|
||||||
/// if(*number > 0){
|
/// if(*number > 0){
|
||||||
/// return TypedHttpResponse::return_standard_response(StatusCode::OK, String::from("This is my test response!"));
|
/// return TypedHttpResponse::return_standard_response(StatusCode::OK, String::from("This is my test response!"));
|
||||||
/// }
|
/// }
|
||||||
/// TypedHttpResponse::return_empty_response(StatusCode::BAD_REQUEST)
|
/// TypedHttpResponse::return_empty_response(StatusCode::BAD_REQUEST)
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
pub struct TypedHttpResponse<B: Serialize = String> {
|
pub struct TypedHttpResponse<B: Serialize = String> {
|
||||||
pub response: HttpResponse<Option<web::Json<Result<B, Vec<MessageResource>>>>>,
|
pub response: HttpResponse<Option<web::Json<Result<B, Vec<MessageResource>>>>>,
|
||||||
}
|
}
|
||||||
|
@ -1,2 +1,3 @@
|
|||||||
pub mod extensions;
|
pub mod extensions;
|
||||||
pub mod dtos;
|
pub mod dtos;
|
||||||
|
pub mod utils;
|
10
src/utils/logger_util.rs
Normal file
10
src/utils/logger_util.rs
Normal 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
1
src/utils/mod.rs
Normal file
@ -0,0 +1 @@
|
|||||||
|
pub mod logger_util;
|
Loading…
Reference in New Issue
Block a user