-
-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Showing
12 changed files
with
239 additions
and
83 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
Large diffs are not rendered by default.
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
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,14 @@ | ||
module github.com/go-kratos/kratos/contrib/log/zerolog/v2 | ||
|
||
go 1.19 | ||
|
||
require ( | ||
github.com/go-kratos/kratos/v2 v2.6.3 | ||
github.com/rs/zerolog v1.30.0 | ||
) | ||
|
||
require ( | ||
github.com/mattn/go-colorable v0.1.12 // indirect | ||
github.com/mattn/go-isatty v0.0.14 // indirect | ||
golang.org/x/sys v0.8.0 // indirect | ||
) |
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,16 @@ | ||
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= | ||
github.com/go-kratos/kratos/v2 v2.6.3 h1:zuejN8CnszyMnbLWd7ObCN9rLuKucJe2/2P6hHpePDc= | ||
github.com/go-kratos/kratos/v2 v2.6.3/go.mod h1:nlBMkZueZps7UaP3GdbqG8V8N/eAX0jh019OKSm4xew= | ||
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= | ||
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40= | ||
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4= | ||
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= | ||
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= | ||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= | ||
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= | ||
github.com/rs/zerolog v1.30.0 h1:SymVODrcRsaRaSInD9yQtKbtWqwsfoPcRff/oRXLj4c= | ||
github.com/rs/zerolog v1.30.0/go.mod h1:/tk+P47gFdPXq4QYjvCmT5/Gsug2nagsFWBWhAiSi1w= | ||
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU= | ||
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= |
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 zerolog | ||
|
||
import ( | ||
"github.com/rs/zerolog" | ||
|
||
"github.com/go-kratos/kratos/v2/log" | ||
) | ||
|
||
var _ log.Logger = (*Logger)(nil) | ||
|
||
type Logger struct { | ||
log *zerolog.Logger | ||
} | ||
|
||
func NewLogger(logger *zerolog.Logger) log.Logger { | ||
return &Logger{ | ||
log: logger, | ||
} | ||
} | ||
|
||
func (l *Logger) Log(level log.Level, keyvals ...interface{}) (err error) { | ||
var event *zerolog.Event | ||
if len(keyvals) == 0 { | ||
return nil | ||
} | ||
if len(keyvals)%2 != 0 { | ||
keyvals = append(keyvals, "") | ||
} | ||
|
||
switch level { | ||
case log.LevelDebug: | ||
event = l.log.Debug() | ||
case log.LevelInfo: | ||
event = l.log.Info() | ||
case log.LevelWarn: | ||
event = l.log.Warn() | ||
case log.LevelError: | ||
event = l.log.Error() | ||
case log.LevelFatal: | ||
event = l.log.Fatal() | ||
default: | ||
event = l.log.Debug() | ||
} | ||
|
||
for i := 0; i < len(keyvals); i += 2 { | ||
key, ok := keyvals[i].(string) | ||
if !ok { | ||
continue | ||
} | ||
event = event.Any(key, keyvals[i+1]) | ||
} | ||
event.Send() | ||
return | ||
} |
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,45 @@ | ||
package zerolog | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/rs/zerolog" | ||
|
||
"github.com/go-kratos/kratos/v2/log" | ||
) | ||
|
||
type testWriteSyncer struct { | ||
output []string | ||
} | ||
|
||
func (x *testWriteSyncer) Write(p []byte) (n int, err error) { | ||
x.output = append(x.output, string(p)) | ||
return len(p), nil | ||
} | ||
|
||
func TestLogger(t *testing.T) { | ||
syncer := &testWriteSyncer{} | ||
zerolog.TimeFieldFormat = "2006-01-02 15:04:05.000" | ||
zlogger := zerolog.New(syncer) | ||
logger := NewLogger(&zlogger) | ||
|
||
klog := log.NewHelper(logger) | ||
|
||
klog.Debugw("log", "debug") | ||
klog.Infow("log", "info") | ||
klog.Warnw("log", "warn") | ||
klog.Errorw("log", "error") | ||
|
||
except := []string{ | ||
"{\"level\":\"debug\",\"log\":\"debug\"}\n", | ||
"{\"level\":\"info\",\"log\":\"info\"}\n", | ||
"{\"level\":\"warn\",\"log\":\"warn\"}\n", | ||
"{\"level\":\"error\",\"log\":\"error\"}\n", | ||
} | ||
for i, s := range except { | ||
if s != syncer.output[i] { | ||
t.Logf("except=%s, got=%s", s, syncer.output[i]) | ||
t.Fail() | ||
} | ||
} | ||
} |
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