about summary refs log tree commit diff
path: root/corp/rih/backend/src/yandex_log.rs
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2023-06-09T13·54+0300
committertazjin <tazjin@tvl.su>2023-06-10T11·23+0000
commit75ffea3fe688ed8b010467ec726522af6391c102 (patch)
tree375845e4a077044843639a3cdef32fe8fd0fb081 /corp/rih/backend/src/yandex_log.rs
parentf72d1f459d5539719ff765a0ad162f40b711b667 (diff)
feat(corp/rih/backend): sprinkle some logging all over the place r/6257
Change-Id: Ifd55a0bf75070b1d47c2d65c32960f05ad7040a0
Reviewed-on: https://cl.tvl.fyi/c/depot/+/8736
Tested-by: BuildkiteCI
Reviewed-by: tazjin <tazjin@tvl.su>
Diffstat (limited to 'corp/rih/backend/src/yandex_log.rs')
-rw-r--r--corp/rih/backend/src/yandex_log.rs47
1 files changed, 47 insertions, 0 deletions
diff --git a/corp/rih/backend/src/yandex_log.rs b/corp/rih/backend/src/yandex_log.rs
new file mode 100644
index 000000000000..64bb4ff97dc5
--- /dev/null
+++ b/corp/rih/backend/src/yandex_log.rs
@@ -0,0 +1,47 @@
+//! Implements a `log::Log` logger that adheres to the structure
+//! expected by Yandex Cloud Serverless logs.
+//!
+//! https://cloud.yandex.ru/docs/serverless-containers/concepts/logs
+
+use log::{Level, Log};
+use serde_json::json;
+
+pub struct YandexCloudLogger;
+
+pub const YANDEX_CLOUD_LOGGER: YandexCloudLogger = YandexCloudLogger;
+
+fn level_map(level: &Level) -> &'static str {
+    match level {
+        Level::Error => "ERROR",
+        Level::Warn => "WARN",
+        Level::Info => "INFO",
+        Level::Debug => "DEBUG",
+        Level::Trace => "TRACE",
+    }
+}
+
+impl Log for YandexCloudLogger {
+    fn enabled(&self, _: &log::Metadata<'_>) -> bool {
+        true
+    }
+
+    fn log(&self, record: &log::Record<'_>) {
+        if !self.enabled(record.metadata()) {
+            return;
+        }
+
+        eprintln!(
+            "{}",
+            json!({
+                "level": level_map(&record.level()),
+                "message": record.args().to_string(),
+                "target": record.target(),
+                "module": record.module_path(),
+                "file": record.file(),
+                "line": record.line(),
+            })
+        );
+    }
+
+    fn flush(&self) {}
+}