cj_femme/
lib.rs

1//! Not just a pretty (inter)face.
2//!
3//! A pretty-printer and [ndjson](http://ndjson.org/) logger for the [log](https://docs.rs/log) crate.
4//!
5//! ## Examples
6//! ```
7//! cj_femme::start();
8//! log::warn!("Unauthorized access attempt on /login");
9//! log::info!("Listening on port 8080");
10//! ```
11
12pub use log::LevelFilter;
13
14#[cfg(not(target_arch = "wasm32"))]
15mod ndjson;
16
17#[cfg(not(target_arch = "wasm32"))]
18mod pretty;
19
20#[cfg(target_arch = "wasm32")]
21mod wasm;
22
23/// Starts logging depending on current environment.
24///
25/// Always logs with 'Info' LevelFilter.
26/// For other filters use with_level.
27///
28/// # Log output
29///
30/// - when compiling with `--release` uses ndjson.
31/// - pretty-prints otherwise.
32/// - works in WASM out of the box.
33///
34/// # Examples
35///
36/// ```
37/// cj_femme::start();
38/// log::warn!("Unauthorized access attempt on /login");
39/// log::info!("Listening on port 8080");
40/// ```
41pub fn start() {
42    with_level(LevelFilter::Info);
43}
44
45/// Start logging with a log level.
46///
47/// All messages under the specified log level will statically be filtered out.
48///
49/// # Examples
50/// ```
51/// cj_femme::with_level(log::LevelFilter::Trace);
52/// ```
53pub fn with_level(level: log::LevelFilter) {
54    #[cfg(target_arch = "wasm32")]
55    wasm::start(level);
56
57    #[cfg(not(target_arch = "wasm32"))]
58    {
59        // Use ndjson in release mode, pretty logging while debugging.
60        if cfg!(debug_assertions) {
61            pretty::start(level);
62        } else {
63            ndjson::start(level);
64        }
65    }
66}