Skip to content

Commit

Permalink
chore: Add implementation of logger (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajaykumargdr authored Feb 23, 2024
1 parent 246aaca commit b87fb09
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
11 changes: 11 additions & 0 deletions runtime/lite/modules/logger/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
4 changes: 4 additions & 0 deletions runtime/lite/modules/logger/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub mod logger;
pub mod traits;
pub use logger::*;
pub use traits::*;
55 changes: 55 additions & 0 deletions runtime/lite/modules/logger/src/logger.rs
Original file line number Diff line number Diff line change
@@ -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:?}");
}
}
6 changes: 6 additions & 0 deletions runtime/lite/modules/logger/src/traits.rs
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit b87fb09

Please sign in to comment.