Expand description
A logger that calls another application or a web service on each log event.
The target application or URL that this library calls, is passed a formatted string that defaults to a JSON representation of the logged record.
If you would like some ideas about how to use this crate, see the examples in the GitHub source repository.
§Why would you do this?
- There are quick a dirty things that you might want to do with log output
- You want your log output to be handled differently in different environments which you can configure
- You want to use call a webhook/webservice to notify another service (e.g. Pushover.net, discord, AWS Cloudwatch)
§Features
timestamps
- add a timestamp to the output
- the timestamp can be set to one of a number of formats specified by a number of
CallLogger
builder functions
§Example - Call default application (echo
) for each log and default info level,
.new()
defaults to calling echo
and therefore is analagous to .with_call_target("echo")
let _ = call_logger::CallLogger::new()
.with_level(log::LevelFilter::Info).init();
log::info!("msg");
§Example - Call an application for each log and write the result of the call to a file
let _ = call_logger::CallLogger::new()
.with_call_target("echo")
.to_file("test.log")
.with_level(log::LevelFilter::Info)
.init();
log::info!("msg");
§Example - Send all output to Discord via their API
// Get the API endpoint from an environment variable, URL should start with `https://discord.com/api/webhooks/`
#[cfg(feature = "timestamps")]
if let Ok(endpoint) = std::env::var("DISCORD_API") {
let _ = call_logger::CallLogger::new()
.with_call_target(endpoint)
.with_level(log::LevelFilter::Info)
.format(|timestamp, message, record| {
format!(
"{{ \"content\": \"{} [{}] {} - {}\" }}",
timestamp,
record.level(),
record.module_path().unwrap_or_default(),
message
)
})
.init();
log::info!("msg");
}
Structs§
- The
CallLogger
implementsLog
and provides some simple builder methods to help configure what and how to log. Some sensible defaults are provided to perform the simple case of calling theecho
program for all error level logs with a JSON representation of the logged item. The logger then needs to be initialized (.init()
) before use.
Enums§
- The format to use when outputting the timestamp of the log. Timestamps are only part of the log output if the
timestamps
feature is enabled forcall_logger
/
Type Aliases§
- The type alias for a log formatter.