File tree Expand file tree Collapse file tree 2 files changed +36
-1
lines changed Expand file tree Collapse file tree 2 files changed +36
-1
lines changed Original file line number Diff line number Diff line change @@ -218,6 +218,7 @@ pub(crate) type FormatFn = Box<dyn RecordFormat + Sync + Send>;
218
218
#[ derive( Default ) ]
219
219
pub ( crate ) struct Builder {
220
220
pub ( crate ) format : ConfigurableFormat ,
221
+ pub ( crate ) format_syslog : bool ,
221
222
built : bool ,
222
223
}
223
224
@@ -238,7 +239,25 @@ impl Builder {
238
239
} ,
239
240
) ;
240
241
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
+ }
242
261
}
243
262
}
244
263
Original file line number Diff line number Diff line change @@ -160,6 +160,10 @@ impl Builder {
160
160
self . parse_write_style ( & s) ;
161
161
}
162
162
163
+ if env. is_daemon ( ) {
164
+ self . format . format_syslog = true ;
165
+ }
166
+
163
167
self
164
168
}
165
169
@@ -299,6 +303,13 @@ impl Builder {
299
303
self
300
304
}
301
305
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
+
302
313
/// Set the format for structured key/value pairs in the log record
303
314
///
304
315
/// With the default format, this function is called for each record and should format
@@ -817,6 +828,11 @@ impl<'a> Env<'a> {
817
828
fn get_write_style ( & self ) -> Option < String > {
818
829
self . write_style . get ( )
819
830
}
831
+
832
+ fn is_daemon ( & self ) -> bool {
833
+ //TODO: support more logging systems
834
+ Var :: new ( "JOURNAL_STREAM" ) . get ( ) . is_some ( )
835
+ }
820
836
}
821
837
822
838
impl < ' a , T > From < T > for Env < ' a >
You can’t perform that action at this time.
0 commit comments