Crates.io | twyg |
lib.rs | twyg |
version | 0.4.1 |
source | src |
created_at | 2020-02-09 04:33:54.72025 |
updated_at | 2024-01-04 07:57:21.370398 |
description | A tiny logging setup for Rust applications |
homepage | |
repository | https://github.com/oxur/twyg |
max_upload_size | |
id | 206556 |
size | 60,977 |
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.
Version warnings:
colors
library whereby one could no longer disable ANSI colour of logged output.OwoColors
, a major code refactor was required to fix the colour regression of v0.3, and as part of that several breaking changes were introduced, including a struct
raname, new fields, etc.First, update your Cargo.toml
s dependencies section:
[dependencies]
twyg = "0.4"
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::Opts
struct for your preferred mechanism:
use twyg::{self, level};
let opts = twyg::Opts{
coloured: true,
level: level::debug(),
report_caller: true,
..Default::default()
};
match twyg::setup(&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 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 full-sized view) is from
running the demos in the examples
directory.
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::Opts,
...
}
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(&cfg.logging) {
Ok(_) => {}
Err(error) => panic!("Could not setup logger: {error:?}"),
};
Copyright © 2020-2024, Oxur Group
Apache License, Version 2.0