File tree Expand file tree Collapse file tree 2 files changed +36
-2
lines changed Expand file tree Collapse file tree 2 files changed +36
-2
lines changed Original file line number Diff line number Diff line change @@ -63,7 +63,6 @@ use std::io::prelude::Write;
63
63
use std:: rc:: Rc ;
64
64
use std:: { fmt, io, mem} ;
65
65
66
- #[ cfg( feature = "color" ) ]
67
66
use log:: Level ;
68
67
use log:: Record ;
69
68
@@ -218,6 +217,7 @@ pub(crate) type FormatFn = Box<dyn RecordFormat + Sync + Send>;
218
217
#[ derive( Default ) ]
219
218
pub ( crate ) struct Builder {
220
219
pub ( crate ) format : ConfigurableFormat ,
220
+ pub ( crate ) format_syslog : bool ,
221
221
built : bool ,
222
222
}
223
223
@@ -238,7 +238,25 @@ impl Builder {
238
238
} ,
239
239
) ;
240
240
241
- Box :: new ( built. format )
241
+ if !built. format_syslog {
242
+ Box :: new ( built. format )
243
+ } else {
244
+ Box :: new ( |buf : & mut Formatter , record : & Record < ' _ > | {
245
+ writeln ! (
246
+ buf,
247
+ "<{}>{}: {}" ,
248
+ match record. level( ) {
249
+ Level :: Error => 3 ,
250
+ Level :: Warn => 4 ,
251
+ Level :: Info => 6 ,
252
+ Level :: Debug => 7 ,
253
+ Level :: Trace => 7 ,
254
+ } ,
255
+ record. target( ) ,
256
+ record. args( )
257
+ )
258
+ } )
259
+ }
242
260
}
243
261
}
244
262
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
@@ -820,6 +831,11 @@ impl<'a> Env<'a> {
820
831
fn get_write_style ( & self ) -> Option < String > {
821
832
self . write_style . get ( )
822
833
}
834
+
835
+ fn is_daemon ( & self ) -> bool {
836
+ //TODO: support more logging systems
837
+ Var :: new ( "JOURNAL_STREAM" ) . get ( ) . is_some ( )
838
+ }
823
839
}
824
840
825
841
impl < ' a , T > From < T > for Env < ' a >
You can’t perform that action at this time.
0 commit comments