Files
ProjectOctopus/agent-common/logger/logger.go
2024-03-28 15:13:56 +08:00

86 lines
2.0 KiB
Go

package logger
import (
"fmt"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// Logger struct represents a zap-based logger.
type Logger struct {
*zap.Logger
}
var Log, _ = NewLogger()
// NewLogger creates a new Logger instance.
func NewLogger() (*Logger, error) {
config := zap.Config{
Encoding: "json",
Level: zap.NewAtomicLevelAt(zap.DebugLevel),
OutputPaths: []string{"stdout"}, // 输出到控制台
ErrorOutputPaths: []string{"stderr"},
EncoderConfig: zapcore.EncoderConfig{
MessageKey: "message",
LevelKey: "level",
TimeKey: "time",
//CallerKey: "caller",
EncodeLevel: zapcore.CapitalLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
//EncodeCaller: zapcore.ShortCallerEncoder,
},
Development: true,
}
logger, err := config.Build()
if err != nil {
return nil, err
}
return &Logger{logger}, nil
}
func (l *Logger) Printf(msg string, args ...interface{}) {
l.Logger.Info(fmt.Sprintf("%s ==> %v", msg, args))
}
// Debug logs a debug message.
func (l *Logger) Debug(msg string, fields ...zap.Field) {
l.Logger.Debug(msg, fields...)
}
func (l *Logger) DebugF(msg string, args ...interface{}) {
l.Logger.Debug(fmt.Sprintf(msg, args...))
}
// Info logs an info message.
func (l *Logger) Info(msg string, fields ...zap.Field) {
l.Logger.Info(msg, fields...)
}
// InfoF logs an info message with format
func (l *Logger) InfoF(msg string, args ...interface{}) {
l.Logger.Info(fmt.Sprintf(msg, args...))
}
// Warn logs a warning message.
func (l *Logger) Warn(msg string, fields ...zap.Field) {
l.Logger.Warn(msg, fields...)
}
func (l *Logger) WarnF(msg string, args ...interface{}) {
l.Logger.Warn(fmt.Sprintf(msg, args...))
}
// Error logs an error message.
func (l *Logger) ErrorF(msg string, args ...interface{}) {
l.Logger.Error(fmt.Sprintf(msg, args...))
}
// Fatal logs a fatal message and exits the program with a non-zero status code.
func (l *Logger) Fatal(msg string, fields ...zap.Field) {
l.Logger.Fatal(msg, fields...)
}