75 lines
1.6 KiB
Go
75 lines
1.6 KiB
Go
package logger
|
|
|
|
import (
|
|
"go.uber.org/zap"
|
|
"go.uber.org/zap/zapcore"
|
|
)
|
|
|
|
// LogMsg represents the standard log message structure
|
|
type LogMsg struct {
|
|
UserID string `json:"user_id"`
|
|
UserName string `json:"user_name"`
|
|
TraceID string `json:"trace"`
|
|
IP string `json:"ip"`
|
|
Msg string `json:"msg"`
|
|
}
|
|
|
|
// MarshalLogObject implements zapcore.ObjectMarshaler interface
|
|
// This allows the encoder to properly encode LogMsg struct
|
|
func (l LogMsg) MarshalLogObject(enc zapcore.ObjectEncoder) error {
|
|
enc.AddString("user_id", l.UserID)
|
|
enc.AddString("user_name", l.UserName)
|
|
enc.AddString("trace", l.TraceID)
|
|
enc.AddString("ip", l.IP)
|
|
enc.AddString("msg", l.Msg)
|
|
return nil
|
|
}
|
|
|
|
// ToZapFields converts LogMsg to zap.Field slice for structured logging
|
|
func (l *LogMsg) ToZapFields() []zap.Field {
|
|
return []zap.Field{
|
|
zap.String("user_id", l.UserID),
|
|
zap.String("user_name", l.UserName),
|
|
zap.String("trace", l.TraceID),
|
|
zap.String("ip", l.IP),
|
|
zap.String("msg", l.Msg),
|
|
}
|
|
}
|
|
|
|
// NewLogMsg creates a new LogMsg instance
|
|
func NewLogMsg(msg string) *LogMsg {
|
|
return &LogMsg{
|
|
Msg: msg,
|
|
}
|
|
}
|
|
|
|
// WithUserID sets the user ID
|
|
func (l *LogMsg) WithUserID(userID string) *LogMsg {
|
|
l.UserID = userID
|
|
return l
|
|
}
|
|
|
|
// WithUserName sets the user name
|
|
func (l *LogMsg) WithUserName(userName string) *LogMsg {
|
|
l.UserName = userName
|
|
return l
|
|
}
|
|
|
|
// WithTraceID sets the trace ID
|
|
func (l *LogMsg) WithTraceID(traceID string) *LogMsg {
|
|
l.TraceID = traceID
|
|
return l
|
|
}
|
|
|
|
// WithIP sets the IP address
|
|
func (l *LogMsg) WithIP(ip string) *LogMsg {
|
|
l.IP = ip
|
|
return l
|
|
}
|
|
|
|
// WithMsg sets the message
|
|
func (l *LogMsg) WithMsg(msg string) *LogMsg {
|
|
l.Msg = msg
|
|
return l
|
|
}
|