Skip to content

Commit 1847921

Browse files
committed
add support for integrating with fern
1 parent 9126de9 commit 1847921

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,4 @@ shall be dual licensed as above, without any additional terms or conditions.
9494
## See Also
9595

9696
- [`wasm-bindgen-console-logger`](https://github.com/blm768/wasm-bindgen-console-logger)
97+
- [`fern`](https://github.com/daboross/fern) (use with `console_log::log`)

src/lib.rs

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@
2828
//! | `warn!()` | `console.warn()` |
2929
//! | `error!()` | `console.error()` |
3030
//!
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+
//!
3137
//! # Code Size
3238
//!
3339
//! [Twiggy] reports this library adding about 180Kb to the size of a minimal wasm binary in a
@@ -74,6 +80,8 @@
7480
//! the shims generated by `wasm-bindgen`, not the location of the logger call.
7581
//!
7682
//! [Twiggy]: https://github.com/rustwasm/twiggy
83+
//! [`console_log::log`]: fn.log.html
84+
//! [`fern`]: https://docs.rs/fern
7785
7886
use log::{Log, Level, Record, Metadata, SetLoggerError};
7987
use web_sys::console;
@@ -92,21 +100,36 @@ impl Log for WebConsoleLogger {
92100
return;
93101
}
94102

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);
105104
}
106105

107106
fn flush(&self) {}
108107
}
109108

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+
110133
/// Initializes the global logger setting `max_log_level` to the given value.
111134
///
112135
/// ## Example

0 commit comments

Comments
 (0)