Compare commits

...

5 Commits
v3.11.26 ... v3

Author SHA1 Message Date
a489aab1c3 Merge pull request 'logger/slog: fixed time len field' (#389) from logger-slog into v3
Some checks failed
coverage / build (push) Failing after 47s
test / test (push) Has started running
Reviewed-on: #389
2024-12-24 20:51:47 +03:00
d160664ef1 fixup test
Some checks failed
lint / lint (pull_request) Successful in 1m25s
coverage / build (pull_request) Failing after 1m25s
test / test (pull_request) Successful in 5m30s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-12-24 20:45:53 +03:00
fa868edcaa logger/slog: fixed time len field
Some checks failed
lint / lint (pull_request) Successful in 1m22s
test / test (pull_request) Successful in 4m50s
coverage / build (pull_request) Failing after 29s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-12-24 20:36:32 +03:00
vtolstov
6ed0b0e090 Apply Code Coverage Badge 2024-12-23 18:18:20 +00:00
533b265d19 add codec.RawMessage support
All checks were successful
test / test (push) Successful in 3m41s
coverage / build (push) Successful in 8m22s
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2024-12-23 21:17:32 +03:00
5 changed files with 35 additions and 6 deletions

View File

@ -1,5 +1,5 @@
# Micro
![Coverage](https://img.shields.io/badge/Coverage-44.9%25-yellow)
![Coverage](https://img.shields.io/badge/Coverage-45.1%25-yellow)
Micro is a standard library for microservices.

View File

@ -3,6 +3,8 @@ package codec
import (
"errors"
"gopkg.in/yaml.v3"
)
var (
@ -66,10 +68,10 @@ func (m *RawMessage) MarshalYAML() ([]byte, error) {
}
// UnmarshalYAML sets *m to a copy of data.
func (m *RawMessage) UnmarshalYAML(data []byte) error {
func (m *RawMessage) UnmarshalYAML(n *yaml.Node) error {
if m == nil {
return errors.New("RawMessage UnmarshalYAML on nil pointer")
}
*m = append((*m)[0:0], data...)
*m = append((*m)[0:0], []byte(n.Value)...)
return nil
}

View File

@ -1,5 +1,7 @@
package codec
import "gopkg.in/yaml.v3"
// Frame gives us the ability to define raw data to send over the pipes
type Frame struct {
Data []byte
@ -26,8 +28,9 @@ func (m *Frame) MarshalYAML() ([]byte, error) {
}
// UnmarshalYAML set frame data
func (m *Frame) UnmarshalYAML(data []byte) error {
return m.Unmarshal(data)
func (m *Frame) UnmarshalYAML(n *yaml.Node) error {
m.Data = []byte(n.Value)
return nil
}
// ProtoMessage noop func

View File

@ -22,6 +22,7 @@ const (
badKey = "!BADKEY"
// defaultCallerSkipCount used by logger
defaultCallerSkipCount = 3
timeFormat = "2006-01-02T15:04:05.000000000Z07:00"
)
var reTrace = regexp.MustCompile(`.*/slog/logger\.go.*\n`)
@ -64,6 +65,7 @@ func (s *slogLogger) renameAttr(_ []string, a slog.Attr) slog.Attr {
a.Key = s.opts.SourceKey
case slog.TimeKey:
a.Key = s.opts.TimeKey
a.Value = slog.StringValue(a.Value.Time().Format(timeFormat))
case slog.MessageKey:
a.Key = s.opts.MessageKey
case slog.LevelKey:

View File

@ -9,12 +9,34 @@ import (
"log/slog"
"strings"
"testing"
"time"
"github.com/google/uuid"
"go.unistack.org/micro/v3/logger"
"go.unistack.org/micro/v3/metadata"
)
func TestTime(t *testing.T) {
ctx := context.TODO()
buf := bytes.NewBuffer(nil)
l := NewLogger(logger.WithLevel(logger.ErrorLevel), logger.WithOutput(buf),
WithHandlerFunc(slog.NewTextHandler),
logger.WithAddStacktrace(true),
logger.WithTimeFunc(func() time.Time {
return time.Unix(0, 0)
}),
)
if err := l.Init(logger.WithFields("key1", "val1")); err != nil {
t.Fatal(err)
}
l.Error(ctx, "msg1", errors.New("err"))
if !bytes.Contains(buf.Bytes(), []byte(`timestamp=1970-01-01T03:00:00.000000000+03:00`)) {
t.Fatalf("logger error not works, buf contains: %s", buf.Bytes())
}
}
func TestStacktrace(t *testing.T) {
ctx := context.TODO()
buf := bytes.NewBuffer(nil)
@ -28,7 +50,7 @@ func TestStacktrace(t *testing.T) {
l.Error(ctx, "msg1", errors.New("err"))
if !bytes.Contains(buf.Bytes(), []byte(`slog_test.go:29`)) {
if !bytes.Contains(buf.Bytes(), []byte(`slog_test.go:51`)) {
t.Fatalf("logger error not works, buf contains: %s", buf.Bytes())
}
}