2018-04-19 16:24:48 +08:00
|
|
|
|
// Copyright 2017 gf Author(https://gitee.com/johng/gf). All Rights Reserved.
|
|
|
|
|
//
|
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
|
// You can obtain one at https://gitee.com/johng/gf.
|
2018-08-30 00:00:15 +08:00
|
|
|
|
// @author john, zseeker
|
2018-04-19 16:24:48 +08:00
|
|
|
|
|
|
|
|
|
package glog
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2018-08-24 23:41:58 +08:00
|
|
|
|
"gitee.com/johng/gf/g/container/gtype"
|
2018-11-03 15:27:48 +08:00
|
|
|
|
"gitee.com/johng/gf/g/os/gfile"
|
2018-08-30 00:00:15 +08:00
|
|
|
|
"gitee.com/johng/gf/g/os/gmlock"
|
2018-10-09 21:41:07 +08:00
|
|
|
|
"gitee.com/johng/gf/g/os/gtime"
|
2018-11-03 15:27:48 +08:00
|
|
|
|
"gitee.com/johng/gf/g/util/gregex"
|
|
|
|
|
"io"
|
|
|
|
|
"os"
|
|
|
|
|
"runtime"
|
|
|
|
|
"strings"
|
|
|
|
|
"sync"
|
|
|
|
|
"time"
|
2018-04-19 16:24:48 +08:00
|
|
|
|
)
|
|
|
|
|
|
2018-10-09 21:41:07 +08:00
|
|
|
|
type Logger struct {
|
|
|
|
|
mu sync.RWMutex
|
|
|
|
|
pr *Logger // 父级Logger
|
|
|
|
|
io io.Writer // 日志内容写入的IO接口
|
|
|
|
|
path *gtype.String // 日志写入的目录路径
|
|
|
|
|
file *gtype.String // 日志文件名称格式
|
|
|
|
|
level *gtype.Int // 日志输出等级
|
2018-10-10 18:39:57 +08:00
|
|
|
|
btSkip *gtype.Int // 错误产生时的backtrace回调信息skip条数
|
2018-10-09 21:41:07 +08:00
|
|
|
|
btEnabled *gtype.Bool // 是否当打印错误时同时开启backtrace打印
|
2018-10-18 13:43:00 +08:00
|
|
|
|
printHeader *gtype.Bool // 是否不打印前缀信息(时间,级别等)
|
2018-10-09 21:41:07 +08:00
|
|
|
|
alsoStdPrint *gtype.Bool // 控制台打印开关,当输出到文件/自定义输出时也同时打印到终端
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-19 16:24:48 +08:00
|
|
|
|
const (
|
2018-10-09 21:41:07 +08:00
|
|
|
|
gDEFAULT_FILE_FORMAT = `{Y-m-d}.log`
|
2018-04-19 16:24:48 +08:00
|
|
|
|
gDEFAULT_FILE_POOL_FLAGS = os.O_CREATE|os.O_WRONLY|os.O_APPEND
|
|
|
|
|
)
|
|
|
|
|
|
2018-10-18 13:43:00 +08:00
|
|
|
|
var (
|
|
|
|
|
// 默认的日志换行符
|
|
|
|
|
ln = "\n"
|
|
|
|
|
// 标准输出互斥锁,防止标准输出串日志
|
|
|
|
|
stdMu = sync.RWMutex{}
|
|
|
|
|
)
|
2018-05-23 16:41:21 +08:00
|
|
|
|
|
|
|
|
|
// 初始化日志换行符
|
|
|
|
|
func init() {
|
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
|
ln = "\r\n"
|
2018-05-24 15:50:35 +08:00
|
|
|
|
}
|
2018-05-23 16:41:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-28 17:06:49 +08:00
|
|
|
|
// 新建自定义的日志操作对象
|
|
|
|
|
func New() *Logger {
|
2018-08-24 23:41:58 +08:00
|
|
|
|
return &Logger {
|
2018-08-30 00:00:15 +08:00
|
|
|
|
io : nil,
|
|
|
|
|
path : gtype.NewString(),
|
2018-10-09 21:41:07 +08:00
|
|
|
|
file : gtype.NewString(gDEFAULT_FILE_FORMAT),
|
2018-08-30 13:54:45 +08:00
|
|
|
|
level : gtype.NewInt(defaultLevel.Val()),
|
2018-10-10 18:39:57 +08:00
|
|
|
|
btSkip : gtype.NewInt(),
|
2018-08-30 00:00:15 +08:00
|
|
|
|
btEnabled : gtype.NewBool(true),
|
2018-10-18 13:43:00 +08:00
|
|
|
|
printHeader : gtype.NewBool(true),
|
2018-08-30 00:00:15 +08:00
|
|
|
|
alsoStdPrint : gtype.NewBool(true),
|
2018-08-24 23:41:58 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-28 17:06:49 +08:00
|
|
|
|
// Logger深拷贝
|
|
|
|
|
func (l *Logger) Clone() *Logger {
|
|
|
|
|
return &Logger {
|
2018-08-30 00:00:15 +08:00
|
|
|
|
pr : l,
|
2018-10-18 13:43:00 +08:00
|
|
|
|
io : l.GetWriter(),
|
2018-08-30 00:00:15 +08:00
|
|
|
|
path : l.path.Clone(),
|
2018-10-10 15:24:30 +08:00
|
|
|
|
file : l.file.Clone(),
|
2018-08-30 13:00:49 +08:00
|
|
|
|
level : l.level.Clone(),
|
2018-10-10 18:39:57 +08:00
|
|
|
|
btSkip : l.btSkip.Clone(),
|
2018-08-30 00:00:15 +08:00
|
|
|
|
btEnabled : l.btEnabled.Clone(),
|
2018-10-18 13:43:00 +08:00
|
|
|
|
printHeader : l.printHeader.Clone(),
|
2018-08-30 00:00:15 +08:00
|
|
|
|
alsoStdPrint : l.alsoStdPrint.Clone(),
|
2018-08-24 23:41:58 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-30 13:00:49 +08:00
|
|
|
|
// 设置日志记录等级
|
|
|
|
|
func (l *Logger) SetLevel(level int) {
|
|
|
|
|
l.level.Set(level)
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-30 13:54:45 +08:00
|
|
|
|
// 获取日志记录等级
|
|
|
|
|
func (l *Logger) GetLevel() int {
|
|
|
|
|
return l.level.Val()
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-30 13:00:49 +08:00
|
|
|
|
// 快捷方法,打开或关闭DEBU日志信息
|
|
|
|
|
func (l *Logger) SetDebug(debug bool) {
|
2018-09-18 21:29:31 +08:00
|
|
|
|
if debug {
|
2018-09-26 09:58:49 +08:00
|
|
|
|
l.level.Set(l.level.Val() | LEVEL_DEBU)
|
2018-09-18 21:29:31 +08:00
|
|
|
|
} else {
|
2018-09-26 09:58:49 +08:00
|
|
|
|
l.level.Set(l.level.Val() & ^LEVEL_DEBU)
|
2018-09-18 21:29:31 +08:00
|
|
|
|
}
|
2018-08-30 13:00:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (l *Logger) SetBacktrace(enabled bool) {
|
|
|
|
|
l.btEnabled.Set(enabled)
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-10 18:39:57 +08:00
|
|
|
|
// 设置BacktraceSkip
|
|
|
|
|
func (l *Logger) SetBacktraceSkip(skip int) {
|
|
|
|
|
l.btSkip.Set(skip)
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-24 23:41:58 +08:00
|
|
|
|
// 可自定义IO接口,IO可以是文件输出、标准输出、网络输出
|
2018-11-06 18:53:25 +08:00
|
|
|
|
func (l *Logger) SetWriter(writer io.Writer) {
|
2018-08-19 11:25:15 +08:00
|
|
|
|
l.mu.Lock()
|
2018-11-06 18:53:25 +08:00
|
|
|
|
l.io = writer
|
2018-08-19 11:25:15 +08:00
|
|
|
|
l.mu.Unlock()
|
2018-04-19 16:24:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-24 23:41:58 +08:00
|
|
|
|
// 返回自定义的IO,默认为nil
|
2018-10-18 13:43:00 +08:00
|
|
|
|
func (l *Logger) GetWriter() io.Writer {
|
2018-04-19 16:24:48 +08:00
|
|
|
|
l.mu.RLock()
|
|
|
|
|
r := l.io
|
|
|
|
|
l.mu.RUnlock()
|
|
|
|
|
return r
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取默认的文件IO
|
2018-11-03 15:27:48 +08:00
|
|
|
|
func (l *Logger) getFilePointer() *os.File {
|
2018-04-19 16:24:48 +08:00
|
|
|
|
if path := l.path.Val(); path != "" {
|
2018-10-09 21:41:07 +08:00
|
|
|
|
// 文件名称中使用"{}"包含的内容使用gtime格式化
|
|
|
|
|
file, _ := gregex.ReplaceStringFunc(`{.+?}`, l.file.Val(), func(s string) string {
|
|
|
|
|
return gtime.Now().Format(strings.Trim(s, "{}"))
|
|
|
|
|
})
|
|
|
|
|
fpath := path + gfile.Separator + file
|
2018-11-03 15:27:48 +08:00
|
|
|
|
if fp, err := gfile.OpenWithFlagPerm(fpath, gDEFAULT_FILE_POOL_FLAGS, 0666); err == nil {
|
2018-04-19 16:24:48 +08:00
|
|
|
|
return fp
|
|
|
|
|
} else {
|
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置日志文件的存储目录路径
|
|
|
|
|
func (l *Logger) SetPath(path string) error {
|
2018-09-16 16:38:17 +08:00
|
|
|
|
// 如果目录不存在,则递归创建
|
|
|
|
|
if !gfile.Exists(path) {
|
|
|
|
|
if err := gfile.Mkdir(path); err != nil {
|
|
|
|
|
fmt.Fprintln(os.Stderr, fmt.Sprintf(`glog mkdir "%s" failed: %s`, path, err.Error()))
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-19 16:24:48 +08:00
|
|
|
|
l.path.Set(strings.TrimRight(path, gfile.Separator))
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-10 18:39:57 +08:00
|
|
|
|
// 获取设置的日志目录路径
|
|
|
|
|
func (l *Logger) GetPath() string {
|
|
|
|
|
return l.path.Val()
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-09 21:41:07 +08:00
|
|
|
|
// 日志文件名称
|
|
|
|
|
func (l *Logger) SetFile(file string) {
|
|
|
|
|
l.file.Set(file)
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-24 12:37:55 +08:00
|
|
|
|
// 设置写日志时开启or关闭控制台打印,默认是关闭的
|
2018-08-28 19:38:05 +08:00
|
|
|
|
func (l *Logger) SetStdPrint(enabled bool) {
|
2018-08-30 00:00:15 +08:00
|
|
|
|
l.alsoStdPrint.Set(enabled)
|
2018-05-24 12:37:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-19 16:24:48 +08:00
|
|
|
|
// 这里的写锁保证统一时刻只会写入一行日志,防止串日志的情况
|
2018-08-30 00:00:15 +08:00
|
|
|
|
func (l *Logger) print(std io.Writer, s string) {
|
2018-08-24 23:41:58 +08:00
|
|
|
|
// 优先使用自定义的IO输出
|
2018-10-18 13:43:00 +08:00
|
|
|
|
if l.printHeader.Val() {
|
|
|
|
|
s = l.format(s)
|
|
|
|
|
}
|
|
|
|
|
writer := l.GetWriter()
|
2018-07-26 13:00:04 +08:00
|
|
|
|
if writer == nil {
|
2018-10-18 13:43:00 +08:00
|
|
|
|
// 如果设置的writer为空,那么其次判断是否有文件输出设置
|
|
|
|
|
// 内部使用了内存锁,保证在glog中对同一个日志文件的并发写入不会串日志(并发安全)
|
2018-09-14 17:02:07 +08:00
|
|
|
|
if f := l.getFilePointer(); f != nil {
|
2018-07-26 13:00:04 +08:00
|
|
|
|
defer f.Close()
|
2018-08-30 00:00:15 +08:00
|
|
|
|
key := l.path.Val()
|
|
|
|
|
gmlock.Lock(key)
|
2018-10-18 13:43:00 +08:00
|
|
|
|
_, err := io.WriteString(f, s)
|
2018-08-30 00:00:15 +08:00
|
|
|
|
gmlock.Unlock(key)
|
2018-09-15 16:40:13 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Fprintln(os.Stderr, err.Error())
|
|
|
|
|
}
|
2018-04-19 16:24:48 +08:00
|
|
|
|
}
|
2018-11-06 18:53:25 +08:00
|
|
|
|
// 当没有设置writer时,需要判断是否允许输出到标准输出
|
|
|
|
|
if l.alsoStdPrint.Val() {
|
|
|
|
|
l.doStdLockPrint(std, s)
|
|
|
|
|
}
|
2018-08-30 00:00:15 +08:00
|
|
|
|
} else {
|
2018-10-18 13:43:00 +08:00
|
|
|
|
l.doStdLockPrint(writer, s)
|
2018-08-30 00:00:15 +08:00
|
|
|
|
}
|
2018-04-19 16:24:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-18 13:43:00 +08:00
|
|
|
|
// 并发安全打印到标准输出
|
|
|
|
|
func (l *Logger) doStdLockPrint(std io.Writer, s string) {
|
|
|
|
|
stdMu.Lock()
|
|
|
|
|
if _, err := std.Write([]byte(s)); err != nil {
|
|
|
|
|
fmt.Fprintln(os.Stderr, err.Error())
|
|
|
|
|
}
|
|
|
|
|
stdMu.Unlock()
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-19 16:24:48 +08:00
|
|
|
|
// 核心打印数据方法(标准输出)
|
|
|
|
|
func (l *Logger) stdPrint(s string) {
|
|
|
|
|
l.print(os.Stdout, s)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 核心打印数据方法(标准错误)
|
|
|
|
|
func (l *Logger) errPrint(s string) {
|
|
|
|
|
// 记录调用回溯信息
|
2018-08-28 17:06:49 +08:00
|
|
|
|
if l.btEnabled.Val() {
|
2018-10-10 15:24:30 +08:00
|
|
|
|
tracestr := l.GetBacktrace()
|
|
|
|
|
if tracestr != "" {
|
|
|
|
|
backtrace := "Backtrace:" + ln + tracestr
|
|
|
|
|
if s[len(s) - 1] == byte('\n') {
|
|
|
|
|
s = s + backtrace + ln
|
|
|
|
|
} else {
|
|
|
|
|
s = s + ln + backtrace + ln
|
|
|
|
|
}
|
2018-08-28 17:06:49 +08:00
|
|
|
|
}
|
2018-04-19 16:24:48 +08:00
|
|
|
|
}
|
2018-10-18 13:43:00 +08:00
|
|
|
|
// 防止串日志情况,这里不使用stderr,而是使用stdout
|
|
|
|
|
l.print(os.Stdout, s)
|
2018-04-19 16:24:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-10 18:39:57 +08:00
|
|
|
|
// 直接打印回溯信息,参数skip表示调用端往上多少级开始回溯
|
|
|
|
|
func (l *Logger) PrintBacktrace(skip...int) {
|
|
|
|
|
l.Println(l.GetBacktrace(skip...))
|
2018-08-27 23:58:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-10 18:39:57 +08:00
|
|
|
|
// 获取文件调用回溯字符串,参数skip表示调用端往上多少级开始回溯
|
|
|
|
|
func (l *Logger) GetBacktrace(skip...int) string {
|
|
|
|
|
customSkip := 0
|
|
|
|
|
if len(skip) > 0 {
|
|
|
|
|
customSkip = skip[0]
|
|
|
|
|
}
|
2018-08-27 23:58:32 +08:00
|
|
|
|
backtrace := ""
|
2018-04-20 17:53:34 +08:00
|
|
|
|
index := 1
|
2018-10-31 16:23:31 +08:00
|
|
|
|
from := 0
|
|
|
|
|
// 首先定位业务文件开始位置
|
|
|
|
|
for i := 0; i < 10; i++ {
|
2018-11-03 15:27:48 +08:00
|
|
|
|
if _, file, _, ok := runtime.Caller(i); ok {
|
|
|
|
|
if !gregex.IsMatchString("/g/os/glog/glog.+$", file) {
|
2018-10-31 16:23:31 +08:00
|
|
|
|
from = i
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 从业务文件开始位置根据自定义的skip开始backtrace
|
2018-11-03 15:27:48 +08:00
|
|
|
|
goRoot := runtime.GOROOT()
|
2018-10-31 16:23:31 +08:00
|
|
|
|
for i := from + customSkip + l.btSkip.Val(); i < 10000; i++ {
|
2018-11-03 15:27:48 +08:00
|
|
|
|
if _, file, cline, ok := runtime.Caller(i); ok && file != "" {
|
2018-10-10 18:39:57 +08:00
|
|
|
|
// 不打印出go源码路径及glog包文件路径,日志打印必须从业务源码文件开始,且从glog包文件开始检索
|
2018-11-03 15:27:48 +08:00
|
|
|
|
if (goRoot == "" || !gregex.IsMatchString("^" + goRoot, file)) && !gregex.IsMatchString(`<autogenerated>`, file) {
|
|
|
|
|
backtrace += fmt.Sprintf(`%d. %s:%d%s`, index, file, cline, ln)
|
2018-04-20 17:53:34 +08:00
|
|
|
|
index++
|
2018-04-19 16:24:48 +08:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return backtrace
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (l *Logger) format(s string) string {
|
|
|
|
|
return time.Now().Format("2006-01-02 15:04:05.000 ") + s
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (l *Logger) Print(v ...interface{}) {
|
2018-09-29 18:25:20 +08:00
|
|
|
|
l.stdPrint(fmt.Sprintln(v...))
|
2018-04-19 16:24:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (l *Logger) Printf(format string, v ...interface{}) {
|
|
|
|
|
l.stdPrint(fmt.Sprintf(format, v...))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (l *Logger) Println(v ...interface{}) {
|
2018-09-29 18:25:20 +08:00
|
|
|
|
l.stdPrint(fmt.Sprintln(v...))
|
2018-04-19 16:24:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (l *Logger) Printfln(format string, v ...interface{}) {
|
2018-05-23 16:41:21 +08:00
|
|
|
|
l.stdPrint(fmt.Sprintf(format + ln, v...))
|
2018-04-19 16:24:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (l *Logger) Fatal(v ...interface{}) {
|
2018-09-29 18:25:20 +08:00
|
|
|
|
l.errPrint(fmt.Sprintln(v...))
|
2018-04-19 16:24:48 +08:00
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (l *Logger) Fatalf(format string, v ...interface{}) {
|
|
|
|
|
l.errPrint(fmt.Sprintf(format, v...))
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (l *Logger) Fatalln(v ...interface{}) {
|
2018-09-29 18:25:20 +08:00
|
|
|
|
l.errPrint(fmt.Sprintln(v...))
|
2018-04-19 16:24:48 +08:00
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (l *Logger) Fatalfln(format string, v ...interface{}) {
|
2018-05-23 16:41:21 +08:00
|
|
|
|
l.errPrint(fmt.Sprintf(format + ln, v...))
|
2018-04-19 16:24:48 +08:00
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (l *Logger) Panic(v ...interface{}) {
|
2018-09-29 18:25:20 +08:00
|
|
|
|
s := fmt.Sprintln(v...)
|
2018-04-19 16:24:48 +08:00
|
|
|
|
l.errPrint(s)
|
|
|
|
|
panic(s)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (l *Logger) Panicf(format string, v ...interface{}) {
|
|
|
|
|
s := fmt.Sprintf(format, v...)
|
|
|
|
|
l.errPrint(s)
|
|
|
|
|
panic(s)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (l *Logger) Panicln(v ...interface{}) {
|
2018-09-29 18:25:20 +08:00
|
|
|
|
s := fmt.Sprintln(v...)
|
2018-04-19 16:24:48 +08:00
|
|
|
|
l.errPrint(s)
|
|
|
|
|
panic(s)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (l *Logger) Panicfln(format string, v ...interface{}) {
|
2018-05-23 16:41:21 +08:00
|
|
|
|
s := fmt.Sprintf(format + ln, v...)
|
2018-04-19 16:24:48 +08:00
|
|
|
|
l.errPrint(s)
|
|
|
|
|
panic(s)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (l *Logger) Info(v ...interface{}) {
|
2018-08-30 13:54:45 +08:00
|
|
|
|
if l.checkLevel(LEVEL_INFO) {
|
2018-09-29 18:25:20 +08:00
|
|
|
|
l.stdPrint("[INFO] " + fmt.Sprintln(v...))
|
2018-08-30 13:00:49 +08:00
|
|
|
|
}
|
2018-04-19 16:24:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-30 13:54:45 +08:00
|
|
|
|
func (l *Logger) Infof(format string, v ...interface{}) {
|
|
|
|
|
if l.checkLevel(LEVEL_INFO) {
|
|
|
|
|
l.stdPrint("[INFO] " + fmt.Sprintf(format, v...))
|
2018-04-19 16:24:48 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-30 13:54:45 +08:00
|
|
|
|
func (l *Logger) Infofln(format string, v ...interface{}) {
|
|
|
|
|
if l.checkLevel(LEVEL_INFO) {
|
|
|
|
|
l.stdPrint("[INFO] " + fmt.Sprintf(format, v...) + ln)
|
2018-08-30 13:00:49 +08:00
|
|
|
|
}
|
2018-04-19 16:24:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-30 13:54:45 +08:00
|
|
|
|
func (l *Logger) Debug(v ...interface{}) {
|
|
|
|
|
if l.checkLevel(LEVEL_DEBU) {
|
2018-09-29 18:25:20 +08:00
|
|
|
|
l.stdPrint("[DEBU] " + fmt.Sprintln(v...))
|
2018-08-30 13:00:49 +08:00
|
|
|
|
}
|
2018-04-19 16:24:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-30 13:54:45 +08:00
|
|
|
|
func (l *Logger) Debugf(format string, v ...interface{}) {
|
|
|
|
|
if l.checkLevel(LEVEL_DEBU) {
|
|
|
|
|
l.stdPrint("[DEBU] " + fmt.Sprintf(format, v...))
|
2018-08-30 13:00:49 +08:00
|
|
|
|
}
|
2018-04-19 16:24:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-30 13:54:45 +08:00
|
|
|
|
func (l *Logger) Debugfln(format string, v ...interface{}) {
|
|
|
|
|
if l.checkLevel(LEVEL_DEBU) {
|
|
|
|
|
l.stdPrint("[DEBU] " + fmt.Sprintf(format, v...) + ln)
|
2018-08-30 13:00:49 +08:00
|
|
|
|
}
|
2018-04-19 16:24:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-30 13:54:45 +08:00
|
|
|
|
func (l *Logger) Notice(v ...interface{}) {
|
|
|
|
|
if l.checkLevel(LEVEL_NOTI) {
|
2018-09-29 18:25:20 +08:00
|
|
|
|
l.errPrint("[NOTI] " + fmt.Sprintln(v...))
|
2018-08-30 13:00:49 +08:00
|
|
|
|
}
|
2018-04-19 16:24:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-30 13:54:45 +08:00
|
|
|
|
func (l *Logger) Noticef(format string, v ...interface{}) {
|
|
|
|
|
if l.checkLevel(LEVEL_NOTI) {
|
|
|
|
|
l.errPrint("[NOTI] " + fmt.Sprintf(format, v...))
|
2018-04-19 16:24:48 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-30 13:54:45 +08:00
|
|
|
|
func (l *Logger) Noticefln(format string, v ...interface{}) {
|
|
|
|
|
if l.checkLevel(LEVEL_NOTI) {
|
|
|
|
|
l.errPrint("[NOTI] " + fmt.Sprintf(format, v...) + ln)
|
2018-08-30 13:00:49 +08:00
|
|
|
|
}
|
2018-04-19 16:24:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-30 13:54:45 +08:00
|
|
|
|
func (l *Logger) Warning(v ...interface{}) {
|
|
|
|
|
if l.checkLevel(LEVEL_WARN) {
|
2018-09-29 18:25:20 +08:00
|
|
|
|
l.errPrint("[WARN] " + fmt.Sprintln(v...))
|
2018-08-30 13:00:49 +08:00
|
|
|
|
}
|
2018-04-19 16:24:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-30 13:54:45 +08:00
|
|
|
|
func (l *Logger) Warningf(format string, v ...interface{}) {
|
|
|
|
|
if l.checkLevel(LEVEL_WARN) {
|
|
|
|
|
l.errPrint("[WARN] " + fmt.Sprintf(format, v...))
|
2018-08-30 13:00:49 +08:00
|
|
|
|
}
|
2018-04-19 16:24:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-30 13:54:45 +08:00
|
|
|
|
func (l *Logger) Warningfln(format string, v ...interface{}) {
|
|
|
|
|
if l.checkLevel(LEVEL_WARN) {
|
|
|
|
|
l.errPrint("[WARN] " + fmt.Sprintf(format, v...) + ln)
|
2018-08-30 13:00:49 +08:00
|
|
|
|
}
|
2018-04-19 16:24:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-30 13:54:45 +08:00
|
|
|
|
func (l *Logger) Error(v ...interface{}) {
|
|
|
|
|
if l.checkLevel(LEVEL_ERRO) {
|
2018-09-29 18:25:20 +08:00
|
|
|
|
l.errPrint("[ERRO] " + fmt.Sprintln(v...))
|
2018-08-30 13:00:49 +08:00
|
|
|
|
}
|
2018-04-19 16:24:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-30 13:54:45 +08:00
|
|
|
|
func (l *Logger) Errorf(format string, v ...interface{}) {
|
|
|
|
|
if l.checkLevel(LEVEL_ERRO) {
|
|
|
|
|
l.errPrint("[ERRO] " + fmt.Sprintf(format, v...))
|
2018-04-19 16:24:48 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-30 13:54:45 +08:00
|
|
|
|
func (l *Logger) Errorfln(format string, v ...interface{}) {
|
|
|
|
|
if l.checkLevel(LEVEL_ERRO) {
|
|
|
|
|
l.errPrint("[ERRO] " + fmt.Sprintf(format, v...) + ln)
|
2018-08-30 13:00:49 +08:00
|
|
|
|
}
|
2018-04-19 16:24:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-30 13:54:45 +08:00
|
|
|
|
func (l *Logger) Critical(v ...interface{}) {
|
|
|
|
|
if l.checkLevel(LEVEL_CRIT) {
|
2018-09-29 18:25:20 +08:00
|
|
|
|
l.errPrint("[CRIT] " + fmt.Sprintln(v...))
|
2018-08-30 13:00:49 +08:00
|
|
|
|
}
|
2018-04-19 16:24:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-30 13:54:45 +08:00
|
|
|
|
func (l *Logger) Criticalf(format string, v ...interface{}) {
|
|
|
|
|
if l.checkLevel(LEVEL_CRIT) {
|
|
|
|
|
l.errPrint("[CRIT] " + fmt.Sprintf(format, v...))
|
2018-08-30 13:00:49 +08:00
|
|
|
|
}
|
2018-04-19 16:24:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (l *Logger) Criticalfln(format string, v ...interface{}) {
|
2018-08-30 13:54:45 +08:00
|
|
|
|
if l.checkLevel(LEVEL_CRIT) {
|
2018-08-30 13:00:49 +08:00
|
|
|
|
l.errPrint("[CRIT] " + fmt.Sprintf(format, v...) + ln)
|
|
|
|
|
}
|
2018-08-30 13:54:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 判断给定level是否满足
|
|
|
|
|
func (l *Logger) checkLevel(level int) bool {
|
|
|
|
|
return l.level.Val() & level > 0
|
2018-04-19 16:24:48 +08:00
|
|
|
|
}
|