Logger macro
This crate provides a simple and flexible logging system for Rust projects. It allows you to log messages of different types (error, warn, info, debug) using macros and choose whether to log messages to the console, a file, or both.
Features
- Log messages of different types (error, warn, info, debug) using macros
- Choose whether to log messages to the console, a file, or both
- Set the log level at runtime
- Automatically create and append to log files based on the current date
- Handle errors when writing to log files
Usage
To use this crate in your project, add it as a dependency in your Cargo.toml
file:
[dependencies]
logger-rust = "0.2.12"
Then, import the crate:
use *;
You can now use the log_error!
, log_warn!
, log_info!
, log_debug
and log_trace!
macros to log messages of different types:
log_trace! example:
Output:
2023-06-09 15:06:46 [TRACE] src\main.rs:L23/C5 - used: my_struct.do_something() ->> (()): () | Type: <()> | ThreadId(1) ->> Timestamp: UN1686305206652692IX | Module: debug
2023-06-09 15:06:46 [TRACE] src\main.rs:L26/C5 - used: x ->> (5): 5 | Type: <i32> | ThreadId(1) ->> Timestamp: UN1686305206653039IX | Module: debug
2023-06-09 15:06:46 [TRACE] src\main.rs:L29/C5 - used: person ->> (Person { name: "Alice", age: 20 }): Person { name: "Alice", age: 20 } | Type: <debug::Person> | ThreadId(1) ->> Timestamp: UN1686305206653281IX ->> Context: <Alice> | Module: debug
By default, log messages are printed to the console. You can use the set_log_level
function to specify where log messages should be written:
use *;
The set_log_level
function takes a LogLevel as an argument. You can pass one of the following variants to specify where log messages should be written:
LogLevel::Console
: Log messages are printed to the console only.LogLevel::File
: Log messages are written to a file only.LogLevel::Both
: Log messages are printed to the console and written to a file.
When logging messages to a file, the crate will automatically create a new file with a name based on the current date (e.g. 2023-06-04.log) or append to an existing file with the same name. If an error occurs while writing to the file (e.g. if the file is not accessible), an error message will be printed to the console.
Also, you can set custom log path. The default path is the same as the path where is your Cargo.toml
file located.
The set_log_path
function takes a string
as an argument. You can pass one of the following variants to specify where log messages should be written:
use *;
Will create a log file on directory that you specified.
Note that if you use "." as dir (which is really not necessary lol), you will get an error message because directory is already busy.
Log rotation
From version 1.0.39, you can create a log rotator
instance which allows you to split logs by their size and duration.
- log_path: path to log directory;
- max_size (
u64
): maximum size of log file; - max_life (
std::time::Duration
): maximum lifetime of log file;
Here's an example:
use *; // importing logger
use ;
Note that you SHOULD NOT use LogRotator and LogPath in single instance. You will block the log file.
Examples
Here’s an example that shows how to use this crate in a Rust project:
use *;
Also if you want, you can add set_log_path(LogConfig::Path(LogPath::From("string")
method:
use *;
Also you can use this:
error;
warn;
info;
Output:
- 2023-06-05 12:23:25 [ERROR] An error occurred: Something went wrong
- 2023-06-05 12:23:25 [WARN] A warning occurred: Something might be wrong
A warning occurred: Something might be wrong
+ 2023-06-05 12:23:25 [INFO] An info message: Something happened
An info message: Something happened
+ 2023-06-05 12:23:25 [DEBUG] A debug message: Something happened in detail
A debug message: Something happened in detail
Custom logging
After 0.1.2 version, you can create a custom logging function with macros.
- Create a
class
named as function that you needed (e.g.custom
):
Note that log_message functions requires 3 arguments: now
, message
, but you can use whatever you want.
2. Create a macros for method which you created:
Note that arg
requires 2 arguments.
3. Use it:
Since you have fn custom
, you can also use this:
custom;
It does the same, but you can provide a value instead of ¤t_time()
. But it requires 2 arguments:
- An
&str
now
in our contex - A
string
value that contains message.