mirror of
https://github.com/warmbly/warmbly.git
synced 2026-10-15 08:02:42 +00:00
48 lines
1.1 KiB
Go
48 lines
1.1 KiB
Go
package worker
|
|
|
|
import (
|
|
"os"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/warmbly/warmbly/internal/pkg/nodeevidence"
|
|
|
|
"go.uber.org/zap"
|
|
"go.uber.org/zap/zapcore"
|
|
)
|
|
|
|
type LoggerClient struct {
|
|
Events chan zapcore.Entry
|
|
*zap.Logger
|
|
}
|
|
|
|
type CustomCore struct {
|
|
zapcore.Core
|
|
OnError func(entry zapcore.Entry)
|
|
}
|
|
|
|
func (c *CustomCore) Write(entry zapcore.Entry, fields []zapcore.Field) error {
|
|
if entry.Level == zapcore.ErrorLevel && c.OnError != nil {
|
|
c.OnError(entry)
|
|
}
|
|
return c.Core.Write(entry, fields)
|
|
}
|
|
|
|
func NewLoggerWithHandler(onError func(zapcore.Entry)) (*zap.Logger, error) {
|
|
cfg := zap.NewProductionConfig()
|
|
cfg.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
|
|
core := zapcore.NewCore(
|
|
zapcore.NewJSONEncoder(cfg.EncoderConfig),
|
|
zapcore.AddSync(os.Stdout),
|
|
cfg.Level,
|
|
)
|
|
|
|
customCore := &CustomCore{Core: core, OnError: onError}
|
|
|
|
return zap.New(customCore, zap.AddCaller(), zap.Hooks(func(entry zapcore.Entry) error {
|
|
if entry.Level >= zapcore.ErrorLevel {
|
|
nodeevidence.Emit(nodeevidence.WorkerError, uuid.Nil, 0, 1)
|
|
}
|
|
return nil
|
|
})), nil
|
|
}
|