Skip to content

Commit a8f9aea

Browse files
committed
feat: Syslog-adapted formating and auto-detection
1 parent e36452e commit a8f9aea

File tree

2 files changed

+54
-18
lines changed

2 files changed

+54
-18
lines changed

src/fmt/mod.rs

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ pub(crate) struct Builder {
212212
pub(crate) format_suffix: &'static str,
213213
pub(crate) format_file: bool,
214214
pub(crate) format_line_number: bool,
215+
pub(crate) format_syslog: bool,
215216
#[cfg(feature = "unstable-kv")]
216217
pub(crate) kv_format: Option<Box<KvFormatFn>>,
217218
built: bool,
@@ -237,24 +238,42 @@ impl Builder {
237238
if let Some(fmt) = built.custom_format {
238239
fmt
239240
} else {
240-
Box::new(move |buf, record| {
241-
let fmt = DefaultFormat {
242-
timestamp: built.format_timestamp,
243-
module_path: built.format_module_path,
244-
target: built.format_target,
245-
level: built.format_level,
246-
written_header_value: false,
247-
indent: built.format_indent,
248-
suffix: built.format_suffix,
249-
source_file: built.format_file,
250-
source_line_number: built.format_line_number,
251-
#[cfg(feature = "unstable-kv")]
252-
kv_format: built.kv_format.as_deref().unwrap_or(&default_kv_format),
253-
buf,
254-
};
255-
256-
fmt.write(record)
257-
})
241+
if !built.format_syslog {
242+
Box::new(move |buf, record| {
243+
let fmt = DefaultFormat {
244+
timestamp: built.format_timestamp,
245+
module_path: built.format_module_path,
246+
target: built.format_target,
247+
level: built.format_level,
248+
written_header_value: false,
249+
indent: built.format_indent,
250+
suffix: built.format_suffix,
251+
source_file: built.format_file,
252+
source_line_number: built.format_line_number,
253+
#[cfg(feature = "unstable-kv")]
254+
kv_format: built.kv_format.as_deref().unwrap_or(&default_kv_format),
255+
buf,
256+
};
257+
258+
fmt.write(record)
259+
})
260+
} else {
261+
Box::new(|buf, record| {
262+
writeln!(
263+
buf,
264+
"<{}>{}: {}",
265+
match record.level() {
266+
Level::Error => 3,
267+
Level::Warn => 4,
268+
Level::Info => 6,
269+
Level::Debug => 7,
270+
Level::Trace => 7,
271+
},
272+
record.target(),
273+
record.args()
274+
)
275+
})
276+
}
258277
}
259278
}
260279
}
@@ -271,6 +290,7 @@ impl Default for Builder {
271290
format_indent: Some(4),
272291
custom_format: None,
273292
format_suffix: "\n",
293+
format_syslog: false,
274294
#[cfg(feature = "unstable-kv")]
275295
kv_format: None,
276296
built: false,

src/logger.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ impl Builder {
160160
self.parse_write_style(&s);
161161
}
162162

163+
if env.is_daemon() {
164+
self.format.format_syslog = true;
165+
}
166+
163167
self
164168
}
165169

@@ -336,6 +340,13 @@ impl Builder {
336340
self
337341
}
338342

343+
/// If set to true, format log messages in a Syslog-adapted format.
344+
/// Overrides the auto-detected value.
345+
pub fn format_syslog(&mut self, syslog: bool) -> &mut Self {
346+
self.format.format_syslog = syslog;
347+
self
348+
}
349+
339350
/// Set the format for structured key/value pairs in the log record
340351
///
341352
/// With the default format, this function is called for each record and should format
@@ -818,6 +829,11 @@ impl<'a> Env<'a> {
818829
fn get_write_style(&self) -> Option<String> {
819830
self.write_style.get()
820831
}
832+
833+
fn is_daemon(&self) -> bool {
834+
//TODO: support more logging systems
835+
Var::new("JOURNAL_STREAM").get().is_some()
836+
}
821837
}
822838

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

0 commit comments

Comments
 (0)