This commit is contained in:
GitLab Deploy Bot
2025-10-21 23:45:13 +07:00
parent 6c387b420c
commit bb60e987e5
3548 changed files with 4952576 additions and 116 deletions
+209
View File
@@ -0,0 +1,209 @@
package log
import (
"context"
"fmt"
"io"
"log"
"os"
"sync"
"github.com/valyala/bytebufferpool"
)
var _ AllLogger = (*defaultLogger)(nil)
type defaultLogger struct {
stdlog *log.Logger
level Level
depth int
}
// privateLog logs a message at a given level log the default logger.
// when the level is fatal, it will exit the program.
func (l *defaultLogger) privateLog(lv Level, fmtArgs []interface{}) {
if l.level > lv {
return
}
level := lv.toString()
buf := bytebufferpool.Get()
_, _ = buf.WriteString(level) //nolint:errcheck // It is fine to ignore the error
_, _ = buf.WriteString(fmt.Sprint(fmtArgs...)) //nolint:errcheck // It is fine to ignore the error
_ = l.stdlog.Output(l.depth, buf.String()) //nolint:errcheck // It is fine to ignore the error
buf.Reset()
bytebufferpool.Put(buf)
if lv == LevelFatal {
os.Exit(1) //nolint:revive // we want to exit the program when Fatal is called
}
}
// privateLog logs a message at a given level log the default logger.
// when the level is fatal, it will exit the program.
func (l *defaultLogger) privateLogf(lv Level, format string, fmtArgs []interface{}) {
if l.level > lv {
return
}
level := lv.toString()
buf := bytebufferpool.Get()
_, _ = buf.WriteString(level) //nolint:errcheck // It is fine to ignore the error
if len(fmtArgs) > 0 {
_, _ = fmt.Fprintf(buf, format, fmtArgs...)
} else {
_, _ = fmt.Fprint(buf, fmtArgs...)
}
_ = l.stdlog.Output(l.depth, buf.String()) //nolint:errcheck // It is fine to ignore the error
buf.Reset()
bytebufferpool.Put(buf)
if lv == LevelFatal {
os.Exit(1) //nolint:revive // we want to exit the program when Fatal is called
}
}
// privateLogw logs a message at a given level log the default logger.
// when the level is fatal, it will exit the program.
func (l *defaultLogger) privateLogw(lv Level, format string, keysAndValues []interface{}) {
if l.level > lv {
return
}
level := lv.toString()
buf := bytebufferpool.Get()
_, _ = buf.WriteString(level) //nolint:errcheck // It is fine to ignore the error
// Write format privateLog buffer
if format != "" {
_, _ = buf.WriteString(format) //nolint:errcheck // It is fine to ignore the error
}
var once sync.Once
isFirst := true
// Write keys and values privateLog buffer
if len(keysAndValues) > 0 {
if (len(keysAndValues) & 1) == 1 {
keysAndValues = append(keysAndValues, "KEYVALS UNPAIRED")
}
for i := 0; i < len(keysAndValues); i += 2 {
if format == "" && isFirst {
once.Do(func() {
_, _ = fmt.Fprintf(buf, "%s=%v", keysAndValues[i], keysAndValues[i+1])
isFirst = false
})
continue
}
_, _ = fmt.Fprintf(buf, " %s=%v", keysAndValues[i], keysAndValues[i+1])
}
}
_ = l.stdlog.Output(l.depth, buf.String()) //nolint:errcheck // It is fine to ignore the error
buf.Reset()
bytebufferpool.Put(buf)
if lv == LevelFatal {
os.Exit(1) //nolint:revive // we want to exit the program when Fatal is called
}
}
func (l *defaultLogger) Trace(v ...interface{}) {
l.privateLog(LevelTrace, v)
}
func (l *defaultLogger) Debug(v ...interface{}) {
l.privateLog(LevelDebug, v)
}
func (l *defaultLogger) Info(v ...interface{}) {
l.privateLog(LevelInfo, v)
}
func (l *defaultLogger) Warn(v ...interface{}) {
l.privateLog(LevelWarn, v)
}
func (l *defaultLogger) Error(v ...interface{}) {
l.privateLog(LevelError, v)
}
func (l *defaultLogger) Fatal(v ...interface{}) {
l.privateLog(LevelFatal, v)
}
func (l *defaultLogger) Panic(v ...interface{}) {
l.privateLog(LevelPanic, v)
}
func (l *defaultLogger) Tracef(format string, v ...interface{}) {
l.privateLogf(LevelTrace, format, v)
}
func (l *defaultLogger) Debugf(format string, v ...interface{}) {
l.privateLogf(LevelDebug, format, v)
}
func (l *defaultLogger) Infof(format string, v ...interface{}) {
l.privateLogf(LevelInfo, format, v)
}
func (l *defaultLogger) Warnf(format string, v ...interface{}) {
l.privateLogf(LevelWarn, format, v)
}
func (l *defaultLogger) Errorf(format string, v ...interface{}) {
l.privateLogf(LevelError, format, v)
}
func (l *defaultLogger) Fatalf(format string, v ...interface{}) {
l.privateLogf(LevelFatal, format, v)
}
func (l *defaultLogger) Panicf(format string, v ...interface{}) {
l.privateLogf(LevelPanic, format, v)
}
func (l *defaultLogger) Tracew(msg string, keysAndValues ...interface{}) {
l.privateLogw(LevelTrace, msg, keysAndValues)
}
func (l *defaultLogger) Debugw(msg string, keysAndValues ...interface{}) {
l.privateLogw(LevelDebug, msg, keysAndValues)
}
func (l *defaultLogger) Infow(msg string, keysAndValues ...interface{}) {
l.privateLogw(LevelInfo, msg, keysAndValues)
}
func (l *defaultLogger) Warnw(msg string, keysAndValues ...interface{}) {
l.privateLogw(LevelWarn, msg, keysAndValues)
}
func (l *defaultLogger) Errorw(msg string, keysAndValues ...interface{}) {
l.privateLogw(LevelError, msg, keysAndValues)
}
func (l *defaultLogger) Fatalw(msg string, keysAndValues ...interface{}) {
l.privateLogw(LevelFatal, msg, keysAndValues)
}
func (l *defaultLogger) Panicw(msg string, keysAndValues ...interface{}) {
l.privateLogw(LevelPanic, msg, keysAndValues)
}
func (l *defaultLogger) WithContext(_ context.Context) CommonLogger {
return &defaultLogger{
stdlog: l.stdlog,
level: l.level,
depth: l.depth - 1,
}
}
func (l *defaultLogger) SetLevel(level Level) {
l.level = level
}
func (l *defaultLogger) SetOutput(writer io.Writer) {
l.stdlog.SetOutput(writer)
}
// DefaultLogger returns the default logger.
func DefaultLogger() AllLogger {
return logger
}
+141
View File
@@ -0,0 +1,141 @@
package log
import (
"context"
"io"
)
// Fatal calls the default logger's Fatal method and then os.Exit(1).
func Fatal(v ...interface{}) {
logger.Fatal(v...)
}
// Error calls the default logger's Error method.
func Error(v ...interface{}) {
logger.Error(v...)
}
// Warn calls the default logger's Warn method.
func Warn(v ...interface{}) {
logger.Warn(v...)
}
// Info calls the default logger's Info method.
func Info(v ...interface{}) {
logger.Info(v...)
}
// Debug calls the default logger's Debug method.
func Debug(v ...interface{}) {
logger.Debug(v...)
}
// Trace calls the default logger's Trace method.
func Trace(v ...interface{}) {
logger.Trace(v...)
}
// Panic calls the default logger's Panic method.
func Panic(v ...interface{}) {
logger.Panic(v...)
}
// Fatalf calls the default logger's Fatalf method and then os.Exit(1).
func Fatalf(format string, v ...interface{}) {
logger.Fatalf(format, v...)
}
// Errorf calls the default logger's Errorf method.
func Errorf(format string, v ...interface{}) {
logger.Errorf(format, v...)
}
// Warnf calls the default logger's Warnf method.
func Warnf(format string, v ...interface{}) {
logger.Warnf(format, v...)
}
// Infof calls the default logger's Infof method.
func Infof(format string, v ...interface{}) {
logger.Infof(format, v...)
}
// Debugf calls the default logger's Debugf method.
func Debugf(format string, v ...interface{}) {
logger.Debugf(format, v...)
}
// Tracef calls the default logger's Tracef method.
func Tracef(format string, v ...interface{}) {
logger.Tracef(format, v...)
}
// Panicf calls the default logger's Tracef method.
func Panicf(format string, v ...interface{}) {
logger.Panicf(format, v...)
}
// Tracew logs a message with some additional context. The variadic key-value
// pairs are treated as they are privateLog With.
func Tracew(msg string, keysAndValues ...interface{}) {
logger.Tracew(msg, keysAndValues...)
}
// Debugw logs a message with some additional context. The variadic key-value
// pairs are treated as they are privateLog With.
func Debugw(msg string, keysAndValues ...interface{}) {
logger.Debugw(msg, keysAndValues...)
}
// Infow logs a message with some additional context. The variadic key-value
// pairs are treated as they are privateLog With.
func Infow(msg string, keysAndValues ...interface{}) {
logger.Infow(msg, keysAndValues...)
}
// Warnw logs a message with some additional context. The variadic key-value
// pairs are treated as they are privateLog With.
func Warnw(msg string, keysAndValues ...interface{}) {
logger.Warnw(msg, keysAndValues...)
}
// Errorw logs a message with some additional context. The variadic key-value
// pairs are treated as they are privateLog With.
func Errorw(msg string, keysAndValues ...interface{}) {
logger.Errorw(msg, keysAndValues...)
}
// Fatalw logs a message with some additional context. The variadic key-value
// pairs are treated as they are privateLog With.
func Fatalw(msg string, keysAndValues ...interface{}) {
logger.Fatalw(msg, keysAndValues...)
}
// Panicw logs a message with some additional context. The variadic key-value
// pairs are treated as they are privateLog With.
func Panicw(msg string, keysAndValues ...interface{}) {
logger.Panicw(msg, keysAndValues...)
}
func WithContext(ctx context.Context) CommonLogger {
return logger.WithContext(ctx)
}
// SetLogger sets the default logger and the system logger.
// Note that this method is not concurrent-safe and must not be called
// after the use of DefaultLogger and global functions privateLog this package.
func SetLogger(v AllLogger) {
logger = v
}
// SetOutput sets the output of default logger and system logger. By default, it is stderr.
func SetOutput(w io.Writer) {
logger.SetOutput(w)
}
// SetLevel sets the level of logs below which logs will not be output.
// The default logger is LevelTrace.
// Note that this method is not concurrent-safe.
func SetLevel(lv Level) {
logger.SetLevel(lv)
}
+100
View File
@@ -0,0 +1,100 @@
package log
import (
"context"
"fmt"
"io"
"log"
"os"
)
var logger AllLogger = &defaultLogger{
stdlog: log.New(os.Stderr, "", log.LstdFlags|log.Lshortfile|log.Lmicroseconds),
depth: 4,
}
// Logger is a logger interface that provides logging function with levels.
type Logger interface {
Trace(v ...interface{})
Debug(v ...interface{})
Info(v ...interface{})
Warn(v ...interface{})
Error(v ...interface{})
Fatal(v ...interface{})
Panic(v ...interface{})
}
// FormatLogger is a logger interface that output logs with a format.
type FormatLogger interface {
Tracef(format string, v ...interface{})
Debugf(format string, v ...interface{})
Infof(format string, v ...interface{})
Warnf(format string, v ...interface{})
Errorf(format string, v ...interface{})
Fatalf(format string, v ...interface{})
Panicf(format string, v ...interface{})
}
// WithLogger is a logger interface that output logs with a message and key-value pairs.
type WithLogger interface {
Tracew(msg string, keysAndValues ...interface{})
Debugw(msg string, keysAndValues ...interface{})
Infow(msg string, keysAndValues ...interface{})
Warnw(msg string, keysAndValues ...interface{})
Errorw(msg string, keysAndValues ...interface{})
Fatalw(msg string, keysAndValues ...interface{})
Panicw(msg string, keysAndValues ...interface{})
}
type CommonLogger interface {
Logger
FormatLogger
WithLogger
}
// ControlLogger provides methods to config a logger.
type ControlLogger interface {
SetLevel(Level)
SetOutput(io.Writer)
}
// AllLogger is the combination of Logger, FormatLogger, CtxLogger and ControlLogger.
// Custom extensions can be made through AllLogger
type AllLogger interface {
CommonLogger
ControlLogger
WithContext(ctx context.Context) CommonLogger
}
// Level defines the priority of a log message.
// When a logger is configured with a level, any log message with a lower
// log level (smaller by integer comparison) will not be output.
type Level int
// The levels of logs.
const (
LevelTrace Level = iota
LevelDebug
LevelInfo
LevelWarn
LevelError
LevelFatal
LevelPanic
)
var strs = []string{
"[Trace] ",
"[Debug] ",
"[Info] ",
"[Warn] ",
"[Error] ",
"[Fatal] ",
"[Panic] ",
}
func (lv Level) toString() string {
if lv >= LevelTrace && lv <= LevelPanic {
return strs[lv]
}
return fmt.Sprintf("[?%d] ", lv)
}