Skip to content

Commit da7d5c9

Browse files
committed
feat: Syslog-adapted formating and auto-detection
1 parent ec31808 commit da7d5c9

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/fmt/mod.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ pub(crate) type FormatFn = Box<dyn RecordFormat + Sync + Send>;
218218
#[derive(Default)]
219219
pub(crate) struct Builder {
220220
pub(crate) format: ConfigurableFormat,
221+
pub(crate) format_syslog: bool,
221222
built: bool,
222223
}
223224

@@ -238,7 +239,25 @@ impl Builder {
238239
},
239240
);
240241

241-
Box::new(built.format)
242+
if !built.format_syslog {
243+
Box::new(built.format)
244+
} else {
245+
Box::new(|buf, record| {
246+
writeln!(
247+
buf,
248+
"<{}>{}: {}",
249+
match record.level() {
250+
Level::Error => 3,
251+
Level::Warn => 4,
252+
Level::Info => 6,
253+
Level::Debug => 7,
254+
Level::Trace => 7,
255+
},
256+
record.target(),
257+
record.args()
258+
)
259+
})
260+
}
242261
}
243262
}
244263

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

@@ -299,6 +303,13 @@ impl Builder {
299303
self
300304
}
301305

306+
/// If set to true, format log messages in a Syslog-adapted format.
307+
/// Overrides the auto-detected value.
308+
pub fn format_syslog(&mut self, syslog: bool) -> &mut Self {
309+
self.format.format_syslog = syslog;
310+
self
311+
}
312+
302313
/// Set the format for structured key/value pairs in the log record
303314
///
304315
/// With the default format, this function is called for each record and should format
@@ -817,6 +828,11 @@ impl<'a> Env<'a> {
817828
fn get_write_style(&self) -> Option<String> {
818829
self.write_style.get()
819830
}
831+
832+
fn is_daemon(&self) -> bool {
833+
//TODO: support more logging systems
834+
Var::new("JOURNAL_STREAM").get().is_some()
835+
}
820836
}
821837

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

0 commit comments

Comments
 (0)