Skip to content

Commit 0d92265

Browse files
committed
feat: Syslog-adapted formating and auto-detection
1 parent 595ff06 commit 0d92265

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

src/fmt/mod.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ use std::io::prelude::Write;
6363
use std::rc::Rc;
6464
use std::{fmt, io};
6565

66-
#[cfg(feature = "color")]
6766
use log::Level;
6867
use log::Record;
6968

@@ -244,6 +243,28 @@ impl<T: Display> Display for StyledValue<T> {
244243
#[cfg(not(feature = "color"))]
245244
type StyledValue<T> = T;
246245

246+
pub struct SyslogFormatter;
247+
248+
impl SyslogFormatter {
249+
pub(crate) fn build(&mut self) -> FormatFn {
250+
Box::new(|buf: &mut Formatter, record: &Record<'_>| {
251+
writeln!(
252+
buf,
253+
"<{}>{}: {}",
254+
match record.level() {
255+
Level::Error => 3,
256+
Level::Warn => 4,
257+
Level::Info => 6,
258+
Level::Debug => 7,
259+
Level::Trace => 7,
260+
},
261+
record.target(),
262+
record.args()
263+
)
264+
})
265+
}
266+
}
267+
247268
/// A [custom format][crate::Builder::format] with settings for which fields to show
248269
pub struct ConfigurableFormat {
249270
// This format needs to work with any combination of crate features.
@@ -259,6 +280,13 @@ pub struct ConfigurableFormat {
259280
pub(crate) kv_format: Option<Box<KvFormatFn>>,
260281
}
261282

283+
impl ConfigurableFormat {
284+
/// Convert the format into a callable function.
285+
pub(crate) fn build(&mut self) -> FormatFn {
286+
Box::new(std::mem::take(self))
287+
}
288+
}
289+
262290
impl ConfigurableFormat {
263291
/// Format the [`Record`] as configured for outputting
264292
pub fn format(&self, formatter: &mut Formatter, record: &Record<'_>) -> io::Result<()> {

src/logger.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ pub struct Builder {
4040
writer: writer::Builder,
4141
format: fmt::ConfigurableFormat,
4242
built: bool,
43+
is_syslog: bool,
4344
}
4445

4546
impl Builder {
@@ -160,6 +161,10 @@ impl Builder {
160161
self.parse_write_style(&s);
161162
}
162163

164+
if env.is_daemon() {
165+
self.is_syslog = true;
166+
}
167+
163168
self
164169
}
165170

@@ -291,6 +296,13 @@ impl Builder {
291296
self
292297
}
293298

299+
/// If set to true, format log messages in a Syslog-adapted format.
300+
/// Overrides the auto-detected value.
301+
pub fn format_syslog(&mut self, syslog: bool) -> &mut Self {
302+
self.is_syslog = syslog;
303+
self
304+
}
305+
294306
/// Set the format for structured key/value pairs in the log record
295307
///
296308
/// With the default format, this function is called for each record and should format
@@ -480,7 +492,11 @@ impl Builder {
480492
Logger {
481493
writer: self.writer.build(),
482494
filter: self.filter.build(),
483-
format: Box::new(std::mem::take(&mut self.format)),
495+
format: if self.is_syslog {
496+
fmt::SyslogFormatter.build()
497+
} else {
498+
self.format.build()
499+
},
484500
}
485501
}
486502

@@ -822,6 +838,11 @@ impl<'a> Env<'a> {
822838
fn get_write_style(&self) -> Option<String> {
823839
self.write_style.get()
824840
}
841+
842+
fn is_daemon(&self) -> bool {
843+
//TODO: support more logging systems
844+
Var::new("JOURNAL_STREAM").get().is_some()
845+
}
825846
}
826847

827848
impl<'a, T> From<T> for Env<'a>

0 commit comments

Comments
 (0)