-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
yangyile
committed
Nov 26, 2024
1 parent
c30ab7e
commit f18b4da
Showing
28 changed files
with
490 additions
and
444 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package zaplog | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
"github.com/yyle88/zaplog/internal/utils" | ||
"github.com/yyle88/zaplog/zaplogs" | ||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
type Config struct { | ||
Debug bool | ||
Level string | ||
OutputPaths []string | ||
Skip int | ||
} | ||
|
||
func NewConfig() *Config { | ||
return &Config{ | ||
Debug: true, | ||
Level: "DEBUG", | ||
OutputPaths: []string{"stdout"}, | ||
Skip: 0, | ||
} | ||
} | ||
|
||
func NewZapLog(cfg *Config) (*zap.Logger, error) { | ||
config := NewZapConfig(cfg.Debug, cfg.Level, cfg.OutputPaths) | ||
|
||
var opts []zap.Option | ||
if cfg.Skip > 0 { | ||
opts = append(opts, zap.AddCallerSkip(cfg.Skip)) | ||
} | ||
|
||
zapLog, err := config.Build(opts...) | ||
if err != nil { | ||
return nil, errors.WithMessage(err, "new zap log is wrong") | ||
} | ||
return zapLog, nil | ||
} | ||
|
||
func NewZapConfig(debug bool, level string, outputPaths []string) *zap.Config { | ||
var config *zap.Config | ||
if debug { | ||
config = utils.PtrX(zap.NewDevelopmentConfig()) | ||
// config.DisableStacktrace = true //认为说还是需要打印错误的调用栈的,保持和默认值相同吧 | ||
config.EncoderConfig.EncodeCaller = zaplogs.NewCallerEncoderTrimPC() | ||
} else { | ||
config = utils.PtrX(zap.NewProductionConfig()) | ||
// config.DisableCaller = true //是否在日志中展示文件的路径和代码行号,保持和默认值相同吧 | ||
config.EncoderConfig.EncodeCaller = zapcore.ShortCallerEncoder | ||
} | ||
if len(outputPaths) > 0 { | ||
config.OutputPaths = outputPaths | ||
} | ||
config.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder | ||
config.EncoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder | ||
config.Level = zap.NewAtomicLevelAt(zaplogs.ParseLevelCode(level)) | ||
return config | ||
} |
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,30 @@ | ||
package zaplog_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/yyle88/zaplog" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func TestNewZapConfig(t *testing.T) { | ||
{ | ||
config := zaplog.NewZapConfig(true, "debug", []string{"stdout"}) | ||
zapLog, err := config.Build() | ||
require.NoError(t, err) | ||
zapLog.Info("abc", zap.String("xyz", "uvw")) | ||
zapLog.Error("abc", zap.String("xyz", "uvw")) | ||
zapLog.Debug("abc", zap.String("xyz", "uvw")) | ||
zapLog.Warn("abc", zap.String("xyz", "uvw")) | ||
} | ||
{ | ||
config := zaplog.NewZapConfig(false, "debug", []string{"stdout"}) | ||
zapLog, err := config.Build() | ||
require.NoError(t, err) | ||
zapLog.Info("abc", zap.String("xyz", "uvw")) | ||
zapLog.Error("abc", zap.String("xyz", "uvw")) | ||
zapLog.Debug("abc", zap.String("xyz", "uvw")) | ||
zapLog.Warn("abc", zap.String("xyz", "uvw")) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,53 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
"github.com/yyle88/zaplog" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func main() { | ||
{ | ||
zaplog.LOG.Info("abc", zap.String("xyz", "uvw")) | ||
show(zaplog.LOG) | ||
} | ||
{ | ||
zaplog.LOGS.P0.Info("abc", zap.String("xyz", "uvw")) | ||
show(zaplog.LOGS.P1) | ||
} | ||
{ | ||
config := zaplog.NewZapConfig(true, "debug", []string{"stdout"}) | ||
zapLog, err := config.Build() | ||
if err != nil { | ||
panic(errors.WithMessage(err, "wrong")) | ||
} | ||
zapLog.Info("abc", zap.String("xyz", "uvw")) | ||
show(zapLog) | ||
} | ||
{ | ||
config := zaplog.NewZapConfig(false, "debug", []string{"stdout"}) | ||
zapLog, err := config.Build() | ||
if err != nil { | ||
panic(errors.WithMessage(err, "wrong")) | ||
} | ||
zapLog.Info("abc", zap.String("xyz", "uvw")) | ||
show(zapLog) | ||
} | ||
} | ||
|
||
func show(zapLog *zap.Logger) { | ||
zapLog.Error("abc", zap.String("xyz", "uvw")) | ||
show2(zapLog) | ||
zapLog.Info("========================================") | ||
} | ||
|
||
func show2(zapLog *zap.Logger) { | ||
zapLog = zapLog.With(zap.String("step2", "show2")) | ||
zapLog.Debug("abc", zap.String("xyz", "uvw")) | ||
show3(zapLog) | ||
} | ||
|
||
func show3(zapLog *zap.Logger) { | ||
zapLog = zapLog.With(zap.String("step3", "show3")) | ||
zapLog.Warn("abc", zap.String("xyz", "uvw")) | ||
} |
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,15 @@ | ||
package utils | ||
|
||
import "net/url" | ||
|
||
func PtrX[T any](v T) *T { | ||
return &v | ||
} | ||
|
||
func SoftPathUnescape(raw string) string { | ||
res, err := url.PathUnescape(raw) // 非 ASCII 的字符要做额外处理 | ||
if err != nil { | ||
return raw // 当出错时就返回原始的 | ||
} | ||
return res | ||
} |
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,23 @@ | ||
package utils | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestPtrX(t *testing.T) { | ||
type aType struct { | ||
v string | ||
} | ||
a := aType{v: "a"} | ||
p := PtrX(a) | ||
require.Equal(t, "a", p.v) | ||
} | ||
|
||
func TestSoftPathUnescape(t *testing.T) { | ||
raw := "github.com/yyle88/zaplog/internal/examples/example1x/ZLG%e6%b5%8b%e9%9d%9eASCII%e8%b7%af%e5%be%84.TestZapLog" | ||
t.Log(raw) | ||
res := SoftPathUnescape(raw) | ||
t.Log(res) | ||
} |
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,34 @@ | ||
package zaplog | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
"go.uber.org/zap" | ||
) | ||
|
||
type Zap struct { | ||
LOG *zap.Logger | ||
SUG *zap.SugaredLogger //比较慢但也是种简单的调用接口 | ||
} | ||
|
||
func NewZap(zapLog *zap.Logger) *Zap { | ||
return &Zap{ | ||
LOG: zapLog, | ||
SUG: zapLog.Sugar(), | ||
} | ||
} | ||
|
||
func NewZapSkip(zapLog *zap.Logger, skipDepth int) *Zap { | ||
return NewZap(zapLog.WithOptions(zap.AddCallerSkip(skipDepth))) | ||
} | ||
|
||
func MustNewZap(cfg *Config) *Zap { | ||
zapLog, err := NewZapLog(cfg) | ||
if err != nil { | ||
panic(errors.Wrap(err, "ERROR WHEN NEW ZAP LOG")) | ||
} | ||
return NewZap(zapLog) | ||
} | ||
|
||
func (T *Zap) Close() error { | ||
return T.LOG.Sync() | ||
} |
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,42 @@ | ||
package zaplog_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/yyle88/zaplog" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func TestDebug(t *testing.T) { | ||
zaplog.LOG.Debug("1", zap.String("a", "0")) | ||
zaplog.LOG.Error("1", zap.Error(errors.New("e"))) | ||
} | ||
|
||
func TestError(t *testing.T) { | ||
zaplog.LOG.Debug("1", zap.String("a", "0")) | ||
zaplog.LOG.Error("1", zap.Error(errors.New("e"))) | ||
} | ||
|
||
func TestDebug2(t *testing.T) { | ||
zaplog.LOG.Debug("ok", zap.String("A", "aaa")) | ||
zaplog.LOG.Debug("ok", zap.String("B", "bbb")) | ||
} | ||
|
||
func TestDebug3(t *testing.T) { | ||
zaplog.LOG.Debug("1") | ||
zaplog.LOG.Error("2", zap.Error(errors.New("x"))) | ||
} | ||
|
||
func TestDebug4(t *testing.T) { | ||
zaplog.SUG.Debug("1") | ||
zaplog.SUG.Error("2", errors.New("x")) | ||
} | ||
|
||
func TestDebug5(t *testing.T) { | ||
zaplog.SUG.Debug(1, 2, 3, 4, 5, 6) | ||
zaplog.SUG.Debug("1", 2, 3, 4, 5, 6) | ||
zaplog.SUG.Debug() | ||
zaplog.SUG.Debug(0) | ||
zaplog.SUG.Debug([]int{0, 1, 2, 3, 4}) | ||
} |
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,54 @@ | ||
package zaplog | ||
|
||
import ( | ||
"github.com/yyle88/mutexmap" | ||
"go.uber.org/zap" | ||
) | ||
|
||
type SkipLogs struct { | ||
P0 *zap.Logger | ||
P1 *zap.Logger | ||
P2 *zap.Logger | ||
P3 *zap.Logger | ||
P4 *zap.Logger | ||
mp *mutexmap.Map[int, *zap.Logger] | ||
} | ||
|
||
func NewSkipLogs(zapLog *zap.Logger) *SkipLogs { | ||
return &SkipLogs{ | ||
P0: newSkipDepth(zapLog, 0), | ||
P1: newSkipDepth(zapLog, 1), | ||
P2: newSkipDepth(zapLog, 2), | ||
P3: newSkipDepth(zapLog, 3), | ||
P4: newSkipDepth(zapLog, 4), | ||
mp: mutexmap.NewMap[int, *zap.Logger](0), | ||
} | ||
} | ||
|
||
func (Z *SkipLogs) Pn(skipDepth int) *zap.Logger { | ||
switch skipDepth { | ||
case 0: | ||
return Z.P0 | ||
case 1: | ||
return Z.P1 | ||
case 2: | ||
return Z.P2 | ||
case 3: | ||
return Z.P3 | ||
case 4: | ||
return Z.P4 | ||
default: | ||
if skipDepth > 0 { | ||
res, _ := Z.mp.Getset(skipDepth, func() *zap.Logger { | ||
return newSkipDepth(LOG, skipDepth) | ||
}) | ||
return res | ||
} else { | ||
return Z.P0 | ||
} | ||
} | ||
} | ||
|
||
func newSkipDepth(zapLog *zap.Logger, skipDepth int) *zap.Logger { | ||
return zapLog.WithOptions(zap.AddCallerSkip(skipDepth)) | ||
} |
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
Oops, something went wrong.