From cc1bbd9bca5ed4a8b58d293075926fdb44198497 Mon Sep 17 00:00:00 2001 From: ajaykumargdr Date: Fri, 23 Feb 2024 19:37:19 +0530 Subject: [PATCH] chore: Add implementation of logger --- runtime/lite/modules/logger/Cargo.toml | 11 +++++ runtime/lite/modules/logger/src/lib.rs | 4 ++ runtime/lite/modules/logger/src/logger.rs | 55 +++++++++++++++++++++++ runtime/lite/modules/logger/src/traits.rs | 6 +++ 4 files changed, 76 insertions(+) create mode 100644 runtime/lite/modules/logger/Cargo.toml create mode 100644 runtime/lite/modules/logger/src/lib.rs create mode 100644 runtime/lite/modules/logger/src/logger.rs create mode 100644 runtime/lite/modules/logger/src/traits.rs diff --git a/runtime/lite/modules/logger/Cargo.toml b/runtime/lite/modules/logger/Cargo.toml new file mode 100644 index 00000000..49f0f8dd --- /dev/null +++ b/runtime/lite/modules/logger/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "logger" +authors.workspace = true +edition.workspace = true +repository.workspace = true +version.workspace = true + +[dependencies] +slog = "2.7.0" +slog-async = "2.8.0" +slog-term = "2.9.1" \ No newline at end of file diff --git a/runtime/lite/modules/logger/src/lib.rs b/runtime/lite/modules/logger/src/lib.rs new file mode 100644 index 00000000..05df283a --- /dev/null +++ b/runtime/lite/modules/logger/src/lib.rs @@ -0,0 +1,4 @@ +pub mod logger; +pub mod traits; +pub use logger::*; +pub use traits::*; diff --git a/runtime/lite/modules/logger/src/logger.rs b/runtime/lite/modules/logger/src/logger.rs new file mode 100644 index 00000000..d1dc7747 --- /dev/null +++ b/runtime/lite/modules/logger/src/logger.rs @@ -0,0 +1,55 @@ +use super::*; +extern crate slog; +extern crate slog_async; +extern crate slog_term; + +use slog::Drain; + +#[derive(Clone, Debug)] +pub struct CoreLogger { + logger: slog::Logger, +} + +impl CoreLogger { + pub fn new() -> CoreLogger { + use std::fs::OpenOptions; + + let file = OpenOptions::new() + .create(true) + .write(true) + .append(true) + .open("./workflows.log") + .unwrap(); + + let decorator = slog_term::PlainDecorator::new(file); + let file_drain = slog_term::FullFormat::new(decorator).build().fuse(); + + let decorator = slog_term::TermDecorator::new().build(); + let terminal_drain = slog_term::FullFormat::new(decorator).build().fuse(); + + let drain = slog::Duplicate::new(file_drain, terminal_drain).fuse(); + + let drain = slog_async::Async::new(drain).overflow_strategy(slog_async::OverflowStrategy::Block).build().fuse(); + let logger = slog::Logger::root(drain, slog::o!()); + + CoreLogger { logger } + } +} + +impl Logger for CoreLogger { + fn info(&self, msg: &str) { + slog::info!(self.logger, "{msg:?}"); + } + + fn warn(&self, msg: &str) { + slog::warn!(self.logger, "{msg:?}"); + } + + fn error(&self, msg: &str) { + slog::error!(self.logger, "{msg:?}"); + } + + fn debug(&self, msg: &str) { + slog::debug!(self.logger, "{msg:?}"); + } +} diff --git a/runtime/lite/modules/logger/src/traits.rs b/runtime/lite/modules/logger/src/traits.rs new file mode 100644 index 00000000..9f2c5417 --- /dev/null +++ b/runtime/lite/modules/logger/src/traits.rs @@ -0,0 +1,6 @@ +pub trait Logger { + fn info(&self, msg: &str); + fn warn(&self, msg: &str); + fn error(&self, msg: &str); + fn debug(&self, msg: &str); +}