Crates.io | layla-log |
lib.rs | layla-log |
version | |
source | src |
created_at | 2024-03-31 13:07:47.457268+00 |
updated_at | 2025-03-28 15:23:15.076496+00 |
description | A simple logger library. |
homepage | https://github.com/ILikeLayla/Layla-log |
repository | |
max_upload_size | |
id | 1191664 |
Cargo.toml error: | TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
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 them to the terminal. The log can be set to different levels (Error, Warn, Debug, Info and Trace). Only the logs with significant levels will be recorded to file or printed to the 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 settings for the writer:
whether disable the logger or not
This is an example:
use layla_log::*;
fn main() {
init(Setting {
dir_path: "/path/to/dir",
single_length: 1219,
file_record_level: LogLevel::Trace,
terminal_print_level: LogLevel::Debug,
time_detailed_display: true,
time_zone: 0,
print_out: true,
disabled: false
});
error!("This is an error message");
warn!("This is a warning message");
info!("This is an info message");
debug!("This is a debug message");
trace!("This is a trace message");
}
and these are the output in the log file:
{TIME} (+00:00) ERROR [main@src\main.rs:14] This is an error message
{TIME} (+00:00) WARN [main@src\main.rs:15] This is a warning message
{TIME} (+00:00) INFO [main@src\main.rs:17] This is an info message
{TIME} (+00:00) DEBUG [main@src\main.rs:16] This is a debug message
{TIME} (+00:00) TRACE [main@src\main.rs:18] This is a trace message
and these are the output in the terminal:
{TIME} (+00:00) ERROR [main@src\main.rs:14] This is an error message
{TIME} (+00:00) WARN [main@src\main.rs:15] This is a warning message
{TIME} (+00:00) INFO [main@src\main.rs:17] This is an info message
{TIME} (+00:00) DEBUG [main@src\main.rs:16] This is a debug message
Furthermore, all the settings have a default value:
"./logs/"
as the default dir_path0
as the default log file single_lengthLogLevel::Trace
as the default file_recode_levelLogLevel::Debug
as the default terminal_print_level for debug assertions, LogLevel::Info
for default terminal_print_level for release assertions0
as the default time_zone offsetfalse
as the default time_detailed_displaytrue
as the default print_outfalse
as the default disabledThese default settings can be used by:
Here is an example using default_init()
:
use layla_log::*;
fn main() {
init(Setting::default());
error!("This is an error message");
warn!("This is a warning message");
info!("This is an info message");
debug!("This is a debug message");
trace!("This is a trace message");
}
and these are the output in the log file:
{TIME} ERROR [main@src\main.rs:14] This is an error message
{TIME} WARN [main@src\main.rs:15] This is a warning message
{TIME} INFO [main@src\main.rs:17] This is an info message
{TIME} DEBUG [main@src\main.rs:16] This is a debug message
{TIME} TRACE [main@src\main.rs:18] This is a trace message
and these are the output in the terminal when release assertions:
{TIME} ERROR [main@src\main.rs:14] This is an error message
{TIME} WARN [main@src\main.rs:15] This is a warning message
{TIME} INFO [main@src\main.rs:17] This is an info 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");
info!("This is an info message");
debug!("This is a debug message");
trace!("This is a trace message");
}
and the both output in the log file and the terminal are the same as using the first method.
In some cases, 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();
}
And also, if some setting changes after a while, then a set
method can be used. (details see init and set)
use layla_log::{init, set, Setting};
fn main() {
init(Setting {
// something
});
// something
set(Setting {
// something
})
}
If the logger is disabled and then enabled after some codes, then the disable_log
and enable_log
methods can be used.
use layla_log::*;
fn main() {
disable_log();
// logs will not be recorded or printed
error!("This is an error message");
warn!("This is a warning message");
info!("This is an info message");
debug!("This is a debug message");
trace!("This is a trace message");
enable_log();
// logs can be recorded or printed (with significant level)
error!("This is an error message");
warn!("This is a warning message");
info!("This is an info message");
debug!("This is a debug message");
trace!("This is a trace message");
}
If you want to announce the level yourself instead of using the corresponding macro, you can use the log!
macro.
use layla_log::*;
fn main() {
log!(LogLevel::Trace, "Hello, {}!", "world");
}
init(setting: Setting)
set(setting: Setting)
disable_log()
enable_log()
trace!
info!
debug!
warn!
error!
log!