Layla-log
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.
Features list
- async (details see async)
Usage
Macros are provided to use the log writer easily, but before it here are several settings for the writer:
- dir_path
- where the log file will be saved
- the directory will be created if it does not exist
- single_length
- the maximum length of a single log file (0 as unlimited)
- file_record_level
- the minimum level of log that will be recorded to a file
- terminal_print_level
- the minimum level of log that will be printed to the terminal
- time_zone
- the time zone of the log file name and log message time
- time_detailed_display
- whether to display detailed time in log message (whether time zone is included)
- print_out
- whether to print the log to terminal
- disabled
- whether disable the logger or not
This is an example:
use *;
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_path0as the default log file single_lengthLogLevel::Traceas the default file_recode_levelLogLevel::Debugas the default terminal_print_level for debug assertions,LogLevel::Infofor default terminal_print_level for release assertions0as the default time_zone offsetfalseas the default time_detailed_displaytrueas the default print_outfalseas the default disabled
These default settings can be used by:
- Using the default setting to initialize the logger
- No explicit initialization.
Here is an example using the default setting:
use *;
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 *;
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 clean_log;
If the logger is disabled and then enabled after some codes, then the disable_log and enable_log methods can be used.
use *;
If you want to announce the level yourself instead of using the corresponding macro, you can use the log! macro.
use *;
Cases
Double initialization
calling init() for multiple times won't lead to a panic, instead, it will record a warn log:
{TIME} WARN [init @ src\logger.rs:97] Log writer had been initialized!
Method list
init(setting: Setting)disable_log()enable_log()
Macro list
trace!info!debug!warn!error!log!