casual_logger
There are only features that are used for self-study of programming examples.
Not for production, but better than not logging anything.
Interested:
- Rotate log by date.
- Delete old log files. (semi-automatic)
- Write the log as a TOML table. A human-readable format that can be analyzed by a computer.
Not interested:
- Only write to 1 file on working directory.
- The file path cannot be set.
- There is no configuration file.
Do you have this problem?
(Problem-1)
I wanted to self-learn short programming with a logger, but setting up a logger is difficult than that short programming. For example, a tic-tac-toe program. The logging period is short.
(Problem-2)
Writing a log parser is tedious.
This logger solves the problem in this way.
(Problem-1 solution)
All features are described in one copy and paste. See "At second, Overall" view below.
(Problem-2 solution)
Write the log as a TOML table. A human-readable format that can be analyzed by a computer.
At first, Disclaim
- It differs from the standard Rust log interface.
- Ignore performance for ease of use and ease of explanation.
- You can break the toml format. Do not validate.
- Depending on the version of this program, the log writing order may be unstable. Check the serial "Seq" number.
- If the log export fails, the error is ignored and it continues.
- There is a waiting time of 1 second or more before the logger ends.
- Don't forget wait for logging to complete at end of program.
At second, Overall view
Your code:
//! All features are described in one copy and paste.
use ;
Output ./default-2020-07-14.log.toml
auto generated:
[]
= "Remove 0 files.\r\n"
[]
= """
Hello, world!!
こんにちわ、世界!!\r\n
"""
[]
= "x is 100.\r\n"
[]
= "( 1)TRACE"
[]
= "( 2)trace-line\r\n"
[]
= "( 3)DEBUG"
[]
= "( 4)debug-line\r\n"
[]
= "( 5)INFO"
[]
= "( 6)info-line\r\n"
[]
= "( 7)NOTICE"
[]
= "( 8)notice-line\r\n"
[]
= "( 9)WARN"
[]
= "(10)warn-line\r\n"
[]
= "(11)ERROR"
[]
= "(12)error-line\r\n"
[]
= "(13)FATAL"
[]
= "(14)fatal-line\r\n"
[]
= """
The sky is from top to bottom!!
上から下まで空です!!\r\n
"""
= "fly in the sky."
= 2
= """
climb
a tall
tree.
"""
[]
= "(7)Trace on (7)Trace."
[]
= "(6)Debug on (7)Trace."
[]
= "(5)Info on (7)Trace."
[]
= "(4)Notice on (7)Trace."
[]
= "(3)Warn on (7)Trace."
[]
= "(2)Error on (7)Trace."
[]
= "(1)Fatal on (7)Trace."
[]
= "(6)Debug on (6)debug."
[]
= "(5)Info on (6)debug."
[]
= "(4)Notice on (6)debug."
[]
= "(3)Warn on (6)debug."
[]
= "(2)Error on (6)debug."
[]
= "(1)Fatal on (6)debug."
[]
= "(5)Info on (5)Info."
[]
= "(4)Notice on (5)Info."
[]
= "(3)Warn on (5)Info."
[]
= "(2)Error on (5)Info."
[]
= "(1)Fatal on (5)Info."
[]
= "(4)Notice on (4)Notice."
[]
= "(3)Warn on (4)Notice."
[]
= "(2)Error on (4)Notice."
[]
= "(1)Fatal on (4)Notice."
[]
= "(3)Warn on (3)Warn."
[]
= "(2)Error on (3)Warn."
[]
= "(1)Fatal on (3)Warn."
[]
= "(2)Error on (2)Error."
[]
= "(1)Fatal on (2)Error."
[]
= "(1)Fatal on (1)Fatal."
Output to terminal:
casual_logger fatal: 0 sec(s). 16 table(s) left.
casual_logger fatalln: 0 sec(s). Wait for 1 thread(s).
casual_logger fatal: 0 sec(s). Wait for 2 thread(s).
casual_logger fatal: 1 sec(s). 3 table(s) left.
casual_logger fatal: 0 sec(s). Wait for 3 thread(s).
casual_logger fatal: 0 sec(s). Wait for 5 thread(s).
casual_logger fatal: 0 sec(s). Wait for 4 thread(s).
casual_logger fatal: 0 sec(s). Wait for 3 thread(s).
casual_logger fatal: 0 sec(s). Wait for 2 thread(s).
casual_logger fatal: 0 sec(s). Wait for 1 thread(s).
It is designed to use Log::fatal()
as the first argument for panic!()
. It is the abnormal termination of the program. There is a waiting time.
At third, Description
Code:
use ;
At the timing of the first writing, a file with a
time stamp in its name is automatically generated.
For example: ./tic-tac-toe-2020-07-12.log.toml
Description:
Part | Name | Description | Default |
---|---|---|---|
./ |
file path | Working directory | |
only. | |||
tic-tac-toe |
Prefix | Editable. | default |
-2020-07-12 |
StartDate | Auto generated. | |
.log |
Suffix | Editable. | .log |
.toml |
Extension | Editable. | .toml |
It is difficult to explain the file path for beginners.
Therefore, it does not move.
Excite yourself with a prefix.
StartDate is basically today.
If the rotation fails, it is the start date.
Suffix to be safe, include a word that
clearly states that you can delete the file.
If you don't like the .toml extension, leave
the suffix empty and the .log extension.
Set up, Code:
Log rotation, Code:
let remove_num = if let Ok = LOGGER.lock else ;
noticeln;
Logger Properties
Name | Description | Default |
---|---|---|
retention_days |
After this number of days, | 7 |
the file will be deleted. | ||
level |
Used to switch between | Trace |
write and non-write. |
Example of retention_days:
retention_days
is 2.- Today is 2020-07-12.
- The
./default-2020-07-09.log.toml
file will be deleted. - The
./default-2020-07-10.log.toml
remains. - Delete old files by date in filename.
Example of level:
- There are 7 log levels.
|Fatal< Error < Warn < Notice < Info < Debug <Trace|
|<-- Small ------------------------------ Large -->|
|<-- Concise -------------------------- Verbose -->|
|<-- Low Level --------------------- High level -->|
|<-- High priority --------------- Low priority -->|
Level | Examle of use. |
---|---|
Fatal |
If the program cannot continue. |
Error |
I didn't get the expected result, |
so I'll continue with the other method. | |
Warn |
It will be abnormal soon, |
but there is no problem and you can ignore it. | |
For example: | |
(1) He reported that it took longer to access | |
than expected. | |
(2) Report that capacity is approaching the limit. | |
Notice |
It must be enabled in the server production |
environment. | |
Record of passing important points correctly. | |
We are monitoring that it is working properly. | |
Info |
Report highlights. |
Everything that needs to be reported regularly in | |
the production environment. | |
Debug |
It should be in a place with many accidents. |
This level is disabled in production environments. | |
Leave it in the source and enable it for | |
troubleshooting. | |
Often, this is the production level of a desktop | |
operating environment. | |
Trace |
Not included in the distribution. |
Remove this level from the source after using it | |
for debugging. | |
If you want to find a bug in the program, | |
write a lot. |
Code:
// Multi-line string.
// The suffix "ln" adds a newline at the end.
infoln;
// After explicitly checking the level.
if enabled
// The level is implicitly confirmed.
trace;
traceln;
debug;
debugln;
info;
infoln;
notice;
noticeln;
warn;
warnln;
error;
errorln;
fatal;
fatalln;
A piece of advice.
// Fatal is Panic! Can be used as the first argument of.
panic!;
Fatal returns a string so you can try to record a panic message.
However, the last log may not be written if the program exits first.
So there is a fatal_timeout_secs parameter.
Code:
if let Ok = LOGGER.lock
Usage of Table
Static method | Description |
---|---|
::default() |
Create a instance. |
Instance method | Description |
---|---|
.str(key, value) |
Insert a string. |
Multi-line string are | |
output with multiple lines. | |
.literal(key, value) |
Not enclose this value in |
quotation marks. | |
You can break the toml format. | |
Do not validate. |
Do not include spaces in the key. TOML collapses.
It is difficult to explain to beginners how to use TOML.
If you make a TOML that cannot be parsed literally,
please correct it.
Code:
// Suffix '_t'. TOML say a table. So-called map.
infoln_t;
Output:
[]
= """
The sky is from top to bottom!!
上から下まで空です!!\r\n
"""
= "fly in the sky."
= 2
= """
climb
a tall
tree.
"""
Don't forget wait for logging to complete at end of program
Code:
// Wait for logging to complete. Time out 30 seconds.
wait_for_logging_to_complete;
If you do not wait,
the program will exit before writing all the logs.
TODO
- Dogfooding.
Tested environment
- OS:
Windows 10
. - Editor:
Visual studio code
.
Appendix
Customize method
Code: main.rs
use Log;
Usage: other.rs
use crate LogExt;