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>.
|
2018-11-06 18:53:25 +08:00
|
|
|
func (l *Logger) To(writer io.Writer) *Logger {
|
|
|
|
logger := (*Logger)(nil)
|
2019-05-21 21:52:17 +08:00
|
|
|
if l.parent == nil {
|
2018-11-06 18:53:25 +08:00
|
|
|
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)
|
2019-05-21 21:52:17 +08:00
|
|
|
if l.parent == nil {
|
2019-03-19 13:58:18 +08:00
|
|
|
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.
|
2019-04-17 23:50:37 +08:00
|
|
|
// Param <category> can be hierarchical, eg: module/user.
|
2018-08-28 17:06:49 +08:00
|
|
|
func (l *Logger) Cat(category string) *Logger {
|
|
|
|
logger := (*Logger)(nil)
|
2019-05-21 21:52:17 +08:00
|
|
|
if l.parent == nil {
|
2018-08-28 17:06:49 +08:00
|
|
|
logger = l.Clone()
|
|
|
|
} else {
|
|
|
|
logger = l
|
|
|
|
}
|
2019-05-21 21:52:17 +08:00
|
|
|
if logger.path != "" {
|
|
|
|
logger.SetPath(logger.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)
|
2019-05-21 21:52:17 +08:00
|
|
|
if l.parent == nil {
|
2018-10-09 21:41:07 +08:00
|
|
|
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)
|
2019-05-21 21:52:17 +08:00
|
|
|
if l.parent == nil {
|
2018-08-30 13:54:45 +08:00
|
|
|
logger = l.Clone()
|
|
|
|
} else {
|
|
|
|
logger = l
|
|
|
|
}
|
|
|
|
logger.SetLevel(level)
|
|
|
|
return logger
|
|
|
|
}
|
|
|
|
|
2019-05-23 19:14:16 +08:00
|
|
|
// Skip is a chaining function,
|
|
|
|
// which sets backtrace skip for the current logging content output.
|
|
|
|
// It also affects the caller file path checks when line number printing enabled.
|
|
|
|
func (l *Logger) Skip(skip int) *Logger {
|
|
|
|
logger := (*Logger)(nil)
|
|
|
|
if l.parent == nil {
|
|
|
|
logger = l.Clone()
|
|
|
|
} else {
|
|
|
|
logger = l
|
|
|
|
}
|
|
|
|
logger.SetBacktraceSkip(skip)
|
|
|
|
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-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)
|
2019-05-21 21:52:17 +08:00
|
|
|
if l.parent == nil {
|
2018-08-28 17:06:49 +08:00
|
|
|
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-05-21 21:52:17 +08:00
|
|
|
// Stdout is a chaining function,
|
2019-02-20 11:16:10 +08:00
|
|
|
// which enables/disables stdout for the current logging content output.
|
2019-05-22 21:43:56 +08:00
|
|
|
// It's enabled in default.
|
|
|
|
func (l *Logger) Stdout(enabled...bool) *Logger {
|
2019-05-21 21:52:17 +08:00
|
|
|
logger := (*Logger)(nil)
|
|
|
|
if l.parent == nil {
|
|
|
|
logger = l.Clone()
|
|
|
|
} else {
|
|
|
|
logger = l
|
|
|
|
}
|
2019-06-01 19:34:03 +08:00
|
|
|
// stdout printing is enabled if <enabled> is not passed.
|
|
|
|
if len(enabled) > 0 && !enabled[0] {
|
2019-05-22 21:43:56 +08:00
|
|
|
logger.stdoutPrint = false
|
2019-06-01 19:34:03 +08:00
|
|
|
} else {
|
|
|
|
logger.stdoutPrint = true
|
2019-05-22 21:43:56 +08:00
|
|
|
}
|
2019-05-21 21:52:17 +08:00
|
|
|
return logger
|
|
|
|
}
|
|
|
|
|
|
|
|
// See Stdout.
|
|
|
|
// Deprecated.
|
2019-05-22 21:43:56 +08:00
|
|
|
func (l *Logger) StdPrint(enabled...bool) *Logger {
|
|
|
|
return l.Stdout(enabled...)
|
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.
|
2019-05-22 21:43:56 +08:00
|
|
|
// It's enabled in default.
|
|
|
|
func (l *Logger) Header(enabled...bool) *Logger {
|
2018-10-18 13:43:00 +08:00
|
|
|
logger := (*Logger)(nil)
|
2019-05-21 21:52:17 +08:00
|
|
|
if l.parent == nil {
|
2018-10-18 13:43:00 +08:00
|
|
|
logger = l.Clone()
|
|
|
|
} else {
|
|
|
|
logger = l
|
|
|
|
}
|
2019-06-01 19:34:03 +08:00
|
|
|
// header is enabled if <enabled> is not passed.
|
|
|
|
if len(enabled) > 0 && !enabled[0] {
|
2019-05-22 21:43:56 +08:00
|
|
|
logger.SetHeaderPrint(false)
|
2019-06-01 19:34:03 +08:00
|
|
|
} else {
|
|
|
|
logger.SetHeaderPrint(true)
|
2019-05-22 21:43:56 +08:00
|
|
|
}
|
2018-10-18 13:43:00 +08:00
|
|
|
return logger
|
2019-05-21 23:57:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Line is a chaining function,
|
2019-05-22 09:19:21 +08:00
|
|
|
// which enables/disables printing its caller file path along with its line number.
|
2019-06-11 20:57:43 +08:00
|
|
|
// The parameter <long> specified whether print the long absolute file path, eg: /a/b/c/d.go:23,
|
2019-05-22 09:19:21 +08:00
|
|
|
// or else short one: d.go:23.
|
2019-05-21 23:57:03 +08:00
|
|
|
func (l *Logger) Line(long...bool) *Logger {
|
|
|
|
logger := (*Logger)(nil)
|
|
|
|
if l.parent == nil {
|
|
|
|
logger = l.Clone()
|
|
|
|
} else {
|
|
|
|
logger = l
|
|
|
|
}
|
|
|
|
if len(long) > 0 && long[0] {
|
|
|
|
logger.flags |= F_FILE_LONG
|
|
|
|
} else {
|
|
|
|
logger.flags |= F_FILE_SHORT
|
|
|
|
}
|
|
|
|
return logger
|
2019-06-01 19:34:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Async is a chaining function,
|
|
|
|
// which enables/disables async logging output feature.
|
|
|
|
func (l *Logger) Async(enabled...bool) *Logger {
|
|
|
|
logger := (*Logger)(nil)
|
|
|
|
if l.parent == nil {
|
|
|
|
logger = l.Clone()
|
|
|
|
} else {
|
|
|
|
logger = l
|
|
|
|
}
|
|
|
|
// async feature is enabled if <enabled> is not passed.
|
|
|
|
if len(enabled) > 0 && !enabled[0] {
|
|
|
|
logger.SetAsync(false)
|
|
|
|
} else {
|
|
|
|
logger.SetAsync(true)
|
|
|
|
}
|
|
|
|
return logger
|
2018-08-28 17:06:49 +08:00
|
|
|
}
|