BETTER-LOGGER
Full stack development, one logger to rule them all
✔️ Native Environment
✔️ WASM Environment
✔️ Terminal Logging
✔️ File Logging
✔️ Network Logging
HOW TO USE
😺 ONE: Declare Feature
/* no default feature enabled (enabling both at once won't compile) */
better-logger =
better-logger =
💻 TWO: Settings
use ;
/* native settings */
let settings = LoggerSettings ;
/* wasm settings */
let settings = LoggerSettings ;
💡 THREE: Initialize
use logger;
⚠️ FOUR: Log
use *;
trace! ➡️ (debug!, debugx!) ➡️ info! ➡️ warn! ➡️ error!
| SETTING | DESCRIPTION |
|---|---|
terminal_logs |
Log to terminal |
terminal_log_lvl |
Minimum level to display |
wasm_logging |
Log to dev tools console |
file_logs |
Log to file |
file_log_lvl |
Minimum level to write |
log_file_path |
Path to log file |
network_logs |
Log to an HTTP endpoint |
network_log_lvl |
Minimum level to send |
network_format |
Network message format |
network_endpoint_url |
URL to send log messages to |
debug_extra |
Show debugx! logs |
async_logging |
Enable async logging |
ℹ️ INFORMATION
- NATIVE console logging uses env_logger
- WASM console logging uses wasm-logger
- Log messages (log) routed through env_logger and wasm-logger are NOT written to the file or sent via HTTP
- Only messages emitted via better-logger are persisted to the log file and sent via HTTP
- You can use better-logger as your logging facade
- You would have to incorporate better-logger into your low level crates, but only initialize ONCE at the highest level
logger::init()Can only be called ONCE, subsequent calls will cause apanic!()- better-logger's NATIVE feature requires the tokio runtime
File and network logginguses the sameformattingas theNATIVE console logs[<RFC 3339 timestamp> <LEVEL> <target>] <message>
- better-logger will automatically create the path and file if not already created
log_file_pathrequires alocalorabsolutepath, a file name only will fail(E.g. log_file_path: "file.log".to_string())- File logs are overwritten, not appended
- Async logging uses a “fire and forget” model:
- It spawns a new async task on the current (tokio) runtime for each message
- Network logging uses a “fire and forget” model
- If your HTTP endpoint is down, better-logger's will continue to run without issue
- Why is
synchronousnetwork loggingNOT allowedin WASM? Browsers don’t allow blocking network I/O on the main thread - Why is
file loggingNOT allowedin WASM? Browsers can't talk to your file system - All macros use
format!()under the hood, any string-like type is accepted - The
testing-wasmandtesting-httpfeatures are for TESTING only- The NATIVE tests are
tests/async_native.rsortests/sync_native.rs - The WASM test is
tests/wasm_environment/main.rs; this is also the"wasm-test"binary - The
"http-test"binary is a simple HTTP server that will print log messages it receives
- The NATIVE tests are
- Instructions for testing are in the comments of these files
What is DEBUGX?
It is just a second debug, the debugx!() logs will be labeled as DEBUG when they print
Why would I want to use DEBUGX?
Let’s say you’re in development, so you want to see all your debug logs. However, some of your debug logs are massive and clutter your terminal.
You can mark those verbose logs as debugx!() and set debug_extra = false to hide them.
Later, if you're troubleshooting or need to view them, set debug_extra = true and see your extra debug logs!
How to use NetworkFormat
PlainText- Sends network logs astext/plainJsonText- Sends network logs asapplication/json
Integrating with external services
Sending logs to JSON endpoints is easy, just set the expected field.
- Slack:
NetworkFormat::JsonText { field: "text".into() } - Discord:
{ field: "content".into() } - Generic:
{ field: "message".into() }
Note:
When using WASM in the browser, CORS will block requests to external domains such ashooks.slack.comordiscord.com.
To avoid this, your web client should send logs to a logging server on the same domain, which can then forward those logs to external services like Slack or Discord.
🎉 Contributing
TODO:
- Validate all user settings in the init function
- Formatting options for the log messages
- UDP logging
- Append option for file logs
- Native async logging without Tokio
- Consolidation, optimization
- Add more network formats
- This list is not exclusive, all ideas are welcome
License
© 2025 Gistyr LLC
This project, better-logger, is dual-licensed under your choice of:
- Apache License 2.0
See the LICENSE-APACHE file or view it online at http://www.apache.org/licenses/LICENSE-2.0 - MIT License
See the LICENSE-MIT file or view it online at http://opensource.org/licenses/MIT