json_log-0.1.0 has been yanked.
Logger with Json Format
json_log helpers you to print logs in json format.
Usage
First, add these in your Cargo.toml:
[]
= { = "0.4" }
= { = "0.1" }
= { = "1", = ["derive"] } # if you want to log your struct...
You need to choose the logging level after the program starts. For example:
use LevelFilter;
Or, you can set environment variable RUST_LOG:
Simple Text Logging
json_log is an adapter of the log trait. You can use all the log features in your code.
For example:
use LevelFilter;
use ;
And here is the output:
{"level":"Debug","ts":1695181245461,"msg":"debug"}
{"level":"Info","ts":1695181245461,"msg":"a = 6"}
{"level":"Warn","ts":1695181245461,"msg":"a is 6"}
Log your struct
If you have already defined your own logging struct, you can log it by make it Serialize. For example:
use Serialize;
// Or, ...
Then, do the log:
use HashMap;
use LevelFilter;
// A static reference, you can define it everywhere.
static MY_LOGGER: &JsonLogger = get_default_logger;
For more examples, see ./examples.