You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
func WithContext(ctx context.Context) *zap.Logger {
if ctx == nil {
return Logger
}
if ctxLogger, ok := ctx.Value(traceId).(*zap.Logger); ok {
return ctxLogger
}
return Logger
}
actual output
2023-07-18 10:04:33 INFO logs/main.go:42 aaaa {"traceId": "5e4ead22-465f-479d-8700-27294944ebd0"}
expected output
2023-07-18 10:04:33 INFO logs/main.go:42 aaaa traceId=5e4ead22-465f-479d-8700-27294944ebd0
or
2023-07-18 10:04:33 INFO logs/main.go:42 aaaa 5e4ead22-465f-479d-8700-27294944ebd0
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I have a log configuration, use ctx to add traceID, my log is executed output, but the added traceID is json, is there a way to change it?
package httplog
import (
"context"
"fmt"
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"io"
"time"
)
const traceId = iota
var Logger *zap.Logger
func Init(path string) {
if len(path) == 0 || path == "" {
panic("init log fail,logpath can not nil")
}
encoder := zapcore.NewConsoleEncoder(zapcore.EncoderConfig{
MessageKey: "msg",
LevelKey: "level",
EncodeLevel: zapcore.CapitalLevelEncoder,
TimeKey: "ts",
EncodeTime: func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(t.Format("2006-01-02 15:04:05"))
},
CallerKey: "file",
EncodeCaller: zapcore.ShortCallerEncoder,
EncodeDuration: func(d time.Duration, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendInt64(int64(d) / 1000000)
},
LineEnding: zapcore.DefaultLineEnding,
})
}
func getWriter(filename string) io.Writer {
hook, err := rotatelogs.New(
filename+".%Y%m%d%H",
rotatelogs.WithLinkName(filename),
rotatelogs.WithMaxAge(time.Hour247),
rotatelogs.WithRotationTime(time.Hour),
)
}
func NewContext(ctx context.Context, fields ...zapcore.Field) context.Context {
}
func WithContext(ctx context.Context) *zap.Logger {
if ctx == nil {
return Logger
}
if ctxLogger, ok := ctx.Value(traceId).(*zap.Logger); ok {
}
actual output
2023-07-18 10:04:33 INFO logs/main.go:42 aaaa {"traceId": "5e4ead22-465f-479d-8700-27294944ebd0"}
expected output
2023-07-18 10:04:33 INFO logs/main.go:42 aaaa traceId=5e4ead22-465f-479d-8700-27294944ebd0
or
2023-07-18 10:04:33 INFO logs/main.go:42 aaaa 5e4ead22-465f-479d-8700-27294944ebd0
Beta Was this translation helpful? Give feedback.
All reactions