28
28
//! | `warn!()` | `console.warn()` |
29
29
//! | `error!()` | `console.error()` |
30
30
//!
31
+ //! # Getting Fancy
32
+ //!
33
+ //! The feature set provided by this crate is intentionally very basic. If you need more flexible
34
+ //! formatting of log messages (timestamps, file and line info, etc.) this crate can be used with
35
+ //! the [`fern`] logger via the [`console_log::log`] function.
36
+ //!
31
37
//! # Code Size
32
38
//!
33
39
//! [Twiggy] reports this library adding about 180Kb to the size of a minimal wasm binary in a
74
80
//! the shims generated by `wasm-bindgen`, not the location of the logger call.
75
81
//!
76
82
//! [Twiggy]: https://github.com/rustwasm/twiggy
83
+ //! [`console_log::log`]: fn.log.html
84
+ //! [`fern`]: https://docs.rs/fern
77
85
78
86
use log:: { Log , Level , Record , Metadata , SetLoggerError } ;
79
87
use web_sys:: console;
@@ -92,21 +100,36 @@ impl Log for WebConsoleLogger {
92
100
return ;
93
101
}
94
102
95
- // pick the console.log() variant for the appropriate logging level
96
- let console_log = match record. level ( ) {
97
- Level :: Error => console:: error_1,
98
- Level :: Warn => console:: warn_1,
99
- Level :: Info => console:: info_1,
100
- Level :: Debug => console:: log_1,
101
- Level :: Trace => console:: debug_1,
102
- } ;
103
-
104
- console_log ( & format ! ( "{}" , record. args( ) ) . into ( ) ) ;
103
+ log ( record) ;
105
104
}
106
105
107
106
fn flush ( & self ) { }
108
107
}
109
108
109
+ /// Print a `log::Record` to the browser's console at the appropriate level.
110
+ ///
111
+ /// This function is useful for integrating with the [`fern`](https://crates.io/crates/fern) logger
112
+ /// crate.
113
+ ///
114
+ /// ## Example
115
+ /// ```rust,ignore
116
+ /// fern::Dispatch::new()
117
+ /// .chain(fern::Output::call(console_log::log))
118
+ /// .apply()?;
119
+ /// ```
120
+ pub fn log ( record : & Record ) {
121
+ // pick the console.log() variant for the appropriate logging level
122
+ let console_log = match record. level ( ) {
123
+ Level :: Error => console:: error_1,
124
+ Level :: Warn => console:: warn_1,
125
+ Level :: Info => console:: info_1,
126
+ Level :: Debug => console:: log_1,
127
+ Level :: Trace => console:: debug_1,
128
+ } ;
129
+
130
+ console_log ( & format ! ( "{}" , record. args( ) ) . into ( ) ) ;
131
+ }
132
+
110
133
/// Initializes the global logger setting `max_log_level` to the given value.
111
134
///
112
135
/// ## Example
0 commit comments