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

@ -12,3 +12,4 @@ license = "MIT"
actix-web = "4.1.0"
serde_json = { version = "1" }
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 logger;

View File

@ -3,7 +3,7 @@ use serde::Serialize;
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};

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;