paris_log/
lib.rs

1//! # paris-log
2//!
3//! A crate that allows you to use [`paris`'s formatting](https://github.com/0x20F/paris/blob/master/README.md#color-keys) with the `log` crate.
4//!
5//! ## Usage
6//!
7//! Add this to your `Cargo.toml`:
8//!
9//! ```toml
10//! [dependencies]
11//! paris-log = "1"
12//! ```
13//!
14//! Instead of using `log` macros:
15//!
16//! ```rs
17//! use log::{info, error, ...};
18//! ```
19//!
20//! You can use `paris-log` macros:
21//!
22//! ```rs
23//! use paris_log::{info, error, ...};
24//! ```
25//!
26//! Now you can use [`paris`'s formatting](https://github.com/0x20F/paris/blob/master/README.md#color-keys):
27//!
28//! ```rs
29//! info!("This <cyan>is <bright green>a log<//>! <green><tick></>");
30//! ```
31//!
32//! If you only need to use `paris`'s formatting a few times, it might be better to use the full path specifier (for example, `paris_log::info`).
33//!
34//! `paris-log`'s macros should be usable just like `log` macros; you can use format specifiers like normal. However, they currently don't support manually specifying log target. If you need this feature,
35//! please make an issue / PR! Additionally, `paris-log` does not have the `log` macro from the `log` crate since it seemed repetitive.
36//!
37//! > **Note**
38//! >
39//! > You must use a logging implementation along with this crate for logs to show up since `paris-log` uses the `log` crate internally.
40//! > [See here for more info.](https://docs.rs/log/latest/log/#available-logging-implementations) If you don't need the extra features the `log` crate and logging implementations provide, consider using
41//! > `paris` itself.
42//!
43//! ## The `icons` feature
44//!
45//! If you have used `paris`, you may have noticed that logs have a nice icon in front of them to indicate if it is info, error, warn or success. `paris-log` can automatically do this for you if you
46//! enable the `icons` feature:
47//!
48//! ```toml
49//! [dependencies]
50//! paris-log = { version = "1", features = ["icons"] }
51//! ```
52//!
53//! ```rs
54//! info!("This <cyan>is <bright green>a log<//>!");
55//! // Without `icons` feature: This is a log!
56//! // With `icons` feature: ℹ This is a log!
57//! ```
58//!
59//! Icons will only be added to the `error`, `warn` and `info` macros. This feature also adds the `success` macro, for feature parity with `paris`. (the `success` macro uses the info log level)
60
61#[cfg(feature = "icons")]
62mod icons;
63#[cfg(not(feature = "icons"))]
64mod normal;
65
66// Do not use directly, this is just so we can use `log` and `paris` even if they aren't directly defined as dependencies
67// Export names are changed to make it harder to appear in editor autocomplete since `#[doc(hidden)]` doesn't seem to work.
68#[doc(hidden)]
69pub mod __private_exports_do_not_use {
70    #[doc(hidden)]
71    pub use log::{
72        debug as __export_debug, error as __export_error, info as __export_info,
73        trace as __export_trace, warn as __export_warn,
74    };
75    #[doc(hidden)]
76    pub use paris::formatter::colorize_string as __export_colorize_string;
77}
78
79#[cfg(test)]
80mod tests {
81    use log::LevelFilter;
82    use simplelog::{ColorChoice, Config, TermLogger, TerminalMode};
83
84    use crate::*;
85
86    #[test]
87    fn logging() {
88        if TermLogger::init(
89            LevelFilter::Trace,
90            Config::default(),
91            TerminalMode::Mixed,
92            ColorChoice::Auto,
93        )
94        .is_ok()
95        {}
96
97        // taken from paris macros test
98        #[cfg(feature = "icons")]
99        success!("This <cyan>is <bright green>a log<//>!");
100        #[cfg(not(feature = "icons"))]
101        info!("This <cyan>is <bright green>a log<//>!");
102        info!(
103            "<red>HAHAHAHAHA<///> <black><on green>{}</>",
104            "the crate supports macros with colors!"
105        );
106        error!("This is going to <bright red>stderr</> {}", "WOOOO");
107        warn!("This is a {} <yellow>BEWARE</>!", "warning");
108        debug!("{} went well, congrats!", "<bright green>Everything</>");
109
110        match "a" {
111            "a" => trace!(
112                "It works inside a match as well!!! {}",
113                "<bright blue>finally</>"
114            ),
115            _ => unreachable!(),
116        }
117    }
118}