A tiny logging setup for Rust applications
I got used to logging my apps in Clojure with Twig and in LFE with Logjam, so here this is.
First, update your Cargo.toml
s dependencies section:
toml
[dependencies]
twyg = "0.1.5"
I like to put my logging setup in YAML config files for my apps, but however
you prefer to store your config, you'll next need to populate the
twyg::LoggerOpts
struct for your preferred mechanism:
```rust use twyg;
let opts = twyg::LoggerOpts{ colored: true, file: String::from(""), level: String::from("debug"), report_caller: true, };
match twyg::setuplogger(&opts) { Ok() => {}, Err(error) => { panic!("Could not setup logger: {:?}", error) }, }; ```
The supported options are:
colored
: setting to false will disable ANIS colors in the logging outputfile
: provide a path to a file, and output will be logged there toolevel
: case-insensitive logging levelreport_caller
: setting to true will output the filename and line number
where the logging call was madeOnce the setup function has been called, all subsequent calls to the standard Rust logging macros will use this setup, providing output like the following:
The output in the screenshot above (click for a a full-sized view) is from running the little demo in main.rs.
Use with the config library is seamless:
Set up some YAML:
yaml
logging:
colored: true
file: ""
level: debug
report_caller: true
Add an entry to your config struct:
```rust
pub struct YourAppConfig { ... pub logging: twyg::LoggerOpts, ... } ```
Create a constructor for YourAppConfig
(see config library docs and examples)
Build your config:
rust
let cfg = YourAppConfig::new().unwrap();
Pass the logging config to twyg:
rust
match twyg::setup_logger(&cfg.logging) {
Ok(_) => {}
Err(error) => panic!("Could not setup logger: {:?}", error),
};
Copyright © 2020, Oxur Group
Apache License, Version 2.0