18 stable releases

1.9.3 Jan 10, 2025
1.9.2 Jan 7, 2025
1.7.0 Dec 29, 2024
1.6.1 Nov 29, 2024
0.1.0 Nov 9, 2024

#215 in Command-line interface

Download history 4/week @ 2024-09-25 228/week @ 2024-11-06 98/week @ 2024-11-13 2961/week @ 2024-11-20 2927/week @ 2024-11-27 367/week @ 2024-12-04 1138/week @ 2024-12-11 7/week @ 2024-12-18 2087/week @ 2024-12-25 203/week @ 2025-01-01 2065/week @ 2025-01-08

4,362 downloads per month

MIT/Apache

38KB
730 lines

forestry

crate version number crate download count GitHub Actions status

A simple, efficient, concise, and elegant logging library for Rust.

Table of Contents

Installation

To install forestry, simply add it to your Rust project via Cargo:

cargo add forestry

Dependencies

Forestry depends on the colored crate for colorized output.

Usage

Forestry is a simple logging library that allows you to log messages to the console with different levels of severity. Here's an example of how to use it:

src/main.rs

use forestry::prelude::*;

fn main() {
    let log = Logger::new();
    log.info("This is an info message.");
    log.warn("This is a warning message.");
    log.error("This is an error message.");
    log.success("This is a success message.");
    log.critical("This is a critical message.");
}

Inlining

These calls can also be inlined as follows

src/main.rs

use forestry::prelude::*

fn main() {
    let log = Logger::new();
    log.info("This is an info message.")
        .warn("This is a warning message.")
        .error("This is an error message.")
        .success("This is a success message.")
        .critical("This is a critical message.");
}

Static Logging

Using the static feature allows for the following to be run on a single static Logger instance

src/main.rs

use forestry::prelude::*;

fn main() {
    info("This is an info message.");
    warn("This is a warning message.");
    error("This is an error message.");
    success("This is a success message.");
    critical("This is a critical message.");
}

This requires your Cargo.toml to have the feature enabled, which is available on versions after and including 1.8.0. To do so, ensure your Cargo.toml has either

Cargo.toml

[dependencies]
forestry = { version = "1.8.0", features = ["static"] }

or

Cargo.toml

[dependencies.forestry]
version = "1.8.0"
features = ["static"]

These will all output the following to the console:

[0000:*] This is an info message.
[0001:~] This is a warning message.
[0002:!] This is an error message.
[0003:+] This is a success message.
[0004:%] This is a critical message.

It will also be coloured in most terminals.

All formatting is optional; please see the documentation at Docs.rs, specifically for crate::logs::Options. Optional file output and timer inclusion is also supported via the same crate::logs::Options enum.

Async

Forestry also supports asynchronous logging. To enable this feature, simply add the async feature to forestry's declaration in your Cargo.toml file.

Example

First, add the async feature to forestry in your Cargo.toml file by changing the default declaration to either of the following:

Cargo.toml

[dependencies]
forestry = { version = ">=1.5", features = ["async"] }

or

Cargo.toml

[dependencies.forestry]
version = ">=1.5"
features = ["async"]

Then, the logger's internal print calls will be asynchronous futures. This is useful for logging in async functions or in async contexts. awaiting the logger's methods will return the same &mut Logger as before, so chaining is still possible (although only by adding await to every call).

async is compatible with static as of version 1.9.0, and will allow for .awaiting on the static calls if both features are enabled.

Contributing

If you would like to contribute to forestry, please open an issue or submit a pull request.

License

This code is dual-licensed under either the MIT or Apache 2.0 license, at your option. Please see the LICENSE file for more information.

Dependencies

~0.1–8MB
~55K SLoC