Crates.io | layla-log |
lib.rs | layla-log |
version | 0.2.9 |
source | src |
created_at | 2024-03-31 13:07:47.457268 |
updated_at | 2024-11-14 05:42:34.094836 |
description | A simple logger library. |
homepage | https://github.com/ILikeLayla/Layla-log |
repository | |
max_upload_size | |
id | 1191664 |
size | 22,604 |
A simple logger library. This library provides a simple log writer and simple log level control. It can record logs to a target directory and also print to the terminal. The log can be set to different levels (Error, Warn, Debug, Info and Trace). And only the logs with high enough level will be recorded to file or printed to terminal. Moreover, when the log file size exceeds a certain limit, it will automatically route to new files with indexing.
Macros are provided to use the log writer easily, but before it here are several setting for the writer:
And this is an example:
use layla_log::*;
fn main() {
init("/path/to/dir", 1219, LogLevel::Trace, LogLevel::Debug, 0, true, true);
error!("This is an error message");
warn!("This is a warning message");
debug!("This is a debug message");
info!("This is an info message");
trace!("This is a trace message");
}
and these are the output in the log file:
{TIME} (+00:00) ERROR This is an error message
{TIME} (+00:00) WARN This is a warning message
{TIME} (+00:00) DEBUG This is a debug message
{TIME} (+00:00) INFO This is an info message
{TIME} (+00:00) TRACE This is a trace message
and these are the output in the terminal:
{TIME} (+00:00) ERROR This is an error message
{TIME} (+00:00) WARN This is a warning message
{TIME} (+00:00) DEBUG This is a debug message
{TIME} (+00:00) INFO This is an info message
Furthermore, all the setting have a default value:
"./logs/"
as the default dir_path200
as the default log file single_lengthLogLevel::Trace
as the default file_recode_levelLogLevel::Debug
as the default terminal_print_level0
as the default time_zone offsetfalse
as the default time_detailed_displaytrue
as the default print_outThese default settings can be used by:
default_init()
to initialize the loggerHere is an example using default_init()
:
use layla_log::*;
fn main() {
init("/path/to/dir"); // dir_path is still needed
error!("This is an error message");
warn!("This is a warning message");
debug!("This is a debug message");
info!("This is an info message");
trace!("This is a trace message");
}
and these are the output in the log file:
{TIME} (+00:00) ERROR This is an error message
{TIME} (+00:00) WARN This is a warning message
{TIME} (+00:00) DEBUG This is a debug message
{TIME} (+00:00) INFO This is an info message
{TIME} (+00:00) TRACE This is a trace message
and these are the output in the terminal:
{TIME} (+00:00) ERROR This is an error message
{TIME} (+00:00) WARN This is a warning message
{TIME} (+00:00) DEBUG This is a debug message
Here is an example without any explicit initialization:
use layla_log::*;
fn main() {
error!("This is an error message");
warn!("This is a warning message");
debug!("This is a debug message");
info!("This is an info message");
trace!("This is a trace message");
}
and the both output in the log file and the terminal are the same as using default_init()
.
In some case, log is only used for debugging, and need to clean the log files each time the program runs, then clean_log()
can be applied to clear the log file.
use layla_log::clean_log;
fn main() {
clean_log();
}
This happens when init()
or default_init()
is called more than once. In this case, logger won't be initialized again, but a warn log will be recorded (printed) with content "Log writer has been initialized!"
.