use std::path::PathBuf;
use crate::{SdkError, wit};
pub struct FileLogger(wit::FileLogger);
impl FileLogger {
pub fn new(path: impl Into<PathBuf>, rotation: Option<LogRotation>) -> Result<Self, SdkError> {
let opts = wit::FileLoggerOptions {
path: path.into().to_string_lossy().into_owned(),
rotate: rotation.map(Into::into),
};
let logger = wit::FileLogger::init(&opts)?;
Ok(Self(logger))
}
pub fn log_json<S>(&self, message: S) -> Result<(), SdkError>
where
S: serde::Serialize,
{
let json = serde_json::to_vec(&message)?;
self.log(&json)
}
pub fn log(&self, message: &[u8]) -> Result<(), SdkError> {
self.0.log(message)?;
Ok(())
}
}
pub enum LogRotation {
Size(u64),
Minutely,
Hourly,
Daily,
Weekly,
Monthly,
Yearly,
}
impl From<LogRotation> for wit::FileLoggerRotation {
fn from(value: LogRotation) -> Self {
match value {
LogRotation::Size(size) => wit::FileLoggerRotation::Size(size),
LogRotation::Minutely => wit::FileLoggerRotation::Minutely,
LogRotation::Hourly => wit::FileLoggerRotation::Hourly,
LogRotation::Daily => wit::FileLoggerRotation::Daily,
LogRotation::Weekly => wit::FileLoggerRotation::Weekly,
LogRotation::Monthly => wit::FileLoggerRotation::Monthly,
LogRotation::Yearly => wit::FileLoggerRotation::Yearly,
}
}
}