godot_logger/
lib.rs

1//! A simple logger that prints to Godot's output window
2//!
3//! [godot-logger] is a simple logger that prints log messages to the output console inside the
4//! [Godot] game engine. It is built around the logging facade of the [log] crate, and uses the
5//! [`godot_print!`] macro from the [gdnative] bindings.
6//!
7//! It is possible to configure different log levels for different Rust modules, similar to other
8//! popular logging frameworks such as [env_logger] or [log4rs]. Simply provide a list as the second
9//! argument to the `init` function with module names and log levels.
10//!
11//! # Use
12//!
13//! Add [godot-logger] and [log] as dependencies to `Cargo.toml`.
14//!
15//! Then initialize [godot-logger] in the `init` function that is exported by `gdnative`. Pass in a
16//! default log level, and a list with module-level overrides (can be empty).
17//!
18//! ```
19//! use gdnative::prelude::*;
20//! use godot_logger::GodotLogger;
21//! use log::{Level, LevelFilter};
22//!
23//! fn init(handle: InitHandle) {
24//!     GodotLogger::builder()
25//!         .default_log_level(Level::Info)
26//!         .add_filter("godot_logger", LevelFilter::Debug)
27//!         .init();
28//!     log::debug!("Initialized the logger");
29//! }
30//!
31//! godot_init!(init);
32//! ```
33//!
34//! The following will appear in the _Output_ console inside Godot:
35//!
36//! ```text
37//! 2021-09-25 19:29:25 DEBUG godot_logger Initialized the logger
38//! ```
39//!
40//! [env_logger]: https://crates.io/crates/env_logger
41//! [gdnative]: https://crates.io/crates/gdnative
42//! [godot-logger]: https://crates.io/crates/godot-logger
43//! [`godot_print!`]: https://docs.rs/gdnative/latest/gdnative/macro.godot_print.html
44//! [log]: https://crates.io/crates/log
45//! [log4rs]: https://crates.io/crates/log4rs
46//! [Godot]: https://godotengine.org/
47
48pub use crate::builder::*;
49
50mod appender;
51mod builder;
52mod filter;
53
54/// A logger that prints to the output console of the Godot game engine
55///
56/// `GodotLogger` is a logger implementation that prints log records to the output console inside
57/// the Godot game engine. The log level can be set per Rust module, similar to other logging
58/// frameworks in Rust.
59///
60/// The recommended way to initialize the logger is by using the crate's [`Builder`]. Its setters
61/// can be used to configure the logger and overwrite the default configuration.
62///
63/// # Examples
64///
65/// ```
66/// use godot_logger::GodotLogger;
67/// use log::{Level, LevelFilter};
68///
69/// // Configure and initialize the logger
70/// GodotLogger::builder()
71///     .default_log_level(Level::Debug)
72///     .add_filter("godot-logger", LevelFilter::Warn)
73///     .init();
74/// ```
75#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
76pub struct GodotLogger;
77
78impl GodotLogger {
79    pub fn builder() -> Builder {
80        Builder::default()
81    }
82}