Replies: 1 comment 1 reply
-
I think you can do this fairly easily with a custom implementation of // logLimitCore truncates messages longer than its limit field.
type logLimitCore struct {
underlying zapcore.Core
limit int
}
var _ zapcore.Core = logLimitCore{}
func (c logLimitCore) Enabled(l zapcore.Level) bool {
return c.underlying.Enabled(l)
}
func (c logLimitCore) With(fields []zapcore.Field) zapcore.Core {
return logLimitCore{
underlying: c.underlying.With(fields),
limit: c.limit,
}
}
func (c logLimitCore) Check(ent zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore.CheckedEntry {
if c.Enabled(ent.Level) {
return ce.AddCore(ent, c)
}
return ce
}
func (c logLimitCore) Write(ent zapcore.Entry, fields []zapcore.Field) error {
ent.Message = ent.Message[:c.limit] // Truncate messages longer than limit
return c.underlying.Write(ent, fields)
}
func (c logLimitCore) Sync() error {
return c.underlying.Sync()
} Then you can wrap around whatever core you're currently using, for example the pre-defined production logger's core: base, err := zap.NewProduction()
if err != nil {
return fmt.Errorf("create prod base logger: %w", err)
}
logger := zap.New(logLimitCore{
underlying: base.Core(),
limit: _myLogLimit,
}) |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello,
we log debug information with some objects, sometimes the log could be very long (more than 10K chars), is it possible to set a max log length, truncate the message if it is too long?
Beta Was this translation helpful? Give feedback.
All reactions