14 releases
0.2.0 | Mar 11, 2023 |
---|---|
0.1.12 | Feb 6, 2023 |
0.1.11 | Jan 18, 2023 |
0.1.10 | Nov 8, 2022 |
0.1.5 | Feb 22, 2020 |
#212 in Development tools
344 downloads per month
Used in 7 crates
(6 directly)
17KB
99 lines
twyg
A tiny logging setup for Rust applications
I got used to logging my apps in Clojure with Twig, in LFE with Logjam, and in Go with zylog, so here's something similar for Rust.
Usage
First, update your Cargo.toml
s dependencies section:
[dependencies]
twyg = "0.2"
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:
use twyg;
let opts = twyg::LoggerOpts{
coloured: true,
file: None,
level: String::from("debug"),
report_caller: true,
};
match twyg::setup_logger(&opts) {
Ok(_) => {},
Err(error) => {
panic!("Could not setup logger: {:?}", error)
},
};
The supported options are:
coloured
: setting to false will disable ANIS colours 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 made
Once 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 full-sized view) is from
running the demos in the examples
directory.
Config
Use with the config library is seamless:
-
Set up some YAML:
logging: coloured: true level: debug report_caller: true
-
Add an entry to your config struct:
#[derive(Debug, Deserialize)] pub struct YourAppConfig { ... pub logging: twyg::LoggerOpts, ... }
-
Create a constructor for
YourAppConfig
(see config library docs and examples) -
Build your config:
let cfg = YourAppConfig::default().unwrap();
-
Pass the logging config to twyg:
match twyg::setup_logger(&cfg.logging) { Ok(_) => {} Err(error) => panic!("Could not setup logger: {error:?}"), };
License
Copyright © 2020-2023, Oxur Group
Apache License, Version 2.0
Dependencies
~2.7–3.5MB
~69K SLoC