Fetch the repository succeeded.
package logger
import (
"fmt"
"io"
"os"
"gitee.com/go-better/dev/debug/errs"
)
const (
Fatal Level = iota
Panic
Recover
Error
Info
Debug
)
type Level int8
type Logger struct {
level Level
fields map[string]interface{}
formatter Formatter
alarmFormatter Formatter
writer io.Writer
alarm Alarm
}
type Alarm interface {
Send(title, content string)
Alarm(title, content, mergeKey string)
}
func New(writer io.Writer) *Logger {
if writer == nil {
writer = os.Stderr
}
var formatter Formatter
if writer == os.Stdout || writer == os.Stderr {
formatter = readableFormatter
} else {
formatter = jsonFormatter
}
hostname, _ := os.Hostname()
return &Logger{
level: Info, writer: writer,
formatter: formatter, alarmFormatter: readableFormatter,
fields: map[string]interface{}{"machineName": hostname},
}
}
// don't use (level, at, msg, stack, duration) as key, they will be overwritten.
func (l *Logger) With(key string, value interface{}) *Fields {
return &Fields{Logger: l, data: map[string]interface{}{key: value}}
}
func (l *Logger) Debug(args ...interface{}) bool {
if len(args) > 0 && l.level >= Debug {
l.output(Debug, fmt.Sprint(args...), getExtraFields(args, nil))
}
return l.level >= Debug
}
func (l *Logger) Debugf(format string, args ...interface{}) {
if l.level >= Debug {
l.output(Debug, fmt.Sprintf(format, args...), getExtraFields(args, nil))
}
}
func (l *Logger) Info(args ...interface{}) bool {
if len(args) > 0 && l.level >= Info {
l.output(Info, fmt.Sprint(args...), getExtraFields(args, nil))
}
return l.level >= Info
}
func (l *Logger) Infof(format string, args ...interface{}) {
if l.level >= Info {
l.output(Info, fmt.Sprintf(format, args...), getExtraFields(args, nil))
}
}
func (l *Logger) Error(args ...interface{}) {
l.output(Error, fmt.Sprint(args...), getExtraFields(args, &errs.Stack{Skip: 1}))
}
func (l *Logger) Errorf(format string, args ...interface{}) {
l.output(Error, fmt.Sprintf(format, args...), getExtraFields(args, &errs.Stack{Skip: 1}))
}
func (l *Logger) Recover() {
if err := recover(); err != nil {
l.output(Recover, fmt.Sprint(err), getExtraFields([]interface{}{err}, &errs.Stack{}))
}
}
func (l *Logger) Panic(args ...interface{}) {
msg := fmt.Sprint(args...)
l.output(Panic, fmt.Sprint(args...), getExtraFields(args, &errs.Stack{Skip: 1}))
panic(msg)
}
func (l *Logger) Panicf(format string, args ...interface{}) {
msg := fmt.Sprintf(format, args...)
l.output(Panic, msg, getExtraFields(args, &errs.Stack{Skip: 1}))
panic(msg)
}
func (l *Logger) Fatal(args ...interface{}) {
l.output(Fatal, fmt.Sprint(args...), getExtraFields(args, &errs.Stack{Skip: 1}))
exitFunc(1)
}
func (l *Logger) Fatalf(format string, args ...interface{}) {
l.output(Fatal, fmt.Sprintf(format, args...), getExtraFields(args, &errs.Stack{Skip: 1}))
exitFunc(1)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。