From 60f6291bd7d9c75b176232fb6fda4e4eaffacad7 Mon Sep 17 00:00:00 2001 From: ZGGSONG Date: Wed, 15 Feb 2023 09:06:56 +0800 Subject: [PATCH] feat: add log with logrus --- common/config.go | 3 ++- common/logger2.go | 26 ++++++++++++++++++++++++++ go.mod | 1 + go.sum | 3 +++ main.go | 5 +++-- message/bark.go | 8 ++++---- message/mail.go | 8 ++++---- message/message_test.go | 3 ++- service/service.go | 4 ++-- 9 files changed, 47 insertions(+), 14 deletions(-) create mode 100644 common/logger2.go diff --git a/common/config.go b/common/config.go index 4b236bc..4a787ee 100644 --- a/common/config.go +++ b/common/config.go @@ -2,6 +2,7 @@ package common import ( "github.com/fsnotify/fsnotify" + log "github.com/sirupsen/logrus" "github.com/spf13/viper" "os" "sendmsg/global" @@ -20,7 +21,7 @@ func InitConfig() (model.Config, error) { viper.AddConfigPath("../config") viper.AddConfigPath(workDir + "/config") viper.OnConfigChange(func(e fsnotify.Event) { - Log.Printf("[conf] config update: %v\n", e.Name) + log.Printf("[conf] config update: %v\n", e.Name) global.GLO_CONF_CH <- updateConfig() }) viper.WatchConfig() diff --git a/common/logger2.go b/common/logger2.go new file mode 100644 index 0000000..16c1155 --- /dev/null +++ b/common/logger2.go @@ -0,0 +1,26 @@ +package common + +import ( + log "github.com/sirupsen/logrus" + "gopkg.in/natefinch/lumberjack.v2" +) + +// InitLog2 +// +// @Description: 初始化 logrus +func InitLog2() { + logger := &lumberjack.Logger{ + Filename: "./log/log.log", + //Filename: "./log/log" + time.Now().Format("20060102_150405") + ".log", + MaxSize: 100, // 日志文件大小,单位是 MB + MaxBackups: 10, // 最大过期日志保留个数 + MaxAge: 28, // 保留过期文件最大时间,单位 天 + Compress: true, // 是否压缩日志,默认是不压缩。这里设置为true,压缩日志 + LocalTime: true, // 是否使用本地时间,默认是使用UTC时间 + } + + log.SetOutput(logger) // logrus 设置日志的输出方式 + log.SetFormatter(&log.TextFormatter{ + TimestampFormat: "2006-01-02 15:04:05", + }) +} diff --git a/go.mod b/go.mod index 598d4ad..3ff0b26 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.19 require ( github.com/fsnotify/fsnotify v1.6.0 github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible + github.com/sirupsen/logrus v1.9.0 github.com/spf13/viper v1.15.0 gopkg.in/natefinch/lumberjack.v2 v2.2.1 ) diff --git a/go.sum b/go.sum index 191b1df..9fc5c34 100644 --- a/go.sum +++ b/go.sum @@ -146,6 +146,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k= +github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0= +github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/spf13/afero v1.9.3 h1:41FoI0fD7OR7mGcKE/aOiLkGreyf8ifIOQmJANWogMk= github.com/spf13/afero v1.9.3/go.mod h1:iUV7ddyEEZPO5gA3zD4fJt6iStLlL+Lg4m2cihcDf8Y= github.com/spf13/cast v1.5.0 h1:rj3WzYc11XZaIZMPKmwP96zkFEnnAmV8s6XbB2aY32w= @@ -303,6 +305,7 @@ golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210225134936-a50acf3fe073/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ= golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/main.go b/main.go index 935bab6..d713062 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + log "github.com/sirupsen/logrus" "sendmsg/common" "sendmsg/global" "sendmsg/message" @@ -11,13 +12,13 @@ import ( func init() { /*日志初始化*/ - common.InitLog() + common.InitLog2() /*配置初始化*/ global.GLO_CONF_CH = make(chan model.Config) _conf, err := common.InitConfig() if err != nil { - common.Log.Fatalf("[init] Failed to initialize config: %v", err) + log.Fatalf("[init] Failed to initialize config: %v", err) } global.GLO_CONF = _conf } diff --git a/message/bark.go b/message/bark.go index fbfce90..32c9999 100644 --- a/message/bark.go +++ b/message/bark.go @@ -3,8 +3,8 @@ package message import ( "bytes" "encoding/json" + log "github.com/sirupsen/logrus" "net/http" - "sendmsg/common" "sendmsg/model" ) @@ -14,7 +14,7 @@ type Bark struct { } func (b *Bark) Send(body Body) { - common.Log.Printf("[bark] sending message...") + log.Printf("[bark] sending message...") var reqBody = model.BarkRequest{ DeviceKey: b.key, Title: body.Title, @@ -25,9 +25,9 @@ func (b *Bark) Send(body Body) { "application/json; charset=utf-8", bytes.NewReader(req)) if err != nil { - common.Log.Fatalf("[bark] http post failed: %v\n", err) + log.Fatalf("[bark] http post failed: %v\n", err) } defer resp.Body.Close() - common.Log.Printf("[bark] Send successful") + log.Printf("[bark] Send successful") } diff --git a/message/mail.go b/message/mail.go index 003b7a7..b2b9a34 100644 --- a/message/mail.go +++ b/message/mail.go @@ -3,8 +3,8 @@ package message import ( "fmt" "github.com/jordan-wright/email" + log "github.com/sirupsen/logrus" "net/smtp" - "sendmsg/common" ) type Mail struct { @@ -18,7 +18,7 @@ type Mail struct { } func (m *Mail) Send(body Body) { - common.Log.Printf("[mail] sending message...") + log.Printf("[mail] sending message...") e := email.NewEmail() e.From = m.FromName e.To = m.To @@ -27,7 +27,7 @@ func (m *Mail) Send(body Body) { addr := fmt.Sprintf("%v:%v", m.Host, m.Port) err := e.Send(addr, smtp.PlainAuth("", m.Username, m.Password, m.Host)) if err != nil { - common.Log.Fatalf("[mail] send failed: %v\n", err) + log.Fatalf("[mail] send failed: %v\n", err) } - common.Log.Printf("[mail] send successful") + log.Printf("[mail] send successful") } diff --git a/message/message_test.go b/message/message_test.go index 14e1766..3f475d0 100644 --- a/message/message_test.go +++ b/message/message_test.go @@ -1,6 +1,7 @@ package message import ( + log "github.com/sirupsen/logrus" "sendmsg/common" "sendmsg/global" "testing" @@ -10,7 +11,7 @@ import ( func TestSend(t *testing.T) { conf, err := common.InitConfig() if err != nil { - common.Log.Println("[test] Failed to initialize config: ", err) + log.Println("[test] Failed to initialize config: ", err) } global.GLO_CONF = conf diff --git a/service/service.go b/service/service.go index 317e50e..cf2b83f 100644 --- a/service/service.go +++ b/service/service.go @@ -1,7 +1,7 @@ package service import ( - "sendmsg/common" + log "github.com/sirupsen/logrus" "sendmsg/message" ) @@ -10,7 +10,7 @@ type Service struct { } func (s Service) Run() { - common.Log.Printf("[service] start excute...") + log.Printf("[service] start excute...") send(s.Body) }