2019-02-02 16:18:25 +08:00
|
|
|
|
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
2018-08-28 17:06:49 +08:00
|
|
|
|
//
|
|
|
|
|
// 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,
|
2019-02-02 16:18:25 +08:00
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2018-08-28 17:06:49 +08:00
|
|
|
|
|
|
|
|
|
package glog
|
|
|
|
|
|
2018-11-06 18:53:25 +08:00
|
|
|
|
import (
|
2019-02-02 16:18:25 +08:00
|
|
|
|
"github.com/gogf/gf/g/os/gfile"
|
2018-11-06 18:53:25 +08:00
|
|
|
|
"io"
|
|
|
|
|
)
|
|
|
|
|
|
2019-02-20 11:16:10 +08:00
|
|
|
|
// To is a chaining function,
|
2019-03-21 00:14:23 +08:00
|
|
|
|
// which redirects current logging content output to the specified <writer>.
|
2019-02-20 11:16:10 +08:00
|
|
|
|
//
|
2018-11-06 18:53:25 +08:00
|
|
|
|
// 链式操作,设置下一次写入日志内容的Writer
|
|
|
|
|
func (l *Logger) To(writer io.Writer) *Logger {
|
|
|
|
|
logger := (*Logger)(nil)
|
|
|
|
|
if l.pr == nil {
|
|
|
|
|
logger = l.Clone()
|
|
|
|
|
} else {
|
|
|
|
|
logger = l
|
|
|
|
|
}
|
|
|
|
|
logger.SetWriter(writer)
|
|
|
|
|
return logger
|
|
|
|
|
}
|
2018-08-28 17:06:49 +08:00
|
|
|
|
|
2019-03-19 13:58:18 +08:00
|
|
|
|
// Path is a chaining function,
|
|
|
|
|
// which sets the directory path to <path> for current logging content output.
|
|
|
|
|
//
|
|
|
|
|
// 链式操作,设置下一次输出的日志路径。
|
|
|
|
|
func (l *Logger) Path(path string) *Logger {
|
|
|
|
|
logger := (*Logger)(nil)
|
|
|
|
|
if l.pr == nil {
|
|
|
|
|
logger = l.Clone()
|
|
|
|
|
} else {
|
|
|
|
|
logger = l
|
|
|
|
|
}
|
|
|
|
|
if path != "" {
|
|
|
|
|
logger.SetPath(path)
|
|
|
|
|
}
|
|
|
|
|
return logger
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-20 11:16:10 +08:00
|
|
|
|
// Cat is a chaining function,
|
|
|
|
|
// which sets the category to <category> for current logging content output.
|
|
|
|
|
//
|
2018-08-28 17:06:49 +08:00
|
|
|
|
// 链式操作,设置下一次输出的日志分类(可以按照文件目录层级设置),在当前logpath或者当前工作目录下创建category目录,
|
|
|
|
|
// 这是一个链式操作,可以设置多个分类,将会创建层级的日志分类目录。
|
|
|
|
|
func (l *Logger) Cat(category string) *Logger {
|
|
|
|
|
logger := (*Logger)(nil)
|
|
|
|
|
if l.pr == nil {
|
|
|
|
|
logger = l.Clone()
|
|
|
|
|
} else {
|
|
|
|
|
logger = l
|
|
|
|
|
}
|
|
|
|
|
path := l.path.Val()
|
2018-10-11 09:56:01 +08:00
|
|
|
|
if path != "" {
|
|
|
|
|
logger.SetPath(path + gfile.Separator + category)
|
2018-08-28 17:06:49 +08:00
|
|
|
|
}
|
|
|
|
|
return logger
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-20 11:16:10 +08:00
|
|
|
|
// File is a chaining function,
|
|
|
|
|
// which sets file name <pattern> for the current logging content output.
|
|
|
|
|
//
|
2018-10-09 21:41:07 +08:00
|
|
|
|
// 日志文件格式
|
|
|
|
|
func (l *Logger) File(file string) *Logger {
|
|
|
|
|
logger := (*Logger)(nil)
|
|
|
|
|
if l.pr == nil {
|
|
|
|
|
logger = l.Clone()
|
|
|
|
|
} else {
|
|
|
|
|
logger = l
|
|
|
|
|
}
|
|
|
|
|
logger.SetFile(file)
|
|
|
|
|
return logger
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-20 11:16:10 +08:00
|
|
|
|
// Level is a chaining function,
|
|
|
|
|
// which sets logging level for the current logging content output.
|
|
|
|
|
//
|
2018-08-30 13:54:45 +08:00
|
|
|
|
// 设置日志打印等级
|
|
|
|
|
func (l *Logger) Level(level int) *Logger {
|
|
|
|
|
logger := (*Logger)(nil)
|
|
|
|
|
if l.pr == nil {
|
|
|
|
|
logger = l.Clone()
|
|
|
|
|
} else {
|
|
|
|
|
logger = l
|
|
|
|
|
}
|
|
|
|
|
logger.SetLevel(level)
|
|
|
|
|
return logger
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-20 11:16:10 +08:00
|
|
|
|
// Backtrace is a chaining function,
|
|
|
|
|
// which sets backtrace options for the current logging content output .
|
|
|
|
|
//
|
2018-08-28 17:06:49 +08:00
|
|
|
|
// 设置文件调用回溯信息
|
2018-10-10 18:39:57 +08:00
|
|
|
|
func (l *Logger) Backtrace(enabled bool, skip...int) *Logger {
|
2018-08-28 17:06:49 +08:00
|
|
|
|
logger := (*Logger)(nil)
|
|
|
|
|
if l.pr == nil {
|
|
|
|
|
logger = l.Clone()
|
|
|
|
|
} else {
|
|
|
|
|
logger = l
|
|
|
|
|
}
|
|
|
|
|
logger.SetBacktrace(enabled)
|
2018-10-10 18:39:57 +08:00
|
|
|
|
if len(skip) > 0 {
|
|
|
|
|
logger.SetBacktraceSkip(skip[0])
|
|
|
|
|
}
|
2018-08-28 17:06:49 +08:00
|
|
|
|
return logger
|
2018-08-28 19:38:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-20 11:16:10 +08:00
|
|
|
|
// StdPrint is a chaining function,
|
|
|
|
|
// which enables/disables stdout for the current logging content output.
|
|
|
|
|
//
|
2018-08-28 19:38:05 +08:00
|
|
|
|
// 是否允许在设置输出文件时同时也输出到终端
|
|
|
|
|
func (l *Logger) StdPrint(enabled bool) *Logger {
|
|
|
|
|
logger := (*Logger)(nil)
|
|
|
|
|
if l.pr == nil {
|
|
|
|
|
logger = l.Clone()
|
|
|
|
|
} else {
|
|
|
|
|
logger = l
|
|
|
|
|
}
|
|
|
|
|
logger.SetStdPrint(enabled)
|
|
|
|
|
return logger
|
2018-10-18 13:43:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-20 11:16:10 +08:00
|
|
|
|
// Header is a chaining function,
|
|
|
|
|
// which enables/disables log header for the current logging content output.
|
|
|
|
|
//
|
2018-10-18 13:43:00 +08:00
|
|
|
|
// 是否打印每行日志头信息(默认开启)
|
|
|
|
|
func (l *Logger) Header(enabled bool) *Logger {
|
|
|
|
|
logger := (*Logger)(nil)
|
|
|
|
|
if l.pr == nil {
|
|
|
|
|
logger = l.Clone()
|
|
|
|
|
} else {
|
|
|
|
|
logger = l
|
|
|
|
|
}
|
|
|
|
|
logger.printHeader.Set(enabled)
|
|
|
|
|
return logger
|
2018-08-28 17:06:49 +08:00
|
|
|
|
}
|