1
1
use log:: { LevelFilter , SetLoggerError } ;
2
- //use std::path::PathBuf;
3
2
4
- /*pub struct HttpConfig {
5
- method: reqwest::Method,
6
- url: String,
7
- authorization_token: String,
8
- }
9
-
10
- pub enum OutputType {
11
- Json,
12
- Stdio,
13
- }
14
-
15
- pub struct FileSystemConfig {
16
- file_name: String,
17
- output_type: OutputType,
18
- output_path: PathBuf,
19
- }*/
3
+ /// The log level. E.g. ``INFO``
4
+ const LEVEL : & str = "{LEVEL}" ;
5
+ /// The file's path or crate name. E.g. ``dioxus-testing``
6
+ const PATH : & str = "{PATH}" ;
7
+ /// The arguments passed through the log macro.
8
+ /// E.g. the args from ``info!("hello")`` will be ``"hello"``
9
+ const ARGS : & str = "{ARGS}" ;
20
10
21
11
/// The primary logging struct that contains all runtime configuration.
22
12
pub struct DioxusLogger {
23
- //http_config: Option<HttpConfig>,
24
- //file_system_config: Option<FileSystemConfig>,
25
13
level_filter : LevelFilter ,
14
+ format : & ' static str ,
26
15
}
27
16
28
17
impl DioxusLogger {
29
- pub fn new (
30
- level_filter : LevelFilter ,
31
- //file_system_config: Option<FileSystemConfig>,
32
- //http_config: Option<HttpConfig>,
33
- ) -> Self {
18
+ /// Create a new [`DioxusLogger`] struct to configure and build.
19
+ pub fn new ( level_filter : LevelFilter ) -> Self {
34
20
Self {
35
- //http_config,
36
- //file_system_config,
37
21
level_filter,
22
+ format : "[{LEVEL}] {PATH} - {ARGS}]" ,
38
23
}
39
24
}
25
+
26
+ /// Builds and initializes the logger with [`log`]
27
+ pub fn build ( self ) -> Result < ( ) , SetLoggerError > {
28
+ let level = self . level_filter . clone ( ) ;
29
+ log:: set_boxed_logger ( Box :: new ( self ) ) . map ( |( ) | log:: set_max_level ( level) )
30
+ }
31
+
32
+ /// Allows you to define a custom format.
33
+ ///
34
+ /// The available options are `{LEVEL}`, `{PATH}` and `{ARGS}`
35
+ ///
36
+ /// Providing the format of `[{LEVEL}] {PATH} - {ARGS}]` will return something like `[INFO] dioxus_testing - this is my log message`
37
+ pub fn use_format ( & mut self , format : & ' static str ) {
38
+ self . format = format;
39
+ }
40
40
}
41
41
42
42
impl log:: Log for DioxusLogger {
@@ -49,7 +49,27 @@ impl log::Log for DioxusLogger {
49
49
return ;
50
50
}
51
51
52
- let formatted = format ! ( "[{}] {} - {}" , record. level( ) , record. module_path( ) . unwrap_or( "" ) , record. args( ) ) ;
52
+ // This is cursed
53
+ let formatted = self
54
+ . format
55
+ // LEVEL
56
+ . replace ( LEVEL , record. level ( ) . as_str ( ) )
57
+ . to_owned ( )
58
+ // PATH
59
+ . replace ( PATH , record. module_path ( ) . unwrap_or ( "" ) )
60
+ . to_owned ( )
61
+ // ARGS
62
+ . replace ( ARGS , record. args ( ) . to_string ( ) . as_str ( ) ) ;
63
+
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
+ */
53
73
54
74
#[ cfg( all(
55
75
not( target_family = "wasm" ) ,
@@ -66,7 +86,7 @@ impl log::Log for DioxusLogger {
66
86
}
67
87
68
88
/// Initialize `log` and `dioxus-logger` with a specified filter.
89
+ /// For more advanced configuration, build the logger through the [`DioxusLogger`] struct.
69
90
pub fn init ( level_filter : LevelFilter ) -> Result < ( ) , SetLoggerError > {
70
- let logger = DioxusLogger :: new ( level_filter) ;
71
- log:: set_boxed_logger ( Box :: new ( logger) ) . map ( |( ) | log:: set_max_level ( level_filter) )
91
+ DioxusLogger :: new ( level_filter) . build ( )
72
92
}
0 commit comments