From b334ee74ac17a81322093b402272772ea57494c1 Mon Sep 17 00:00:00 2001 From: daz-3ux Date: Fri, 8 Sep 2023 18:00:48 +0800 Subject: [PATCH] feat: init log system: zap Signed-off-by: daz-3ux --- go.mod | 2 + go.sum | 12 +++++- internal/pkg/log/doc.go | 6 +++ internal/pkg/log/log.go | 79 +++++++++++++++++++++++++++++++++++++ internal/pkg/log/options.go | 29 ++++++++++++++ 5 files changed, 126 insertions(+), 2 deletions(-) create mode 100644 internal/pkg/log/doc.go create mode 100644 internal/pkg/log/log.go create mode 100644 internal/pkg/log/options.go diff --git a/go.mod b/go.mod index 2d89597..fadee63 100644 --- a/go.mod +++ b/go.mod @@ -5,9 +5,11 @@ go 1.21.0 require ( github.com/spf13/cobra v1.7.0 go.uber.org/automaxprocs v1.5.3 + go.uber.org/zap v1.25.0 ) require ( github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/spf13/pflag v1.0.5 // indirect + go.uber.org/multierr v1.10.0 // indirect ) diff --git a/go.sum b/go.sum index 58d4bdf..bc29a2b 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A= +github.com/benbjohnson/clock v1.3.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -12,10 +14,16 @@ github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I= github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= -github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY= -github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= go.uber.org/automaxprocs v1.5.3 h1:kWazyxZUrS3Gs4qUpbwo5kEIMGe/DAvi5Z4tl2NW4j8= go.uber.org/automaxprocs v1.5.3/go.mod h1:eRbA25aqJrxAbsLO0xy5jVwPt7FQnRgjW+efnwa1WM0= +go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk= +go.uber.org/goleak v1.2.0/go.mod h1:XJYK+MuIchqpmGmUSAzotztawfKvYLUIgg7guXrwVUo= +go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ= +go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= +go.uber.org/zap v1.25.0 h1:4Hvk6GtkucQ790dqmj7l1eEnRdKm3k3ZUrUMS2d5+5c= +go.uber.org/zap v1.25.0/go.mod h1:JIAUzQIH94IC4fOJQm7gMmBJP5k7wQfdcnYdPoEXJYk= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/pkg/log/doc.go b/internal/pkg/log/doc.go new file mode 100644 index 0000000..73ab173 --- /dev/null +++ b/internal/pkg/log/doc.go @@ -0,0 +1,6 @@ +// Copyright 2023 daz-3ux(杨鹏达) . All rights reserved. +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. The original repo for +// this file is https://github.com/Daz-3ux/dBlog. + +package log // Package log import "github.com/Daz-3ux/dBlog/internal/pkg/log" diff --git a/internal/pkg/log/log.go b/internal/pkg/log/log.go new file mode 100644 index 0000000..e3d5794 --- /dev/null +++ b/internal/pkg/log/log.go @@ -0,0 +1,79 @@ +// Copyright 2023 daz-3ux(杨鹏达) . All rights reserved. +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. The original repo for +// this file is https://github.com/Daz-3ux/dBlog. + +package log + +import ( + "go.uber.org/zap" + "go.uber.org/zap/zapcore" + + "sync" + "time" +) + +// zapLogger wraps zap.Logger +// the specific implementation of the Logger interface +type zapLogger struct { + z *zap.Logger +} + +var ( + mu sync.Mutex + + // std the global default logger + std = NewLogger(NewOptions()) +) + +// Init initializes the global logger with the given options +func Init(opts *Options) { + mu.Lock() + defer mu.Unlock() + + std = NewLogger(opts) +} + +// NewLogger creates a new logger with the given options +func NewLogger(opts *Options) *zapLogger { + if opts == nil { + opts = NewOptions() + } + + var zapLevel zapcore.Level + if err := zapLevel.UnmarshalText([]byte(opts.Level)); err != nil { + zapLevel = zapcore.InfoLevel + } + + encoderConfig := zap.NewProductionEncoderConfig() + encoderConfig.MessageKey = "message" + encoderConfig.TimeKey = "timestamp" + encoderConfig.EncodeTime = func(t time.Time, enc zapcore.PrimitiveArrayEncoder) { + enc.AppendString(t.Format("2006-01-02 15:04:05.000")) + } + encoderConfig.EncodeDuration = func(d time.Duration, enc zapcore.PrimitiveArrayEncoder) { + enc.AppendFloat64(float64(d) / float64(time.Millisecond)) + } + + cfg := &zap.Config{ + DisableCaller: opts.DisableCaller, + DisableStacktrace: opts.DisableStacktrace, + Level: zap.NewAtomicLevelAt(zapLevel), + Encoding: opts.Format, + EncoderConfig: encoderConfig, + OutputPaths: opts.OutputPaths, + ErrorOutputPaths: []string{"stderr"}, + } + + z, err := cfg.Build(zap.AddStacktrace(zap.PanicLevel), zap.AddCallerSkip(1)) + if err != nil { + panic(err) + } + + // OOP: wrap the zap library in a custom struct + logger := &zapLogger{z: z} + + zap.RedirectStdLog(z) + + return logger +} diff --git a/internal/pkg/log/options.go b/internal/pkg/log/options.go new file mode 100644 index 0000000..443aafa --- /dev/null +++ b/internal/pkg/log/options.go @@ -0,0 +1,29 @@ +// Copyright 2023 daz-3ux(杨鹏达) . All rights reserved. +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. The original repo for +// this file is https://github.com/Daz-3ux/dBlog. + +package log + +import ( + "go.uber.org/zap/zapcore" +) + +// Options the options for the logger +type Options struct { + DisableCaller bool + DisableStacktrace bool + Level string + Format string + OutputPaths []string +} + +func NewOptions() *Options { + return &Options{ + DisableCaller: false, + DisableStacktrace: false, + Level: zapcore.InfoLevel.String(), + Format: "console", + OutputPaths: []string{"stdout"}, + } +}