123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- package logger
- import (
- "github.com/druidcaesa/gotool"
- "github.com/sirupsen/logrus"
- "iot_manager_service/config"
- "os"
- "path"
- "time"
- )
- var Logger *logrus.Logger
- func InitLog() {
- logFilePath := config.Instance().Logger.Path
- logFileName := config.Instance().Logger.Name
- err := os.MkdirAll(logFilePath, os.ModeDir)
- if err != nil {
- gotool.Logs.ErrorLog().Println(err)
- panic(err)
- }
- // 日志文件
- fileName := path.Join(logFilePath, logFileName) + time.Now().Format("20060102") + ".log"
- if !gotool.FileUtils.Exists(fileName) {
- create, err := os.Create(fileName)
- if err != nil {
- gotool.Logs.ErrorLog().Println(err)
- panic(err)
- }
- defer create.Close()
- }
- // 写入文件
- src, err := os.OpenFile(fileName, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
- if err != nil {
- panic(err)
- }
- // 实例化
- logger := logrus.New()
- // 设置输出
- logger.Out = src
- // 设置日志级别
- logger.SetLevel(logrus.DebugLevel)
- logger.Formatter = &logrus.JSONFormatter{
- TimestampFormat: "2006-01-02 15:04:05.000",
- }
- Logger = logger
- }
|