3 releases
0.1.3 | Jul 13, 2023 |
---|---|
0.1.2 | Jul 13, 2023 |
0.1.1 | Jul 13, 2023 |
#801 in WebAssembly
16KB
279 lines
log4wasm
✨ A rust-generated wasm logging utility with styling capabilities for display in a web console.
Be advised that this crate is still considered experimental however should be stable for use in its current state.
![output of generated example log](https://img.gs/czjpqfbdkz/800,2x/https://raw.githubusercontent.com/heavens/log4wasm/HEAD/demo.png)
Getting started
You can start using log4wasm
by adding the following in your Cargo.toml [dependencies]
section:
log4wasm = "0.1.3"
Note: log4wasm
relies on the wasm-bindgen crate for code-generation therefore it is the current recommended approach for building reliably.
Example
A minimal use-case within your application would look something like the following:
fn main() {
log4wasm::log::info!("A basic, unformatted log with a level of INFO.");
}
log4wasm
provides a suite of macros out of the box available for use. These may be favorable in instances where building a custom logger is not necessary.
log4wasm::log::trace!("a basic trace log!");
log4wasm::log::debug!("a basic debug log!");
log4wasm::log::info!("a basic info log!");
log4wasm::log::warning!("a basic warn log!");
log4wasm::log::error!("a basic error log!");
log4wasm::log::fatal!("a basic fatal log!");
Alternatively, custom loggers may be created using the designated builder:
use log4wasm::logger::{Level, Logger};
use log4wasm::styled::{Color, Decoration, Styled};
// Create a custom logger with a desired name and log level.
let logger = Logger::new_builder()
.named("MAIN")
.with_level(Level::Debug)
.build();
// Create a styling to be applied on the output written to console.
let styled_log = Styled::with_content(format!("A formatted debug log named \"MAIN\" with green, underlined text and an argument of {}", 1))
.colored(Color::Green)
.decorate(Decoration::Underline)
.into_log();
// Write the styled output to console.
logger.print_fmt(styled_log);
Dependencies
~7.5–10MB
~182K SLoC