2024-04-09 10:17:08 +00:00
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2024-04-15 09:01:42 +00:00
|
|
|
var dlog *logrus.Logger
|
2024-04-09 10:17:08 +00:00
|
|
|
|
|
|
|
func init() {
|
2024-04-15 09:01:42 +00:00
|
|
|
dlog = New(0)
|
|
|
|
}
|
2024-04-09 10:17:08 +00:00
|
|
|
|
2024-04-15 09:01:42 +00:00
|
|
|
func New(skip int) *logrus.Logger {
|
|
|
|
myl := logrus.New()
|
2024-04-09 10:17:08 +00:00
|
|
|
|
2024-04-15 09:01:42 +00:00
|
|
|
// 配置 Logstash 作为输出
|
|
|
|
myl.AddHook(NewUTCTimeHook())
|
2024-04-09 10:17:08 +00:00
|
|
|
jf := &JSONFormatter{
|
2024-04-15 09:01:42 +00:00
|
|
|
skip: make([]*levelSkip, len(logrus.AllLevels)),
|
|
|
|
diffSkip: skip,
|
2024-04-09 10:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := range jf.skip {
|
|
|
|
jf.skip[i] = &levelSkip{}
|
|
|
|
}
|
|
|
|
|
2024-04-15 09:01:42 +00:00
|
|
|
myl.Formatter = jf
|
|
|
|
myl.SetReportCaller(true)
|
2024-04-09 10:17:08 +00:00
|
|
|
|
2024-04-15 09:01:42 +00:00
|
|
|
if dlog != nil {
|
|
|
|
myl.SetOutput(dlog.Out)
|
|
|
|
}
|
2024-04-09 10:17:08 +00:00
|
|
|
|
2024-04-15 09:01:42 +00:00
|
|
|
return myl
|
2024-04-09 10:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type SkipHook struct {
|
2024-04-15 09:01:42 +00:00
|
|
|
autoSkip int
|
2024-04-09 10:17:08 +00:00
|
|
|
Formatter func(*logrus.Hook, *logrus.Entry) error
|
|
|
|
once sync.Once
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *SkipHook) Levels() []logrus.Level {
|
|
|
|
return logrus.AllLevels
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *SkipHook) Fire(e *logrus.Entry) error {
|
2024-04-15 09:01:42 +00:00
|
|
|
h.once.Do(func() {
|
|
|
|
for i := 4; i < 100; i++ {
|
|
|
|
log.Println(i)
|
|
|
|
if pc, file, line, ok := runtime.Caller(i); ok {
|
|
|
|
funcStruct := runtime.FuncForPC(pc)
|
|
|
|
log.Println(funcStruct.Name(), file, line)
|
|
|
|
if !strings.Contains(funcStruct.Name(), "github.com/sirupsen/logrus.") {
|
|
|
|
h.autoSkip++
|
|
|
|
if h.autoSkip >= 2 {
|
|
|
|
h.autoSkip = i - 3
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
2024-04-09 10:17:08 +00:00
|
|
|
|
|
|
|
}
|
2024-04-15 09:01:42 +00:00
|
|
|
})
|
2024-04-09 10:17:08 +00:00
|
|
|
|
2024-04-15 09:01:42 +00:00
|
|
|
if _, file, line, ok := runtime.Caller(h.autoSkip); ok {
|
2024-04-09 10:17:08 +00:00
|
|
|
// funcStruct := runtime.FuncForPC(pc)
|
|
|
|
// log.Println(file, line, funcStruct.Name())
|
|
|
|
// funcName := funcStruct.Name()
|
|
|
|
file := fmt.Sprintf("%s:%d", file, line)
|
|
|
|
e.Data["file"] = file
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 自定义钩子以设置时间为UTC
|
|
|
|
type UTCTimeHook struct{}
|
|
|
|
|
|
|
|
func NewUTCTimeHook() *UTCTimeHook {
|
|
|
|
return &UTCTimeHook{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hook *UTCTimeHook) Levels() []logrus.Level {
|
|
|
|
return logrus.AllLevels
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hook *UTCTimeHook) Fire(entry *logrus.Entry) error {
|
|
|
|
entry.Time = time.Now().UTC()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithField allocates a new entry and adds a field to it.
|
|
|
|
// Debug, Print, Info, Warn, Error, Fatal or Panic must be then applied to
|
|
|
|
// this new returned entry.
|
|
|
|
// If you want multiple fields, use `WithFields`.
|
|
|
|
func WithField(key string, value interface{}) *logrus.Entry {
|
2024-04-15 09:01:42 +00:00
|
|
|
return dlog.WithField(key, value)
|
2024-04-09 10:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Adds a struct of fields to the log entry. All it does is call `WithField` for
|
|
|
|
// each `Field`.
|
|
|
|
func WithFields(fields logrus.Fields) *logrus.Entry {
|
2024-04-15 09:01:42 +00:00
|
|
|
return dlog.WithFields(fields)
|
2024-04-09 10:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add an error as single field to the log entry. All it does is call
|
|
|
|
// `WithError` for the given `error`.
|
|
|
|
func WithError(err error) *logrus.Entry {
|
|
|
|
|
2024-04-15 09:01:42 +00:00
|
|
|
return dlog.WithError(err)
|
2024-04-09 10:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add a context to the log entry.
|
|
|
|
func WithContext(ctx context.Context) *logrus.Entry {
|
|
|
|
|
2024-04-15 09:01:42 +00:00
|
|
|
return dlog.WithContext(ctx)
|
2024-04-09 10:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Overrides the time of the log entry.
|
|
|
|
func WithTime(t time.Time) *logrus.Entry {
|
|
|
|
|
2024-04-15 09:01:42 +00:00
|
|
|
return dlog.WithTime(t)
|
2024-04-09 10:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Logf(level logrus.Level, format string, args ...interface{}) {
|
2024-04-15 09:01:42 +00:00
|
|
|
dlog.Logf(level, format, args...)
|
2024-04-09 10:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Tracef(format string, args ...interface{}) {
|
2024-04-15 09:01:42 +00:00
|
|
|
dlog.Tracef(format, args...)
|
2024-04-09 10:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Debugf(format string, args ...interface{}) {
|
2024-04-15 09:01:42 +00:00
|
|
|
dlog.Debugf(format, args...)
|
2024-04-09 10:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Infof(format string, args ...interface{}) {
|
2024-04-15 09:01:42 +00:00
|
|
|
dlog.Infof(format, args...)
|
2024-04-09 10:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Printf(format string, args ...interface{}) {
|
2024-04-15 09:01:42 +00:00
|
|
|
dlog.Printf(format, args...)
|
2024-04-09 10:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Warnf(format string, args ...interface{}) {
|
2024-04-15 09:01:42 +00:00
|
|
|
dlog.Warnf(format, args...)
|
2024-04-09 10:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Warningf(format string, args ...interface{}) {
|
2024-04-15 09:01:42 +00:00
|
|
|
dlog.Warningf(format, args...)
|
2024-04-09 10:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Errorf(format string, args ...interface{}) {
|
2024-04-15 09:01:42 +00:00
|
|
|
dlog.Errorf(format, args...)
|
2024-04-09 10:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Fatalf(format string, args ...interface{}) {
|
2024-04-15 09:01:42 +00:00
|
|
|
dlog.Fatalf(format, args...)
|
2024-04-09 10:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Panicf(format string, args ...interface{}) {
|
2024-04-15 09:01:42 +00:00
|
|
|
dlog.Panicf(format, args...)
|
2024-04-09 10:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Log(level logrus.Level, args ...interface{}) {
|
2024-04-15 09:01:42 +00:00
|
|
|
dlog.Log(level, args...)
|
2024-04-09 10:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Trace(args ...interface{}) {
|
2024-04-15 09:01:42 +00:00
|
|
|
dlog.Trace(args...)
|
2024-04-09 10:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Debug(args ...interface{}) {
|
2024-04-15 09:01:42 +00:00
|
|
|
dlog.Debug(args...)
|
2024-04-09 10:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Info(args ...interface{}) {
|
2024-04-15 09:01:42 +00:00
|
|
|
dlog.Info(args...)
|
2024-04-09 10:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Print(args ...interface{}) {
|
2024-04-15 09:01:42 +00:00
|
|
|
dlog.Print(args...)
|
2024-04-09 10:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Warn(args ...interface{}) {
|
2024-04-15 09:01:42 +00:00
|
|
|
dlog.Warn(args...)
|
2024-04-09 10:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Warning(args ...interface{}) {
|
2024-04-15 09:01:42 +00:00
|
|
|
dlog.Warning(args...)
|
2024-04-09 10:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Error(args ...interface{}) {
|
2024-04-15 09:01:42 +00:00
|
|
|
dlog.Error(args...)
|
2024-04-09 10:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Fatal(args ...interface{}) {
|
2024-04-15 09:01:42 +00:00
|
|
|
dlog.Fatal(args...)
|
2024-04-09 10:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Panic(args ...interface{}) {
|
2024-04-15 09:01:42 +00:00
|
|
|
dlog.Panic(args...)
|
2024-04-09 10:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Logln(level logrus.Level, args ...interface{}) {
|
2024-04-15 09:01:42 +00:00
|
|
|
dlog.Logln(level, args...)
|
2024-04-09 10:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Traceln(args ...interface{}) {
|
2024-04-15 09:01:42 +00:00
|
|
|
dlog.Traceln(args...)
|
2024-04-09 10:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Debugln(args ...interface{}) {
|
2024-04-15 09:01:42 +00:00
|
|
|
dlog.Debugln(args...)
|
2024-04-09 10:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Infoln(args ...interface{}) {
|
2024-04-15 09:01:42 +00:00
|
|
|
dlog.Infoln(args...)
|
2024-04-09 10:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Println(args ...interface{}) {
|
2024-04-15 09:01:42 +00:00
|
|
|
dlog.Println(args...)
|
2024-04-09 10:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Warnln(args ...interface{}) {
|
2024-04-15 09:01:42 +00:00
|
|
|
dlog.Warnln(args...)
|
2024-04-09 10:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Warningln(args ...interface{}) {
|
2024-04-15 09:01:42 +00:00
|
|
|
dlog.Warningln(args...)
|
2024-04-09 10:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Errorln(args ...interface{}) {
|
2024-04-15 09:01:42 +00:00
|
|
|
dlog.Errorln(args...)
|
2024-04-09 10:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Fatalln(args ...interface{}) {
|
2024-04-15 09:01:42 +00:00
|
|
|
dlog.Fatalln(args...)
|
2024-04-09 10:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Panicln(args ...interface{}) {
|
2024-04-15 09:01:42 +00:00
|
|
|
dlog.Panicln(args...)
|
2024-04-09 10:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Exit(code int) {
|
2024-04-15 09:01:42 +00:00
|
|
|
dlog.Exit(code)
|
2024-04-09 10:17:08 +00:00
|
|
|
}
|