nolog logger
20 lines of code with 0 deps.
- Colored output.
- Support for named format arguments
info!("{line_count} lines.");. - Displays the location of the code
[src/main.rs 15:5]. - Automatically disabled in the release build
cargo run --release. - Same syntax as
logcrate. As the project grows, it is possible to migrate to an advanced logger (using thelogcrate facade) without changing the code. - Can be built into the project directly instead of as a dependency.
- Uses Rust's built-in macros.
- Easy to modify to redirect log output (to
stderrorfile). - MIT License.

Using nolog
Create a logger.rs file with the following content (this is the complete
library code):
/* ----------------------------------------------------------------------------
MIT License
Copyright (c) 2022 Vadim Glinka
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
---------------------------------------------------------------------------- */
macro_rules!
trace
macro_rules!
debug
macro_rules!
info
macro_rules!
warn
macro_rules!
error
macro_rules!
crit
macro_rules!
writelog
main.rs could be like this:
// ^^^^^^
// Must go higher in the list than the modules
// in which it will be used.
// Macro import in modules below is not needed.
Now you have a logger and no new dependencies.
Redirect output
To redirect output to stderr you need to add a single 'e' character
to the writelog macro so that instead of println!()
it becomes eprintln!(). See full example.
Write log to file: See full example.
Using nolog as a dependency
Although this logger was created to be used directly in small projects, you can also use it as a dependency.
In Cargo.toml:
= "0.1.0"
In main.rs:
use *;
OR
use ;
OR
pub extern crate nolog;
Example
cargo run --example base
cargo run --example to-stderr
cargo run --example to-file