Skip to content

Commit 2742522

Browse files
committed
refactor: migrate logging to zerolog
1 parent 0e7a157 commit 2742522

24 files changed

Lines changed: 176 additions & 157 deletions

File tree

cmd/observer/app.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func appStart(ver *semver.Version, build *unibuild.UniBuild, args arguments) {
7373
if err := conf.Migrate(logger.GetLogger(main)); err != nil {
7474
logger.GetLogger(main).Fatalln(err)
7575
}
76-
logger.GetLogger(main).Info("global configuration has been loaded")
76+
logger.GetLogger(main).Infoln("global configuration has been loaded")
7777

7878
if conf.Server.Debug {
7979
go func() {
@@ -114,7 +114,7 @@ func appStart(ver *semver.Version, build *unibuild.UniBuild, args arguments) {
114114
if err = daoObj.Open(conf.Database.Database); err != nil {
115115
logger.GetLogger(main).Fatalln(err)
116116
}
117-
logger.GetLogger(main).Info("database connection has been established")
117+
logger.GetLogger(main).Infoln("database connection has been established")
118118

119119
if err = daoObj.AutoMigrate(
120120
&model.SchemaVersion{},
@@ -124,7 +124,7 @@ func appStart(ver *semver.Version, build *unibuild.UniBuild, args arguments) {
124124
); err != nil {
125125
logger.GetLogger(main).Fatalln(err)
126126
}
127-
logger.GetLogger(main).Info("database schema has been configured")
127+
logger.GetLogger(main).Infoln("database schema has been configured")
128128
actionHandler := action.NewHandler(daoObj)
129129

130130
var hardwareDevice hardware.IHardware
@@ -314,9 +314,9 @@ func appStart(ver *semver.Version, build *unibuild.UniBuild, args arguments) {
314314
sendHardwareAbortSignal()
315315
runCleanerTasks()
316316
if warn {
317-
logger.GetLogger(main).Warn(reason)
317+
logger.GetLogger(main).Warnln(reason)
318318
} else {
319-
logger.GetLogger(main).Info(reason)
319+
logger.GetLogger(main).Infoln(reason)
320320
}
321321
if callback != nil {
322322
callback()

config/base.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package config
33
import (
44
"fmt"
55

6+
_logger "github.com/anyshake/observer/pkg/logger"
67
"github.com/go-playground/validator/v10"
7-
"github.com/sirupsen/logrus"
88
"github.com/spf13/viper"
99
)
1010

@@ -81,7 +81,7 @@ func (c *BaseConfig) Parse(configPath, configType string) error {
8181
return nil
8282
}
8383

84-
func (cfg *BaseConfig) Migrate(logger *logrus.Entry) error {
84+
func (cfg *BaseConfig) Migrate(logger *_logger.Adapter) error {
8585
// 2025-08-22: starting from v4.2.0, NTP Client configuration has deprecated the `endpoint` field.
8686
if cfg.NtpClient.Endpoint != "" && len(cfg.NtpClient.Pool) == 0 {
8787
cfg.NtpClient.Pool = []string{cfg.NtpClient.Endpoint}

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ require (
77
github.com/PuerkitoBio/goquery v1.10.3
88
github.com/alphadose/haxmap v1.4.1
99
github.com/ameshkov/dnscrypt/v2 v2.4.0
10-
github.com/antonfisher/nested-logrus-formatter v1.3.1
1110
github.com/appleboy/gin-jwt/v2 v2.10.1
1211
github.com/bclswl0827/eewgo v0.1.0
1312
github.com/bclswl0827/go-serial v0.0.1
@@ -41,10 +40,10 @@ require (
4140
github.com/miekg/dns v1.1.65
4241
github.com/ncruces/go-sqlite3 v0.24.0
4342
github.com/ncruces/go-sqlite3/gormlite v0.24.0
43+
github.com/rs/zerolog v1.35.0
4444
github.com/samber/lo v1.51.0
4545
github.com/sbabiv/xml2map v1.2.1
4646
github.com/shirou/gopsutil/v4 v4.25.1
47-
github.com/sirupsen/logrus v1.9.3
4847
github.com/spf13/viper v1.19.0
4948
github.com/vardius/message-bus v1.1.5
5049
github.com/vektah/gqlparser/v2 v2.5.30
@@ -134,6 +133,7 @@ require (
134133
github.com/longbridgeapp/sqlparser v0.3.1 // indirect
135134
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
136135
github.com/magiconair/properties v1.8.7 // indirect
136+
github.com/mattn/go-colorable v0.1.14 // indirect
137137
github.com/mitchellh/mapstructure v1.5.0 // indirect
138138
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
139139
github.com/modern-go/reflect2 v1.0.2 // indirect

go.sum

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@ github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883 h1:bvNMNQO63//z+xNg
6060
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo29Kk6CurOXKm700vrz8f0KW0JNfpkRJY/8=
6161
github.com/andybalholm/cascadia v1.3.3 h1:AG2YHrzJIm4BZ19iwJ/DAua6Btl3IwJX+VI4kktS1LM=
6262
github.com/andybalholm/cascadia v1.3.3/go.mod h1:xNd9bqTn98Ln4DwST8/nG+H0yuB8Hmgu1YHNnWw0GeA=
63-
github.com/antonfisher/nested-logrus-formatter v1.3.1 h1:NFJIr+pzwv5QLHTPyKz9UMEoHck02Q9L0FP13b/xSbQ=
64-
github.com/antonfisher/nested-logrus-formatter v1.3.1/go.mod h1:6WTfyWFkBc9+zyBaKIqRrg/KwMqBbodBjgbHjDz7zjA=
6563
github.com/appleboy/gin-jwt/v2 v2.10.1 h1:I68+9qGsgHDx8omd65MKhYXF7Qz5LtdFFTsB/kSU4z0=
6664
github.com/appleboy/gin-jwt/v2 v2.10.1/go.mod h1:xuzn4aNUwqwR3+j+jbL6MhryiRKinUL1SJ7WUfB33vU=
6765
github.com/appleboy/gofight/v2 v2.1.2 h1:VOy3jow4vIK8BRQJoC/I9muxyYlJ2yb9ht2hZoS3rf4=
@@ -310,6 +308,8 @@ github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 h1:6E+4a0GO5zZEnZ
310308
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I=
311309
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
312310
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
311+
github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=
312+
github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
313313
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
314314
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
315315
github.com/microsoft/go-mssqldb v1.6.0 h1:mM3gYdVwEPFrlg/Dvr2DNVEgYFG7L42l+dGc67NNNpc=
@@ -369,6 +369,8 @@ github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94
369369
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
370370
github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=
371371
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
372+
github.com/rs/zerolog v1.35.0 h1:VD0ykx7HMiMJytqINBsKcbLS+BJ4WYjz+05us+LRTdI=
373+
github.com/rs/zerolog v1.35.0/go.mod h1:EjML9kdfa/RMA7h/6z6pYmq1ykOuA8/mjWaEvGI+jcw=
372374
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
373375
github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ=
374376
github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4=
@@ -382,8 +384,6 @@ github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8=
382384
github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I=
383385
github.com/shirou/gopsutil/v4 v4.25.1 h1:QSWkTc+fu9LTAWfkZwZ6j8MSUk4A2LV7rbH0ZqmLjXs=
384386
github.com/shirou/gopsutil/v4 v4.25.1/go.mod h1:RoUCUpndaJFtT+2zsZzzmhvbfGoDCJ7nFXKJf8GqJbI=
385-
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
386-
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
387387
github.com/songgao/water v0.0.0-20200317203138-2b4b6d7c09d8 h1:TG/diQgUe0pntT/2D9tmUCz4VNwm9MfrtPr0SU2qSX8=
388388
github.com/songgao/water v0.0.0-20200317203138-2b4b6d7c09d8/go.mod h1:P5HUIBuIWKbyjl083/loAegFkfbFNx5i2qEP4CNbm7E=
389389
github.com/sosodev/duration v1.3.1 h1:qtHBDMQ6lvMQsL15g4aopM4HEfOaYuhWBw3NPTtlqq4=
@@ -569,7 +569,6 @@ golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7w
569569
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
570570
golang.org/x/sys v0.0.0-20210616045830-e2b7044e8c71/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
571571
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
572-
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
573572
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
574573
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
575574
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

internal/hardware/explorer/proto_v1.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,20 @@ import (
1212
"unsafe"
1313

1414
"github.com/anyshake/observer/pkg/fifo"
15+
"github.com/anyshake/observer/pkg/logger"
1516
"github.com/anyshake/observer/pkg/message"
1617
"github.com/anyshake/observer/pkg/metadata"
1718
"github.com/anyshake/observer/pkg/ntpclient"
1819
"github.com/anyshake/observer/pkg/ringbuf"
1920
"github.com/anyshake/observer/pkg/timesource"
2021
"github.com/anyshake/observer/pkg/transport"
21-
"github.com/sirupsen/logrus"
2222
)
2323

2424
type ExplorerProtoImplV1 struct {
2525
ChannelCodes []string
2626
ExplorerOptions ExplorerOptions
2727
NtpOptions NtpOptions
28-
Logger *logrus.Entry
28+
Logger *logger.Adapter
2929
TimeSource *timesource.Source
3030

3131
Transport transport.ITransport
@@ -246,7 +246,7 @@ func (g *ExplorerProtoImplV1) Open(ctx context.Context) (context.Context, contex
246246
for {
247247
select {
248248
case <-subCtx.Done():
249-
g.Logger.Info("exiting from data packet reader")
249+
g.Logger.Infoln("exiting from data packet reader")
250250
return
251251
default:
252252
}
@@ -347,7 +347,7 @@ func (g *ExplorerProtoImplV1) Open(ctx context.Context) (context.Context, contex
347347
g.deviceStatus.SetUpdatedAt(time.UnixMilli(timestamp))
348348
}
349349
case <-subCtx.Done():
350-
g.Logger.Info("exiting from data packet decoder")
350+
g.Logger.Infoln("exiting from data packet decoder")
351351
timer.Stop()
352352
return
353353
}
@@ -358,7 +358,7 @@ func (g *ExplorerProtoImplV1) Open(ctx context.Context) (context.Context, contex
358358
for timer := time.NewTimer(resyncInterval); ; {
359359
select {
360360
case <-timer.C:
361-
g.Logger.Info("re-synchronizing time with NTP servers")
361+
g.Logger.Infoln("re-synchronizing time with NTP servers")
362362
offset, server, err := ntpClient.Query()
363363
if err != nil {
364364
g.Logger.Warnf("error occurred while re-synchronizing time with NTP: %v", err)

internal/hardware/explorer/proto_v2.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,21 @@ import (
1414
"unsafe"
1515

1616
"github.com/anyshake/observer/pkg/fifo"
17+
"github.com/anyshake/observer/pkg/logger"
1718
"github.com/anyshake/observer/pkg/message"
1819
"github.com/anyshake/observer/pkg/metadata"
1920
"github.com/anyshake/observer/pkg/ntpclient"
2021
"github.com/anyshake/observer/pkg/ringbuf"
2122
"github.com/anyshake/observer/pkg/timesource"
2223
"github.com/anyshake/observer/pkg/transport"
2324
"github.com/samber/lo"
24-
"github.com/sirupsen/logrus"
2525
)
2626

2727
type ExplorerProtoImplV2 struct {
2828
ChannelCodes []string
2929
ExplorerOptions ExplorerOptions
3030
NtpOptions NtpOptions
31-
Logger *logrus.Entry
31+
Logger *logger.Adapter
3232
TimeSource *timesource.Source
3333

3434
Transport transport.ITransport
@@ -277,7 +277,7 @@ func (g *ExplorerProtoImplV2) Open(ctx context.Context) (context.Context, contex
277277
for timeSourceInitialized := false; ; {
278278
select {
279279
case <-subCtx.Done():
280-
g.Logger.Info("exiting from data packet reader")
280+
g.Logger.Infoln("exiting from data packet reader")
281281
if atomic.LoadInt32(&initFlag) == 0 {
282282
close(readyChan)
283283
}
@@ -501,7 +501,7 @@ func (g *ExplorerProtoImplV2) Open(ctx context.Context) (context.Context, contex
501501
g.deviceStatus.IncrementFrames()
502502
g.deviceStatus.SetUpdatedAt(time.UnixMilli(int64(timestamp)))
503503
case <-subCtx.Done():
504-
g.Logger.Info("exiting from data packet decoder")
504+
g.Logger.Infoln("exiting from data packet decoder")
505505
timer.Stop()
506506
return
507507
}
@@ -525,7 +525,7 @@ func (g *ExplorerProtoImplV2) Open(ctx context.Context) (context.Context, contex
525525
timer.Reset(resyncInterval)
526526
continue
527527
}
528-
g.Logger.Info("re-synchronizing time with NTP servers")
528+
g.Logger.Infoln("re-synchronizing time with NTP servers")
529529
offset, server, err := ntpClient.Query()
530530
if err != nil {
531531
g.Logger.Warnf("error occurred while re-synchronizing time with NTP: %v", err)

internal/hardware/explorer/proto_v3.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ import (
1313
"time"
1414

1515
"github.com/anyshake/observer/pkg/fifo"
16+
"github.com/anyshake/observer/pkg/logger"
1617
"github.com/anyshake/observer/pkg/message"
1718
"github.com/anyshake/observer/pkg/metadata"
1819
"github.com/anyshake/observer/pkg/ntpclient"
1920
"github.com/anyshake/observer/pkg/ringbuf"
2021
"github.com/anyshake/observer/pkg/timesource"
2122
"github.com/anyshake/observer/pkg/transport"
2223
"github.com/samber/lo"
23-
"github.com/sirupsen/logrus"
2424
)
2525

2626
type explorerProtocolPacketV3 struct {
@@ -32,7 +32,7 @@ type ExplorerProtoImplV3 struct {
3232
ChannelCodes []string
3333
ExplorerOptions ExplorerOptions
3434
NtpOptions NtpOptions
35-
Logger *logrus.Entry
35+
Logger *logger.Adapter
3636
TimeSource *timesource.Source
3737

3838
Transport transport.ITransport
@@ -280,7 +280,7 @@ func (g *ExplorerProtoImplV3) Open(ctx context.Context) (context.Context, contex
280280
for timeSourceInitialized := false; ; {
281281
select {
282282
case <-subCtx.Done():
283-
g.Logger.Info("exiting from data packet reader")
283+
g.Logger.Infoln("exiting from data packet reader")
284284
if atomic.LoadInt32(&initFlag) == 0 {
285285
close(readyChan)
286286
}
@@ -314,7 +314,7 @@ func (g *ExplorerProtoImplV3) Open(ctx context.Context) (context.Context, contex
314314
cancelFn()
315315
}
316316
if timeout {
317-
g.Logger.Error("timeout when reading data from transport")
317+
g.Logger.Errorln("timeout when reading data from transport")
318318
continue
319319
}
320320

@@ -518,13 +518,13 @@ func (g *ExplorerProtoImplV3) Open(ctx context.Context) (context.Context, contex
518518
g.messageBus.Publish(g.packetTimeObj, &g.deviceConfig, &g.deviceVariable, g.channelDataBuf)
519519
g.deviceStatus.IncrementMessages()
520520
} else {
521-
g.Logger.Warn("collected samples exceeded the sample rate, resetting counters")
521+
g.Logger.Warnln("collected samples exceeded the sample rate, resetting counters")
522522
g.resetVariables()
523523
}
524524

525525
g.resetFlags()
526526
case <-subCtx.Done():
527-
g.Logger.Info("exiting from data packet decoder")
527+
g.Logger.Infoln("exiting from data packet decoder")
528528
timer.Stop()
529529
return
530530
}
@@ -551,7 +551,7 @@ func (g *ExplorerProtoImplV3) Open(ctx context.Context) (context.Context, contex
551551
timer.Reset(resyncInterval)
552552
continue
553553
}
554-
g.Logger.Info("re-synchronizing time with NTP servers")
554+
g.Logger.Infoln("re-synchronizing time with NTP servers")
555555
offset, server, err := ntpClient.Query()
556556
if err != nil {
557557
g.Logger.Warnf("error occurred while re-synchronizing time with NTP: %v", err)

internal/hardware/new.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import (
66
"github.com/anyshake/observer/config"
77
"github.com/anyshake/observer/internal/dao/action"
88
"github.com/anyshake/observer/internal/hardware/explorer"
9+
"github.com/anyshake/observer/pkg/logger"
910
"github.com/anyshake/observer/pkg/timesource"
1011
"github.com/anyshake/observer/pkg/transport"
11-
"github.com/sirupsen/logrus"
1212
)
1313

14-
func New(logger *logrus.Entry, timeSrc *timesource.Source, actionHandler *action.Handler, explorerOptions explorer.ExplorerOptions, ntpOptions explorer.NtpOptions) (IHardware, error) {
14+
func New(logger *logger.Adapter, timeSrc *timesource.Source, actionHandler *action.Handler, explorerOptions explorer.ExplorerOptions, ntpOptions explorer.NtpOptions) (IHardware, error) {
1515
tr, err := transport.New(explorerOptions.Endpoint, explorerOptions.ReadTimeout)
1616
if err != nil {
1717
return nil, fmt.Errorf("failed to create hardware transport: %w", err)

internal/hook/cleaner/close_database/execute.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66

77
func (d *CloseDatabaseCleanerImpl) Execute() error {
88
if d.DAO != nil {
9-
logger.GetLogger(d.GetName()).Info("closing connection to database")
10-
defer logger.GetLogger(d.GetName()).Info("database connection has been closed")
9+
logger.GetLogger(d.GetName()).Infoln("closing connection to database")
10+
defer logger.GetLogger(d.GetName()).Infoln("database connection has been closed")
1111
return d.DAO.Close()
1212
}
1313

internal/hook/cleaner/close_explorer/execute.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66

77
func (p *CloseExplorerCleanerImpl) Execute() error {
88
if p.HardwareDev != nil {
9-
logger.GetLogger(p.GetName()).Info("closing connection to hardware")
10-
defer logger.GetLogger(p.GetName()).Info("hardware connection has been closed")
9+
logger.GetLogger(p.GetName()).Infoln("closing connection to hardware")
10+
defer logger.GetLogger(p.GetName()).Infoln("hardware connection has been closed")
1111
return p.HardwareDev.Close()
1212
}
1313

0 commit comments

Comments
 (0)