grafbase_sdk/host_io/logger/
file.rs1use std::path::PathBuf;
4
5use crate::{SdkError, wit};
6
7pub struct FileLogger(wit::FileLogger);
9
10impl FileLogger {
11 pub fn new(path: impl Into<PathBuf>, rotation: Option<LogRotation>) -> Result<Self, SdkError> {
13 let opts = wit::FileLoggerOptions {
14 path: path.into().to_string_lossy().into_owned(),
15 rotate: rotation.map(Into::into),
16 };
17
18 let logger = wit::FileLogger::init(&opts)?;
19
20 Ok(Self(logger))
21 }
22
23 pub fn log_json<S>(&self, message: S) -> Result<(), SdkError>
27 where
28 S: serde::Serialize,
29 {
30 let json = serde_json::to_vec(&message)?;
31 self.log(&json)
32 }
33
34 pub fn log(&self, message: &[u8]) -> Result<(), SdkError> {
36 self.0.log(message)?;
37
38 Ok(())
39 }
40}
41
42pub enum LogRotation {
44 Size(u64),
46 Minutely,
48 Hourly,
50 Daily,
52 Weekly,
54 Monthly,
56 Yearly,
58}
59
60impl From<LogRotation> for wit::FileLoggerRotation {
61 fn from(value: LogRotation) -> Self {
62 match value {
63 LogRotation::Size(size) => wit::FileLoggerRotation::Size(size),
64 LogRotation::Minutely => wit::FileLoggerRotation::Minutely,
65 LogRotation::Hourly => wit::FileLoggerRotation::Hourly,
66 LogRotation::Daily => wit::FileLoggerRotation::Daily,
67 LogRotation::Weekly => wit::FileLoggerRotation::Weekly,
68 LogRotation::Monthly => wit::FileLoggerRotation::Monthly,
69 LogRotation::Yearly => wit::FileLoggerRotation::Yearly,
70 }
71 }
72}