✔️ Native Environment
✔️ WASM Environment
✔️ Terminal Logging
✔️ File Logging
✔️ Network Logging
✔️ Auth and Request Signing
✔️ Relay Server
✔️ Production Ready
/* no default feature enabled (enabling both at once won't compile) */
better-logger = { version = "3.0.1", features = ["native"] }
better-logger = { version = "3.0.1", features = ["wasm"] }use better_logger::{
LoggerSettings,
NetworkFormat,
NetworkEndpointUrl,
SingleNet,
MultipleNet,
};
/* native settings */
let settings = LoggerSettings {
terminal_logs: true,
terminal_log_lvl: "trace".to_string(),
wasm_logging: false, // must be false
file_logs: true,
file_log_lvl: "error".to_string(),
log_file_path: "/path/to/my/file.log".to_string(),
network_logs: true,
network_log_lvl: "warn".to_string(),
network_endpoint_url: NetworkEndpointUrl::Single(SingleNet { url: "http://127.0.0.1:8090/".to_string() }), // Using single endpoint
network_format: NetworkFormat::PlainText,
use_bearer_auth: true, // Using bearer auth
bearer_auth_key: "secretkey".to_string(),
request_signer: Some(RequestSigner(sign_log)), // See Request Signing for details
debug_extra: true,
async_logging: false,
};
let endpoints = MultipleNet {
trace: "http://0.0.0.0:8070/trace".to_string(),
debug: "https://hooks.slack.com/services/TTT/000/XXX".to_string(),
debugx: "https://hooks.slack.com/services/TTT/000/XXX".to_string(),
info: "http://127.0.0.1:8090/".to_string(),
warn: "http://127.0.0.1:8090/".to_string(),
error: "https://discord.com/api/webhooks/123/abc".to_string(),
};
/* wasm settings */
let settings = LoggerSettings {
terminal_logs: true,
terminal_log_lvl: "debug".to_string(),
wasm_logging: true, // must be true
file_logs: false, // must be false
file_log_lvl: "".to_string(), // value doesn't matter
log_file_path: "".to_string(), // value doesn't matter
network_logs: true,
network_log_lvl: "trace".to_string(),
network_endpoint_url: NetworkEndpointUrl::MultipleNet(endpoints), // Using multiple endpoints
network_format: NetworkFormat::JsonText { field: "text".into() },
use_bearer_auth: false, // Not using bearer auth
bearer_auth_key: "".to_string(),
request_signer: None, // No request signing, see Request Signing for details
debug_extra: true,
async_logging: true, // if network_logs is true, async_logging must also be true
};use better_logger::logger;
fn main() {
if let Err(err) = logger::init(settings) {
eprintln!("{:?}", err);
std::process::exit(1);
}
}use better_logger::logger::*;
fn my_function() {
let debug_msg: &str = "world";
let debugx_msg: String = format!(", world");
let fail: &str = r#""failed""#;
trace!("hello");
debug!("{}", debug_msg);
debugx!("hello{}", debugx_msg);
info!("hello message");
warn!("world message");
error!(r#""hello" "world" {}"#, fail);
}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(s) to send log messages to |
use_bearer_auth |
Enable Bearer auth for HTTP logs |
bearer_auth_key |
Bearer token used for HTTP logs |
request_signer |
Optional per-request header signer |
debug_extra |
Show debugx! logs |
async_logging |
Enable async logging |
- 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
- Optional Bearer auth can be added to every network log request
- Set
use_bearer_auth = trueand provide a non-emptybearer_auth_key - Initialization fails if
use_bearer_auth = trueandbearer_auth_keyis empty
- Set
- Optional per-request headers (for example signatures) can be added with
request_signer, see Request signing - 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
It is just a second debug, the debugx!() logs will be labeled as DEBUG when they print
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!
pub enum NetworkFormat {
PlainText,
JsonText { field: String },
}PlainText- Sends network logs astext/plainJsonText- Sends network logs asapplication/json
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() }
pub enum NetworkEndpointUrl {
Single(SingleNet),
Multiple(MultipleNet),
}
pub struct SingleNet {
pub url: String,
}
pub struct MultipleNet {
pub trace: String,
pub debug: String,
pub debugx: String,
pub info: String,
pub warn: String,
pub error: String,
}If you want all your log levels to hit one endpoint, use SingleNet.
If you have multiple endpoints, use MultipleNet.
request_signer lets your application add its own headers to every network log request, for example a timestamp and an HMAC signature that your endpoint verifies.
better-logger only calls your function, it contains no signing algorithm or keys.
use better_logger::{RequestSigner, SignableRequest};
fn sign_log(request: &SignableRequest<'_>) -> Result<Vec<(String, String)>, String> {
// request.url, request.content_type and request.body (the exact bytes being sent)
let signature: String = my_hmac(request.body)?;
return Ok(vec![("X-Log-Signature".to_string(), signature)]);
}
let settings = LoggerSettings {
/* ... */
request_signer: Some(RequestSigner(sign_log)), // or None to disable
/* ... */
};RequestSignerwraps a plainfn, not a closure. Keep any key your signer needs in your ownstatic/OnceLock- It is called once per HTTP attempt, right before sending. WASM retries call it again, so time based signatures stay fresh
bodyis exactly what is sent: the formatted line forPlainText, the serialized payload forJsonText- Your headers are added after
Content-Typeand the optionalAuthorization: Bearerheader - If your signer returns
Err, that log message is not sent (it is never sent unsigned), and the error is printed like any other network failure - A signer may not set
Content-Type, and may not setAuthorizationwhenuse_bearer_authistrue(checked case-insensitively). Empty header names are rejected. Any of these drops the message the same way as anErr - In a browser, custom headers trigger a CORS preflight. Your endpoint must list them in
Access-Control-Allow-Headers - The
relayserver does not forward custom headers, so request signing only works when sending directly to your endpoint
- When using WASM in the browser, CORS will block requests to external domains such as
hooks.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.
See the working example: https://github.com/Lozlof/easy-log-forwarding
better-logger = { version = "3.0.1", features = ["relay"] }
/* if you want the relay server to also send logs, declare native as well */
/* "relay" cannot run in a WASM environment */
better-logger = { version = "3.0.1", features = ["relay", "native"] } use better_logger::{NetworkFormat, RelaySettings};
let settings = RelaySettings {
listen_address: "0.0.0.0:8070".to_string(),
output_format: NetworkFormat::JsonText { field: "text".into() },
output_url: "https://logs.mydomain.com".to_string(),
cors_allowed_origins: vec!["*".into()],
actix_workers: 1,
};use better_logger::relay;
#[tokio::main]
async fn main() {
if let Err(err) = relay::start(settings).await {
eprintln!("{:?}", err);
std::process::exit(1);
}
}| SETTING | DESCRIPTION |
|---|---|
listen_address |
The socket to listen on |
output_format |
See NetworkFormat above |
output_url |
Logs are forwarded to here |
cors_allowed_origins |
Domains allowed (CORS) |
actix_workers |
Number of Actix worker threads |
NOTE: Send log messages to the relay server as NetworkFormat::PlainText, it does not accept incoming NetworkFormat::JsonText
It can output as either option
- This relay server runs on actix-web and requires the tokio runtime
- It is designed to be a standalone server
- In actix-web, each worker thread is an independent thread that runs the full Actix runtime.
cors_allowed_origins: vec!["*".into()]allows all origins.- Specify a specific URL to restrict access.
© 2026 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