
Table of Contents
TL;DR
Installing the library:
Quick start:
// Include stuff from the library:
use Logger;
use Verbosity;
// A `Logger` struct with default configuration
let mut logger = default;
// Configure `Logger` to your liking
logger.set_verbosity; // Don't suppress any log messages
// Print logs:
logger.debug;
logger.info;
logger.warning;
logger.error;
logger.fatal;
Installation
To install the library with cargo, run:
Or add this to your Cargo.toml:
[]
= "2.0.0"
The Logger
The Logger struct is the core of the entire project.
This is what you are going to use when you want to print a log, set filtering
rules or modify log formatting. All of it's fields are private, only allowing for
modification via setters.
Creating a Logger struct with default configuration:
# use Logger;
let mut logger = default;
Controlling Terminal Output
By default, Logger streams all logs to stdout and stderr. If you only want
to write logs to a file or store them in a custom buffer, use:
# use Logger;
# let mut logger = default;
logger.toggle_console_output;
Custom Log Buffer
Logger can store logs in a buffer instead of printing them or writing them
to a file. Later, you can reference that buffer and do whatever you want with it.
Enabling custom log buffer:
# use Logger;
# let mut logger = default;
logger.toggle_custom_log_buffer;
And when you need a reference to that buffer, call:
# use Logger;
# let mut logger = default;
let buffer = logger.log_buffer;
Log Format
A basic log consists of several headers:
- Log Type → The type of the log (debug, info, warning etc.)
- Timestamp → Contains the date and time the log was created
- Message → The actual log message
Those headers can then be formatted using a log format string, similarly to how you would format a datetime string.
Here is a log message with all it's headers marked:
[ DEBUG 21:52:37 An example debug message ]
^^^^^ ^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^
| | |
| | the message
| timestamp
log type
This specific effect was achieved by setting the datetime format to %H:%M:%S,
log format to [ %h %d %m ] and the debug log type header to DEBUG.
Use this method to set the datetime format of a Logger:
# use Logger;
# let mut logger = default;
logger.set_datetime_format;
You can set a custom log format like this:
# use Logger;
# let mut logger = default;
logger.set_log_format;
Note that the %m (message) placeholder is mandatory and you will get an error
unless you include it in your format string.
Log type headers can be customized with their corresponding methods:
# use Logger;
# let mut logger = default;
logger.set_debug_header;
logger.set_info_header;
logger.set_warning_header;
logger.set_error_header;
logger.set_fatal_header;
And you can also customize their colors:
# use ;
# let mut logger = default;
logger.set_debug_color;
logger.set_info_color;
logger.set_warning_color;
logger.set_error_color;
logger.set_fatal_color;
Using the LogStruct
LogStruct is a type that represents a single log entry. You can create LogStruct instance using one of it's constructors:
debug(message: &str)info(message: &str)warning(message: &str)error(message: &str)fatal_error(message: &str)
Using LogStruct's debug constructor to create a debug log, and then formatting
with logger.format_log(...):
# use ;
# let mut logger = default;
let log_formatted = logger.format_log;
Log Filtering
Logs are filtered based on the current LogLevel and the Logger's verbosity
setting. config::Verbosity enum can be used to set the verbosity of a Logger.
The Verbosity level determines which logs are filtered out:
All→ Disables log filtering, allowing all logs to pass through.Standard(default) → Filters out debug logs.Quiet→ Only allows errors and warnings to be displayed.ErrorsOnly→ Only allows errors to be shown.
To modify the verbosity of the Logger, use:
# use ;
# let mut logger = default;
logger.set_verbosity;
To toggle log filtering, use:
# use ;
# let mut logger = default;
logger.toggle_log_filtering;
File Logging
File logging is a feature that allows you to automatically save log output to a file.
Enabling file logging:
# use Logger;
# let mut logger = default;
# let mut path = temp_dir;
# path.push;
# let path = &path.to_str.unwrap.to_string;
// Set the log file path first:
logger.set_log_file_path;
// Then enable file logging:
logger.toggle_file_logging;
logger.info; // Yay!
logger.flush; // Flush the log buffer to a file
It is CRUCIAL to set the log file path FIRST. If you try to enable file
logging before specifying a valid path, Logger will check the log file path, and
since the default path is an empty string, it will result in an error.
Locking the Log File
The log file can be locked to prevent race conditions when there are multiple
threads accessing it at the same time. It prevents Logger from writing to
the log file until the lock has been released. The lock is only ignored when a
Logger is being dropped and the OnDropPolicy is set to IgnoreLogFileLock
(off by default).
Log file lock is not persistent (it's not saved when calling
logger.save_template("path")).
Toggling log file lock:
# use Logger;
# let mut logger = default;
logger.toggle_log_file_lock;
// Do some I/O operations on the log file here
logger.toggle_log_file_lock;
To set the on drop log file policy, use:
# use ;
# let mut logger = default;
logger.set_on_drop_file_policy;
Automatic Log Buffer Flushing
You can either flush the log buffer automatically or set up automatic flushing based on the log buffer size:
# use Logger;
# let mut logger = default;
# let mut path = temp_dir;
# path.push;
# let path = &path.to_str.unwrap.to_string;
logger.set_log_file_path;
logger.toggle_file_logging;
// This will make `Logger` to flush the log buffer every 16 logs:
logger.set_max_log_buffer_size;
let mut i = 0;
loop
Logger Templates
A Logger template is a JSON file that defines the configuration of a
Logger struct. This allows you to easily manage and store logging settings in a
file.
Here’s an example of what a Logger struct looks like in JSON format:
Loading Logger from a template file:
# use Logger;
# let mut path = temp_dir;
# path.push;
# let path = &path.to_str.unwrap.to_string;
# default.save_template;
let mut logger = from_template;
Deserializing Logger from a JSON string:
# use Logger;
let raw_json = to_string
.expect;
# assert_eq!;
Saving Logger to a template file:
# use Logger;
let mut logger = default; // Create a default `Logger`
# let mut path = temp_dir;
# path.push;
# let path = &path.to_str.unwrap.to_string;
logger.save_template;