e.g. SetFormat(FORMAT_SHORTFILENAME|FORMAT_DATE|FORMAT_TIME)
FORMAT_SHORTFILENAME|FORMAT_DATE|FORMAT_TIME default format
only log content is printed(not formatted) FORMAT_NANO e.g. SetFormat(FORMAT_NANO)
long file name and number of lines FORMAT_LONGFILENAME e.g. /etc/log/logging_test.go:10
short file name and line number FORMAT_SHORTFILENAME e.g. logging_test.go:10
to date FORMAT_DATE e.g. 2023/02/14
to the second FORMAT_TIME e.g. 01:33:27
to microseconds FORMAT_MICROSECNDS e.g. 01:33:27.123456
when: SetLevel(INFO)
then: Debug("*********") will not be executed
by parity of reasoning:
when: SetLevel(ERROR)
then: Debug() and Info() and Warn() will not be executed
when: SetLevel(OFF)
All logs will not be printed
If logs need to be written to a file, set the log file
When the global log object is used, the setting method is called directly:
SetRollingDaily() Split log files by date
SetRollingByTime() Log file can be cut by hour, day, and month
SetRollingFile() Split log files by file size
SetRollingFileLoop() Split log files by file size and retain a maximum number of log files
SetGzipOn(true) Compress the split log file
log.SetRollingDaily("d:/foldTest", "log.txt")
Split the log file in this format every day: log_20221015.txt
if log_20221015.txt exists, log_20221015.1.txt will be generated
log.SetRollingByTime("d:/foldTest", "log.txt",MODE_MONTH)
Split logs by month, and keep the logs of the previous month when cross-month, such as:
log_202210.txt
log_202211.txt
log_202212.txt
log.SetRollingByTime("d:/foldTest", "log.txt",MODE_HOUR)
Split the log by hour, such as:
log_2022101506.txt
log_2022101507.txt
log_2022101508.txt
2. Split log files by file size
log.SetRollingFile("d:/foldTest", "log.txt", 300, MB)
If the file size exceeds 300MB, back up the file in the log.1.txt or log.2.txt format
the directory parameter can be empty, the current directory is used by default.
log.SetRollingFileLoop(`d:/foldTest`, "log.txt", 300, MB, 50)
Set the file size to a maximum of 300 MB and reserve only the latest 50 log files
FileOption
FileOption is an interface. There are two implementation objects FileSizeMode and FileTimeMode
FileTimeMode
Filename path of Log file
Timemode slice by the hour, the day, the month:MODE_HOUR,MODE_DAY,MODE_MONTH
Maxbuckup Maximum number of backup log files
IsCompress Whether backup files are compressed
FileSizeMode
Filename path of Log file
Maxsize The maximum log file size. If the log file size exceeds the maximum, the log file will be backed up
Maxbuckup Maximum number of backup log files
IsCompress Whether backup files are compressed