comments for glog update

This commit is contained in:
John
2019-02-20 11:16:10 +08:00
parent 8925460718
commit 1fb5a8cd6f
3 changed files with 150 additions and 26 deletions

View File

@ -7,8 +7,7 @@
// Package glog implements powerful and easy-to-use levelled logging functionality.
//
// 日志模块,
// 直接文件/输出操作,没有异步逻辑,没有使用缓存或者通道
// 日志模块, 直接文件/输出操作,没有异步逻辑,没有使用缓存或者通道
package glog
import (
@ -28,10 +27,10 @@ const (
)
var (
// glog默认的日志等级影响全局
// default level for log
defaultLevel = gtype.NewInt(LEVEL_ALL)
// 默认的日志对象
// default logger object, for package method usage
logger = New()
)
@ -39,101 +38,157 @@ func init() {
SetDebug(cmdenv.Get("gf.glog.debug", true).Bool())
}
// 日志日志目录绝对路径
// SetPath sets the directory path for file logging.
//
// 日志日志目录绝对路径.
func SetPath(path string) {
logger.SetPath(path)
}
// 日志文件名称
func SetFile(file string) {
logger.SetFile(file)
// SetFile sets the file name <pattern> for file logging.
// Datetime pattern can be used in <pattern>, eg: access-{Ymd}.log.
// The default file name pattern is: Y-m-d.log, eg: 2018-01-01.log
//
// 日志文件名称.
func SetFile(pattern string) {
logger.SetFile(pattern)
}
// 设置全局的日志记录等级
// SetLevel sets the default logging level.
//
// 设置全局的日志记录等级.
func SetLevel(level int) {
logger.SetLevel(level)
defaultLevel.Set(level)
}
// 可自定义IO接口IO可以是文件输出、标准输出、网络输出
// SetWriter sets the customed logging <writer> for logging.
// The <writer> object should implements the io.Writer interface.
// Developer can use customed logging <writer> to redirect logging output to another service,
// eg: kafka, mysql, mongodb, etc.
//
// 可自定义IO接口IO可以是文件输出、标准输出、网络输出.
func SetWriter(writer io.Writer) {
logger.SetWriter(writer)
}
// 返回自定义的IO默认为nil
// GetWriter returns the customed writer object, which implements the io.Writer interface.
// It returns nil if no customed writer set.
//
// 返回自定义的IO默认为nil.
func GetWriter() io.Writer {
return logger.GetWriter()
}
// 获取全局的日志记录等级
// GetLevel returns the default logging level value.
//
// 获取全局的日志记录等级.
func GetLevel() int {
return defaultLevel.Val()
}
// 设置是否允许输出DEBUG信息
// SetDebug enables/disables the debug level for default logger.
// The debug level is enbaled in default.
//
// 设置是否允许输出DEBUG信息.
func SetDebug(debug bool) {
logger.SetDebug(debug)
}
// SetStdPrint sets whether ouptput the logging contents to stdout, which is false indefault.
//
// 设置写日志的同时开启or关闭控制台打印默认是关闭的
func SetStdPrint(open bool) {
logger.SetStdPrint(open)
}
// GetPath returns the logging directory path for file logging.
// It returns empty string if no directory path set.
//
// 获取日志目录绝对路径
func GetPath() string {
return logger.GetPath()
}
// PrintBacktrace prints the caller backtrace,
// the optional parameter <skip> specify the skipped backtraces offset from the end point.
//
// 打印文件调用回溯信息
func PrintBacktrace(skip...int) {
logger.PrintBacktrace(skip...)
}
// 获取文件调用回溯信息
// GetBacktrace returns the caller backtrace content,
// the optional parameter <skip> specify the skipped backtraces offset from the end point.
//
// 获取文件调用回溯信息.
func GetBacktrace(skip...int) string {
return logger.GetBacktrace(skip...)
}
// SetBacktrace enables/disables the backtrace feature in failure logging outputs.
//
// 是否关闭全局的backtrace信息
func SetBacktrace(enabled bool) {
logger.SetBacktrace(enabled)
}
// To is a chaining function,
// which redirects current logging content output to the sepecified <writer>.
//
// 链式操作设置下一次写入日志内容的Writer
func To(writer io.Writer) *Logger {
return logger.To(writer)
}
// 设置下一次输出的分类,支持多级分类设置
// Cat is a chaining function,
// which sets the category to <category> for current logging content output.
//
// 设置下一次输出的分类,支持多级分类设置.
func Cat(category string) *Logger {
return logger.Cat(category)
}
// File is a chaining function,
// which sets file name <pattern> for the current logging content output.
//
// 设置日志输出文件名称格式
func File(file string) *Logger {
return logger.File(file)
func File(pattern string) *Logger {
return logger.File(pattern)
}
// 设置日志打印等级
// Level is a chaining function,
// which sets logging level for the current logging content output.
//
// 设置日志打印等级.
func Level(level int) *Logger {
return logger.Level(level)
}
// 设置文件调用回溯信息
// Backtrace is a chaining function,
// which sets backtrace options for the current logging content output .
//
// 设置文件调用回溯信息.
func Backtrace(enabled bool, skip...int) *Logger {
return logger.Backtrace(enabled, skip...)
}
// StdPrint is a chaining function,
// which enables/disables stdout for the current logging content output.
//
// 是否允许在设置输出文件时同时也输出到终端
func StdPrint(enabled bool) *Logger {
return logger.StdPrint(enabled)
}
// Header is a chaining function,
// which enables/disables log header for the current logging content output.
//
// 是否打印每行日志头信息(默认开启)
func Header(enabled bool) *Logger {
return logger.Header(enabled)
}
func Print(v ...interface{}) {
logger.Print(v ...)
}
@ -150,14 +205,17 @@ func Printfln(format string, v ...interface{}) {
logger.Printfln(format, v ...)
}
// Fatal prints the logging content with [FATA] header and newline, then exit the current process.
func Fatal(v ...interface{}) {
logger.Fatal(v ...)
}
// Fatalf prints the logging content with [FATA] header and custom format, then exit the current process.
func Fatalf(format string, v ...interface{}) {
logger.Fatalf(format, v ...)
}
// Fatalf prints the logging content with [FATA] header, custom format and newline, then exit the current process.
func Fatalfln(format string, v ...interface{}) {
logger.Fatalfln(format, v ...)
}

View File

@ -58,6 +58,8 @@ func init() {
}
}
// New creates a custom logger.
//
// 新建自定义的日志操作对象
func New() *Logger {
return &Logger {
@ -72,7 +74,9 @@ func New() *Logger {
}
}
// Logger深拷贝
// Clone returns a new logger, which is the clone the current logger.
//
// Logger拷贝.
func (l *Logger) Clone() *Logger {
return &Logger {
pr : l,
@ -87,16 +91,23 @@ func (l *Logger) Clone() *Logger {
}
}
// SetLevel sets the logging level.
//
// 设置日志记录等级
func (l *Logger) SetLevel(level int) {
l.level.Set(level)
}
// GetLevel returns the logging level value.
//
// 获取日志记录等级
func (l *Logger) GetLevel() int {
return l.level.Val()
}
// SetDebug enables/disables the debug level for logger.
// The debug level is enbaled in default.
//
// 快捷方法打开或关闭DEBU日志信息
func (l *Logger) SetDebug(debug bool) {
if debug {
@ -106,6 +117,7 @@ func (l *Logger) SetDebug(debug bool) {
}
}
// SetBacktrace enables/disables the backtrace feature in failure logging outputs.
func (l *Logger) SetBacktrace(enabled bool) {
if enabled {
l.btStatus.Set(1)
@ -115,11 +127,16 @@ func (l *Logger) SetBacktrace(enabled bool) {
}
// 设置BacktraceSkip
// SetBacktraceSkip sets the backtrace offset from the end point.
func (l *Logger) SetBacktraceSkip(skip int) {
l.btSkip.Set(skip)
}
// SetWriter sets the customed logging <writer> for logging.
// The <writer> object should implements the io.Writer interface.
// Developer can use customed logging <writer> to redirect logging output to another service,
// eg: kafka, mysql, mongodb, etc.
//
// 可自定义IO接口IO可以是文件输出、标准输出、网络输出
func (l *Logger) SetWriter(writer io.Writer) {
l.mu.Lock()
@ -127,6 +144,9 @@ func (l *Logger) SetWriter(writer io.Writer) {
l.mu.Unlock()
}
// GetWriter returns the customed writer object, which implements the io.Writer interface.
// It returns nil if no customed writer set.
//
// 返回自定义的IO默认为nil
func (l *Logger) GetWriter() io.Writer {
l.mu.RLock()
@ -135,7 +155,10 @@ func (l *Logger) GetWriter() io.Writer {
return r
}
// 获取默认的文件IO
// getFilePointer returns the file pinter for file logging.
// It returns nil if file logging disabled, or file open fails.
//
// 获取默认的文件IO.
func (l *Logger) getFilePointer() *gfpool.File {
if path := l.path.Val(); path != "" {
// 文件名称中使用"{}"包含的内容使用gtime格式化
@ -159,7 +182,9 @@ func (l *Logger) getFilePointer() *gfpool.File {
return nil
}
// 设置日志文件的存储目录路径
// SetPath sets the directory path for file logging.
//
// 设置日志文件的存储目录路径.
func (l *Logger) SetPath(path string) error {
// path必须有值
if path == "" {
@ -176,16 +201,25 @@ func (l *Logger) SetPath(path string) error {
return nil
}
// GetPath returns the logging directory path for file logging.
// It returns empty string if no directory path set.
//
// 获取设置的日志目录路径
func (l *Logger) GetPath() string {
return l.path.Val()
}
// 日志文件名称
func (l *Logger) SetFile(file string) {
l.file.Set(file)
// SetFile sets the file name <pattern> for file logging.
// Datetime pattern can be used in <pattern>, eg: access-{Ymd}.log.
// The default file name pattern is: Y-m-d.log, eg: 2018-01-01.log
//
// 设置日志文件名称格式.
func (l *Logger) SetFile(pattern string) {
l.file.Set(pattern)
}
// SetStdPrint sets whether ouptput the logging contents to stdout, which is false indefault.
//
// 设置写日志时开启or关闭控制台打印默认是关闭的
func (l *Logger) SetStdPrint(enabled bool) {
l.alsoStdPrint.Set(enabled)
@ -263,11 +297,17 @@ func (l *Logger) appendBacktrace(s string, skip...int) string {
return s
}
// PrintBacktrace prints the caller backtrace,
// the optional parameter <skip> specify the skipped backtraces offset from the end point.
//
// 直接打印回溯信息参数skip表示调用端往上多少级开始回溯
func (l *Logger) PrintBacktrace(skip...int) {
l.Println(l.appendBacktrace("", skip...))
}
// GetBacktrace returns the caller backtrace content,
// the optional parameter <skip> specify the skipped backtraces offset from the end point.
//
// 获取文件调用回溯字符串参数skip表示调用端往上多少级开始回溯
func (l *Logger) GetBacktrace(skip...int) string {
customSkip := 0
@ -322,16 +362,19 @@ func (l *Logger) Printfln(format string, v ...interface{}) {
l.stdPrint(fmt.Sprintf(format + ln, v...))
}
// Fatal prints the logging content with [FATA] header and newline, then exit the current process.
func (l *Logger) Fatal(v ...interface{}) {
l.errPrint("[FATA] " + fmt.Sprintln(v...))
os.Exit(1)
}
// Fatalf prints the logging content with [FATA] header and custom format, then exit the current process.
func (l *Logger) Fatalf(format string, v ...interface{}) {
l.errPrint("[FATA] " + fmt.Sprintf(format, v...))
os.Exit(1)
}
// Fatalf prints the logging content with [FATA] header, custom format and newline, then exit the current process.
func (l *Logger) Fatalfln(format string, v ...interface{}) {
l.errPrint("[FATA] " + fmt.Sprintf(format + ln, v...))
os.Exit(1)
@ -463,6 +506,8 @@ func (l *Logger) Criticalfln(format string, v ...interface{}) {
}
}
// checkLevel checks whether the given <level> could be output.
//
// 判断给定level是否满足
func (l *Logger) checkLevel(level int) bool {
return l.level.Val() & level > 0

View File

@ -11,6 +11,9 @@ import (
"io"
)
// To is a chaining function,
// which redirects current logging content output to the sepecified <writer>.
//
// 链式操作设置下一次写入日志内容的Writer
func (l *Logger) To(writer io.Writer) *Logger {
logger := (*Logger)(nil)
@ -23,6 +26,9 @@ func (l *Logger) To(writer io.Writer) *Logger {
return logger
}
// Cat is a chaining function,
// which sets the category to <category> for current logging content output.
//
// 链式操作,设置下一次输出的日志分类(可以按照文件目录层级设置)在当前logpath或者当前工作目录下创建category目录
// 这是一个链式操作,可以设置多个分类,将会创建层级的日志分类目录。
func (l *Logger) Cat(category string) *Logger {
@ -39,6 +45,9 @@ func (l *Logger) Cat(category string) *Logger {
return logger
}
// File is a chaining function,
// which sets file name <pattern> for the current logging content output.
//
// 日志文件格式
func (l *Logger) File(file string) *Logger {
logger := (*Logger)(nil)
@ -51,6 +60,9 @@ func (l *Logger) File(file string) *Logger {
return logger
}
// Level is a chaining function,
// which sets logging level for the current logging content output.
//
// 设置日志打印等级
func (l *Logger) Level(level int) *Logger {
logger := (*Logger)(nil)
@ -63,6 +75,9 @@ func (l *Logger) Level(level int) *Logger {
return logger
}
// Backtrace is a chaining function,
// which sets backtrace options for the current logging content output .
//
// 设置文件调用回溯信息
func (l *Logger) Backtrace(enabled bool, skip...int) *Logger {
logger := (*Logger)(nil)
@ -78,6 +93,9 @@ func (l *Logger) Backtrace(enabled bool, skip...int) *Logger {
return logger
}
// StdPrint is a chaining function,
// which enables/disables stdout for the current logging content output.
//
// 是否允许在设置输出文件时同时也输出到终端
func (l *Logger) StdPrint(enabled bool) *Logger {
logger := (*Logger)(nil)
@ -90,6 +108,9 @@ func (l *Logger) StdPrint(enabled bool) *Logger {
return logger
}
// Header is a chaining function,
// which enables/disables log header for the current logging content output.
//
// 是否打印每行日志头信息(默认开启)
func (l *Logger) Header(enabled bool) *Logger {
logger := (*Logger)(nil)