#![warn(missing_docs)]
extern crate log;
#[cfg(feature = "kv_unstable")]
mod kv;
use slog::{b, Level, KV};
use std::{fmt, io};
struct Logger;
fn log_to_slog_level(level: log::Level) -> Level {
match level {
log::Level::Trace => Level::Trace,
log::Level::Debug => Level::Debug,
log::Level::Info => Level::Info,
log::Level::Warn => Level::Warning,
log::Level::Error => Level::Error,
}
}
fn record_as_location(r: &log::Record) -> slog::RecordLocation {
let module = r.module_path_static().unwrap_or("<unknown>");
let file = r.file_static().unwrap_or("<unknown>");
let line = r.line().unwrap_or_default();
slog::RecordLocation {
file,
line,
column: 0,
function: "",
module,
}
}
impl log::Log for Logger {
fn enabled(&self, _: &log::Metadata) -> bool {
true
}
fn log(&self, r: &log::Record) {
let level = log_to_slog_level(r.metadata().level());
let args = r.args();
let target = r.target();
let location = &record_as_location(r);
let s = slog::RecordStatic {
location,
level,
tag: target,
};
#[cfg(feature = "kv_unstable")]
{
let key_values = r.key_values();
let mut visitor = kv::Visitor::new();
key_values.visit(&mut visitor).unwrap();
slog_scope::with_logger(|logger| logger.log(&slog::Record::new(&s, args, b!(visitor))))
}
#[cfg(not(feature = "kv_unstable"))]
slog_scope::with_logger(|logger| logger.log(&slog::Record::new(&s, args, b!())))
}
fn flush(&self) {}
}
pub fn init() -> Result<(), log::SetLoggerError> {
init_with_level(log::Level::max())
}
pub fn init_with_level(level: log::Level) -> Result<(), log::SetLoggerError> {
log::set_boxed_logger(Box::new(Logger))?;
log::set_max_level(level.to_level_filter());
Ok(())
}
pub struct StdLog;
struct LazyLogString<'a> {
info: &'a slog::Record<'a>,
logger_values: &'a slog::OwnedKVList,
}
impl<'a> LazyLogString<'a> {
fn new(info: &'a slog::Record, logger_values: &'a slog::OwnedKVList) -> Self {
LazyLogString {
info,
logger_values,
}
}
}
impl<'a> fmt::Display for LazyLogString<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.info.msg())?;
let io = io::Cursor::new(Vec::new());
let mut ser = Ksv::new(io);
self.logger_values
.serialize(self.info, &mut ser)
.map_err(|_| fmt::Error)?;
self.info
.kv()
.serialize(self.info, &mut ser)
.map_err(|_| fmt::Error)?;
let values = ser.into_inner().into_inner();
write!(f, "{}", String::from_utf8_lossy(&values))
}
}
impl slog::Drain for StdLog {
type Ok = ();
type Err = io::Error;
fn log(&self, info: &slog::Record, logger_values: &slog::OwnedKVList) -> io::Result<()> {
let level = match info.level() {
slog::Level::Critical | slog::Level::Error => log::Level::Error,
slog::Level::Warning => log::Level::Warn,
slog::Level::Info => log::Level::Info,
slog::Level::Debug => log::Level::Debug,
slog::Level::Trace => log::Level::Trace,
};
let mut target = info.tag();
if target.is_empty() {
target = info.module();
}
let lazy = LazyLogString::new(info, logger_values);
log::logger().log(
&log::Record::builder()
.args(format_args!("{}", lazy))
.level(level)
.target(target)
.module_path_static(Some(info.module()))
.file_static(Some(info.file()))
.line(Some(info.line()))
.build(),
);
Ok(())
}
}
struct Ksv<W: io::Write> {
io: W,
}
impl<W: io::Write> Ksv<W> {
fn new(io: W) -> Self {
Ksv { io }
}
fn into_inner(self) -> W {
self.io
}
}
impl<W: io::Write> slog::Serializer for Ksv<W> {
fn emit_arguments(&mut self, key: slog::Key, val: &fmt::Arguments) -> slog::Result {
write!(self.io, ", {}: {}", key, val)?;
Ok(())
}
}