xframe/component/loggerc/logs.go

137 lines
3.6 KiB
Go
Raw Normal View History

2024-10-12 12:55:20 +08:00
package loggerc
import (
"context"
"fmt"
"git.wishpal.cn/wishpal_ironfan/xframe/component/logger"
)
type (
LogConf = logger.LogConf
LogField = logger.LogField
)
// AddGlobalFields adds global fields.
func AddGlobalFields(fields ...LogField) {
logger.AddGlobalFields(fields...)
}
// Alert alerts v in alert level, and the message is written to error logger.
func Alert(_ context.Context, v string) {
logger.Alert(v)
}
// Close closes the logging.
func Close() error {
return logger.Close()
}
// Debugln writes v into access logger.
func Debugln(ctx context.Context, v ...interface{}) {
getLogger(ctx).Debugln(v...)
}
// Debugf writes v with format into access logger.
func Debugf(ctx context.Context, format string, v ...interface{}) {
getLogger(ctx).Debugf(format, v...)
}
// Debugw writes msg along with fields into access logger.
func Debugw(ctx context.Context, msg string, fields ...LogField) {
getLogger(ctx).Debugw(msg, fields...)
}
// Errorln writes v into error logger.
func Errorln(ctx context.Context, v ...any) {
getLogger(ctx).Errorln(v...)
}
// Errorf writes v with format into error logger.
func Errorf(ctx context.Context, format string, v ...any) {
getLogger(ctx).Errorf(fmt.Errorf(format, v...).Error())
}
// Errorw writes msg along with fields into error logger.
func Errorw(ctx context.Context, msg string, fields ...LogField) {
getLogger(ctx).Errorw(msg, fields...)
}
// Warnln writes v into warn logger.
func Warnln(ctx context.Context, v ...any) {
getLogger(ctx).Warnln(v...)
}
// Warnf writes v with format into warn logger.
func Warnf(ctx context.Context, format string, v ...any) {
getLogger(ctx).Warnf(format, v...)
}
// Warnw writes msg along with fields into warn logger.
func Warnw(ctx context.Context, msg string, fields ...LogField) {
getLogger(ctx).Warnw(msg, fields...)
}
// Field returns a LogField for the given key and value.
func Field(key string, value any) LogField {
return logger.Field(key, value)
}
// Infoln writes v into access logger.
func Infoln(ctx context.Context, v ...any) {
getLogger(ctx).Infoln(v...)
}
// Infof writes v with format into access logger.
func Infof(ctx context.Context, format string, v ...any) {
getLogger(ctx).Infof(format, v...)
}
// Infow writes msg along with fields into access logger.
func Infow(ctx context.Context, msg string, fields ...LogField) {
getLogger(ctx).Infow(msg, fields...)
}
// Must checks if err is nil, otherwise logs the error and exits.
func Must(err error) {
logger.Must(err)
}
// MustSetup sets up logging with given config c. It exits on error.
func MustSetup(c logger.LogConf) {
logger.MustSetup(c)
}
// SetLevel sets the logging level. It can be used to suppress some logs.
func SetLevel(level uint32) {
logger.SetLevel(level)
}
// SetUp sets up the logger. If already set up, just return nil.
// we allow SetUp to be called multiple times, because for example
// we need to allow different service frameworks to initialize logger respectively.
// the same logic for SetUp
func SetUp(c LogConf) error {
return logger.SetUp(c)
}
// Slowln writes v into slow logger.
func Slowln(ctx context.Context, v ...any) {
getLogger(ctx).Slowln(v...)
}
// Slowf writes v with format into slow logger.
func Slowf(ctx context.Context, format string, v ...any) {
getLogger(ctx).Slowf(format, v...)
}
// Sloww writes msg along with fields into slow logger.
func Sloww(ctx context.Context, msg string, fields ...LogField) {
getLogger(ctx).Sloww(msg, fields...)
}
// getLogger returns the logger.Logger with the given ctx and correct caller.
func getLogger(ctx context.Context) logger.Logger {
return logger.WithContext(ctx).WithCallerSkip(1)
}