Skip to content

Commit 167a9ca

Browse files
authored
adjust errors and logger (sjzar#10)
1 parent f31953c commit 167a9ca

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+553
-981
lines changed

cmd/chatlog/cmd_decrypt.go

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

77
"github.com/sjzar/chatlog/internal/chatlog"
88

9-
log "github.com/sirupsen/logrus"
9+
"github.com/rs/zerolog/log"
1010
"github.com/spf13/cobra"
1111
)
1212

@@ -33,11 +33,11 @@ var decryptCmd = &cobra.Command{
3333
Run: func(cmd *cobra.Command, args []string) {
3434
m, err := chatlog.New("")
3535
if err != nil {
36-
log.Error(err)
36+
log.Err(err).Msg("failed to create chatlog instance")
3737
return
3838
}
3939
if err := m.CommandDecrypt(dataDir, workDir, key, decryptPlatform, decryptVer); err != nil {
40-
log.Error(err)
40+
log.Err(err).Msg("failed to decrypt")
4141
return
4242
}
4343
fmt.Println("decrypt success")

cmd/chatlog/cmd_key.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55

66
"github.com/sjzar/chatlog/internal/chatlog"
77

8-
log "github.com/sirupsen/logrus"
8+
"github.com/rs/zerolog/log"
99
"github.com/spf13/cobra"
1010
)
1111

@@ -21,12 +21,12 @@ var keyCmd = &cobra.Command{
2121
Run: func(cmd *cobra.Command, args []string) {
2222
m, err := chatlog.New("")
2323
if err != nil {
24-
log.Error(err)
24+
log.Err(err).Msg("failed to create chatlog instance")
2525
return
2626
}
2727
ret, err := m.CommandKey(pid)
2828
if err != nil {
29-
log.Error(err)
29+
log.Err(err).Msg("failed to get key")
3030
return
3131
}
3232
fmt.Println(ret)

cmd/chatlog/log.go

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,26 @@
11
package chatlog
22

33
import (
4-
"fmt"
54
"io"
65
"os"
7-
"path"
86
"path/filepath"
9-
"runtime"
7+
"time"
108

119
"github.com/sjzar/chatlog/pkg/util"
1210

13-
log "github.com/sirupsen/logrus"
11+
"github.com/rs/zerolog"
12+
"github.com/rs/zerolog/log"
13+
"github.com/sirupsen/logrus"
1414
"github.com/spf13/cobra"
1515
)
1616

1717
var Debug bool
1818

1919
func initLog(cmd *cobra.Command, args []string) {
20-
log.SetFormatter(&log.TextFormatter{
21-
FullTimestamp: true,
22-
CallerPrettyfier: func(f *runtime.Frame) (string, string) {
23-
_, filename := path.Split(f.File)
24-
return "", fmt.Sprintf("%s:%d", filename, f.Line)
25-
},
26-
})
20+
zerolog.SetGlobalLevel(zerolog.InfoLevel)
2721

2822
if Debug {
29-
log.SetLevel(log.DebugLevel)
30-
log.SetReportCaller(true)
23+
zerolog.SetGlobalLevel(zerolog.DebugLevel)
3124
}
3225
}
3326

@@ -43,8 +36,8 @@ func initTuiLog(cmd *cobra.Command, args []string) {
4336
panic(err)
4437
}
4538
logOutput = logFD
46-
log.SetReportCaller(true)
4739
}
4840

49-
log.SetOutput(logOutput)
41+
log.Logger = log.Output(zerolog.ConsoleWriter{Out: logOutput, NoColor: true, TimeFormat: time.RFC3339})
42+
logrus.SetOutput(logOutput)
5043
}

cmd/chatlog/root.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package chatlog
33
import (
44
"github.com/sjzar/chatlog/internal/chatlog"
55

6-
log "github.com/sirupsen/logrus"
6+
"github.com/rs/zerolog/log"
77
"github.com/spf13/cobra"
88
)
99

@@ -17,7 +17,7 @@ func init() {
1717

1818
func Execute() {
1919
if err := rootCmd.Execute(); err != nil {
20-
log.Error(err)
20+
log.Err(err).Msg("command execution failed")
2121
}
2222
}
2323

@@ -38,11 +38,11 @@ func Root(cmd *cobra.Command, args []string) {
3838

3939
m, err := chatlog.New("")
4040
if err != nil {
41-
log.Error(err)
41+
log.Err(err).Msg("failed to create chatlog instance")
4242
return
4343
}
4444

4545
if err := m.Run(); err != nil {
46-
log.Error(err)
46+
log.Err(err).Msg("failed to run chatlog instance")
4747
}
4848
}

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
github.com/mattn/go-sqlite3 v1.14.24
1111
github.com/pierrec/lz4/v4 v4.1.22
1212
github.com/rivo/tview v0.0.0-20250325173046-7b72abf45814
13+
github.com/rs/zerolog v1.34.0
1314
github.com/shirou/gopsutil/v4 v4.25.2
1415
github.com/sirupsen/logrus v1.9.3
1516
github.com/spf13/cobra v1.9.1
@@ -41,6 +42,7 @@ require (
4142
github.com/leodido/go-urn v1.4.0 // indirect
4243
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
4344
github.com/lufia/plan9stats v0.0.0-20250317134145-8bc96cf8fc35 // indirect
45+
github.com/mattn/go-colorable v0.1.13 // indirect
4446
github.com/mattn/go-isatty v0.0.20 // indirect
4547
github.com/mattn/go-runewidth v0.0.16 // indirect
4648
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect

go.sum

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ github.com/bytedance/sonic/loader v0.2.4/go.mod h1:N8A3vUdtUebEY2/VQC0MyhYeKUFos
66
github.com/cloudwego/base64x v0.1.5 h1:XPciSp1xaq2VCSt6lF0phncD4koWyULpl5bUxbfCyP4=
77
github.com/cloudwego/base64x v0.1.5/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
88
github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY=
9+
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
910
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
1011
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1112
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
@@ -41,6 +42,7 @@ github.com/go-viper/mapstructure/v2 v2.2.1 h1:ZAaOCxANMuZx5RCeg0mBdEZk7DZasvvZIx
4142
github.com/go-viper/mapstructure/v2 v2.2.1/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
4243
github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4=
4344
github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
45+
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
4446
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
4547
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
4648
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
@@ -68,6 +70,10 @@ github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69
6870
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
6971
github.com/lufia/plan9stats v0.0.0-20250317134145-8bc96cf8fc35 h1:PpXWgLPs+Fqr325bN2FD2ISlRRztXibcX6e8f5FR5Dc=
7072
github.com/lufia/plan9stats v0.0.0-20250317134145-8bc96cf8fc35/go.mod h1:autxFIvghDt3jPTLoqZ9OZ7s9qTGNAWmYCjVFWPX/zg=
73+
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
74+
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
75+
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
76+
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
7177
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
7278
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
7379
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
@@ -83,6 +89,7 @@ github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNH
8389
github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc=
8490
github.com/pierrec/lz4/v4 v4.1.22 h1:cKFw6uJDK+/gfw5BcDL0JL5aBsAFdsIT18eRtLj7VIU=
8591
github.com/pierrec/lz4/v4 v4.1.22/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
92+
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
8693
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
8794
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
8895
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 h1:o4JXh1EVt9k/+g42oCprj/FisM4qX9L3sZB3upGN2ZU=
@@ -95,6 +102,9 @@ github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
95102
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
96103
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
97104
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
105+
github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0=
106+
github.com/rs/zerolog v1.34.0 h1:k43nTLIwcTVQAncfCw4KZ2VY6ukYoZaBPNOE8txlOeY=
107+
github.com/rs/zerolog v1.34.0/go.mod h1:bJsvje4Z08ROH4Nhs5iH600c3IkWhwp44iRc54W6wYQ=
98108
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
99109
github.com/sagikazarmark/locafero v0.9.0 h1:GbgQGNtTrEmddYDSAH9QLRyfAHY12md+8YFTqyMTC9k=
100110
github.com/sagikazarmark/locafero v0.9.0/go.mod h1:UBUyz37V+EdMS3hDF3QWIiVr/2dPrx49OMO0Bn0hJqk=
@@ -180,6 +190,7 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc
180190
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
181191
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
182192
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
193+
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
183194
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
184195
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
185196
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

internal/chatlog/http/route.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func (s *Service) GetChatlog(c *gin.Context) {
8888
var err error
8989
start, end, ok := util.TimeRangeOf(q.Time)
9090
if !ok {
91-
errors.Err(c, errors.ErrInvalidArg("time"))
91+
errors.Err(c, errors.InvalidArg("time"))
9292
}
9393
if q.Limit < 0 {
9494
q.Limit = 0
@@ -276,7 +276,7 @@ func (s *Service) GetFile(c *gin.Context) {
276276
func (s *Service) GetMedia(c *gin.Context, _type string) {
277277
key := c.Param("key")
278278
if key == "" {
279-
errors.Err(c, errors.ErrInvalidArg(key))
279+
errors.Err(c, errors.InvalidArg(key))
280280
return
281281
}
282282

internal/chatlog/http/service.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"github.com/sjzar/chatlog/internal/errors"
1212

1313
"github.com/gin-gonic/gin"
14-
log "github.com/sirupsen/logrus"
14+
"github.com/rs/zerolog/log"
1515
)
1616

1717
const (
@@ -33,14 +33,14 @@ func NewService(ctx *ctx.Context, db *database.Service, mcp *mcp.Service) *Servi
3333

3434
// Handle error from SetTrustedProxies
3535
if err := router.SetTrustedProxies(nil); err != nil {
36-
log.Error("Failed to set trusted proxies:", err)
36+
log.Err(err).Msg("Failed to set trusted proxies")
3737
}
3838

3939
// Middleware
4040
router.Use(
4141
errors.RecoveryMiddleware(),
4242
errors.ErrorHandlerMiddleware(),
43-
gin.LoggerWithWriter(log.StandardLogger().Out),
43+
gin.LoggerWithWriter(log.Logger),
4444
)
4545

4646
s := &Service{
@@ -68,11 +68,11 @@ func (s *Service) Start() error {
6868
go func() {
6969
// Handle error from Run
7070
if err := s.server.ListenAndServe(); err != nil {
71-
log.Error("Server Stopped: ", err)
71+
log.Err(err).Msg("Failed to start HTTP server")
7272
}
7373
}()
7474

75-
log.Info("Server started on ", s.ctx.HTTPAddr)
75+
log.Info().Msg("Starting HTTP server on " + s.ctx.HTTPAddr)
7676

7777
return nil
7878
}
@@ -88,10 +88,10 @@ func (s *Service) Stop() error {
8888
defer cancel()
8989

9090
if err := s.server.Shutdown(ctx); err != nil {
91-
return errors.HTTP("HTTP server shutdown error", err)
91+
return errors.HTTPShutDown(err)
9292
}
9393

94-
log.Info("HTTP server stopped")
94+
log.Info().Msg("HTTP server stopped")
9595
return nil
9696
}
9797

internal/chatlog/wechat/service.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"github.com/sjzar/chatlog/internal/wechat/decrypt"
1414
"github.com/sjzar/chatlog/pkg/util"
1515

16-
log "github.com/sirupsen/logrus"
16+
"github.com/rs/zerolog/log"
1717
)
1818

1919
type Service struct {
@@ -64,7 +64,7 @@ func (s *Service) FindDBFiles(rootDir string, recursive bool) ([]string, error)
6464
walkFunc := func(path string, info os.FileInfo, err error) error {
6565
if err != nil {
6666
// If a file or directory can't be accessed, log the error but continue
67-
fmt.Printf("Warning: Cannot access %s: %v\n", path, err)
67+
log.Err(err).Msgf("Warning: Cannot access %s", path)
6868
return nil
6969
}
7070

@@ -121,7 +121,7 @@ func (s *Service) DecryptDBFiles(dataDir string, workDir string, key string, pla
121121
defer outputFile.Close()
122122

123123
if err := decryptor.Decrypt(ctx, dbfile, key, outputFile); err != nil {
124-
log.Debugf("failed to decrypt %s: %v", dbfile, err)
124+
log.Err(err).Msgf("failed to decrypt %s", dbfile)
125125
if err == errors.ErrAlreadyDecrypted {
126126
if data, err := os.ReadFile(dbfile); err == nil {
127127
outputFile.Write(data)

0 commit comments

Comments
 (0)