It's a tool for free log by golang.
-
Run the order
go get "github.com/Shadas/logger"
-
Create a config object for drive the logger
loggerConfig := logger.LoggerConfig{}
-
The contents of LoggerConfig are as shown in config.sample.json
-
Add the function InitLogger to finish the configure of logger.
err := logger.InitLogger(loggerConfig) if err != nil { log.Println(err.Error()) } ...(business code)
-
Then you can use it by
logger.Log(logger.DEBUG, "debug thing1") logger.Log(logger.DEBUG, "debug thing2") logger.Log(logger.ERROR, "error thing1") logger.Log(logger.INFO, "info thing1")
-
Do not forget to close the logger in the end.
... defer logger.CloseLogger()
-
Log classify. There are 5 kinds of log can be chosen. They are DEBUG, INFO, WARNING, ERROR, FATAL low-to-high. If you print a high class log, it will be print in the lower logs at the same time.
The different log message will be written in different files.
-
Asynchronous write. The messages will be put in a channel first, one other thread will get it from the channel and write to the file.
-
Full write. If there are still some messages in the channel while the main thread is over, the CloseLogger function will ensure the messages can be written to the file before the program exit.
-
Separate file. Configure the config object, set SeparateFileByDate as true, the files will be separated by date like '2017-1-7_DEBUG.log, 2017-1-8_DEBUG.log' etc.w