@@ -7,6 +7,8 @@ const PATH: &str = "{PATH}";
7
7
/// The arguments passed through the log macro.
8
8
/// E.g. the args from ``info!("hello")`` will be ``"hello"``
9
9
const ARGS : & str = "{ARGS}" ;
10
+ /// A timestamp of when the log was generated.
11
+ const TIMESTAMP : & str = "{TIMESTAMP}" ;
10
12
11
13
/// The primary logging struct that contains all runtime configuration.
12
14
pub struct DioxusLogger {
@@ -17,9 +19,14 @@ pub struct DioxusLogger {
17
19
impl DioxusLogger {
18
20
/// Create a new [`DioxusLogger`] struct to configure and build.
19
21
pub fn new ( level_filter : LevelFilter ) -> Self {
22
+ let format = "[{LEVEL}] {PATH} - {ARGS}]" ;
23
+
24
+ #[ cfg( feature = "timestamps" ) ]
25
+ let format = "[{TIMESTAMP} | {LEVEL}] {PATH} - {ARGS}]" ;
26
+
20
27
Self {
21
28
level_filter,
22
- format : "[{LEVEL}] {PATH} - {ARGS}]" ,
29
+ format,
23
30
}
24
31
}
25
32
@@ -30,9 +37,9 @@ impl DioxusLogger {
30
37
}
31
38
32
39
/// Allows you to define a custom format.
33
- ///
34
- /// The available options are `{LEVEL}`, `{PATH}` and `{ARGS }`
35
- ///
40
+ ///
41
+ /// The available options are `{LEVEL}`, `{PATH}`, `{ARGS}` and `{TIMESTAMP }`
42
+ ///
36
43
/// Providing the format of `[{LEVEL}] {PATH} - {ARGS}]` will return something like `[INFO] dioxus_testing - this is my log message`
37
44
pub fn use_format ( & mut self , format : & ' static str ) {
38
45
self . format = format;
@@ -61,15 +68,8 @@ impl log::Log for DioxusLogger {
61
68
// ARGS
62
69
. replace ( ARGS , record. args ( ) . to_string ( ) . as_str ( ) ) ;
63
70
64
- /*
65
- // The old format, not customizable
66
- let formatted = format!(
67
- "[{}] {} - {}",
68
- record.level(),
69
- record.module_path().unwrap_or(""),
70
- record.args()
71
- );
72
- */
71
+ #[ cfg( feature = "timestamps" ) ]
72
+ let formatted = format_timestamp ( formatted) ;
73
73
74
74
#[ cfg( all(
75
75
not( target_family = "wasm" ) ,
@@ -85,6 +85,13 @@ impl log::Log for DioxusLogger {
85
85
fn flush ( & self ) { }
86
86
}
87
87
88
+ fn format_timestamp ( formatted : String ) -> String {
89
+ let timestamp = time:: OffsetDateTime :: now_utc ( ) ;
90
+ formatted
91
+ . to_owned ( )
92
+ . replace ( TIMESTAMP , timestamp. to_string ( ) . as_str ( ) )
93
+ }
94
+
88
95
/// Initialize `log` and `dioxus-logger` with a specified filter.
89
96
/// For more advanced configuration, build the logger through the [`DioxusLogger`] struct.
90
97
pub fn init ( level_filter : LevelFilter ) -> Result < ( ) , SetLoggerError > {
0 commit comments