-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: daz-3ux <[email protected]>
- Loading branch information
Showing
5 changed files
with
126 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Copyright 2023 daz-3ux(杨鹏达) <[email protected]>. 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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright 2023 daz-3ux(杨鹏达) <[email protected]>. 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright 2023 daz-3ux(杨鹏达) <[email protected]>. 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"}, | ||
} | ||
} |