Skip to main content

rustbasic_core/support/
log.rs

1use crate::logger::{log, Level};
2
3pub struct Log;
4
5impl Log {
6    /// Menulis pesan info ke log (Log::info())
7    pub fn info(msg: impl AsRef<str>) {
8        log(Level::Info, msg.as_ref());
9    }
10
11    /// Menulis pesan debug ke log (Log::debug())
12    pub fn debug(msg: impl AsRef<str>) {
13        log(Level::Debug, msg.as_ref());
14    }
15
16    /// Menulis pesan peringatan ke log (Log::warning())
17    pub fn warning(msg: impl AsRef<str>) {
18        log(Level::Warn, msg.as_ref());
19    }
20
21    /// Menulis pesan error ke log (Log::error())
22    pub fn error(msg: impl AsRef<str>) {
23        log(Level::Error, msg.as_ref());
24    }
25
26    /// Mencatat pesan info dengan konteks JSON (Log::info($msg, $context))
27    pub fn info_with(msg: impl AsRef<str>, context: &serde_json::Value) {
28        let msg_with_ctx = format!("{} - Context: {}", msg.as_ref(), context);
29        log(Level::Info, &msg_with_ctx);
30    }
31
32    /// Mencatat pesan debug dengan konteks JSON (Log::debug($msg, $context))
33    pub fn debug_with(msg: impl AsRef<str>, context: &serde_json::Value) {
34        let msg_with_ctx = format!("{} - Context: {}", msg.as_ref(), context);
35        log(Level::Debug, &msg_with_ctx);
36    }
37
38    /// Mencatat pesan peringatan dengan konteks JSON (Log::warning($msg, $context))
39    pub fn warning_with(msg: impl AsRef<str>, context: &serde_json::Value) {
40        let msg_with_ctx = format!("{} - Context: {}", msg.as_ref(), context);
41        log(Level::Warn, &msg_with_ctx);
42    }
43
44    /// Mencatat pesan error dengan konteks JSON (Log::error($msg, $context))
45    pub fn error_with(msg: impl AsRef<str>, context: &serde_json::Value) {
46        let msg_with_ctx = format!("{} - Context: {}", msg.as_ref(), context);
47        log(Level::Error, &msg_with_ctx);
48    }
49}