gf/os/glog/glog_logger_chaining.go

224 lines
5.2 KiB
Go
Raw Normal View History

2021-01-17 21:46:25 +08:00
// Copyright GoFrame Author(https://goframe.org). 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,
// You can obtain one at https://github.com/gogf/gf.
2018-08-28 17:06:49 +08:00
package glog
import (
2019-06-19 09:06:52 +08:00
"io"
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/os/gfile"
)
2019-06-19 09:06:52 +08:00
// To is a chaining function,
2021-08-08 13:56:26 +08:00
// which redirects current logging content output to the specified `writer`.
func (l *Logger) To(writer io.Writer) *Logger {
2019-06-19 09:06:52 +08:00
logger := (*Logger)(nil)
if l.parent == 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,
2021-08-08 13:56:26 +08:00
// which sets the directory path to `path` for current logging content output.
2020-02-26 23:26:24 +08:00
//
2021-08-08 13:56:26 +08:00
// Note that the parameter `path` is a directory path, not a file path.
2019-03-19 13:58:18 +08:00
func (l *Logger) Path(path string) *Logger {
2019-06-19 09:06:52 +08:00
logger := (*Logger)(nil)
if l.parent == nil {
logger = l.Clone()
} else {
logger = l
}
if path != "" {
if err := logger.SetPath(path); err != nil {
2021-09-27 21:27:24 +08:00
panic(err)
}
2019-06-19 09:06:52 +08:00
}
return logger
2019-03-19 13:58:18 +08:00
}
2019-06-19 09:06:52 +08:00
// Cat is a chaining function,
2021-08-08 13:56:26 +08:00
// which sets the category to `category` for current logging content output.
// Param `category` can be hierarchical, eg: module/user.
2018-08-28 17:06:49 +08:00
func (l *Logger) Cat(category string) *Logger {
2019-06-19 09:06:52 +08:00
logger := (*Logger)(nil)
if l.parent == nil {
logger = l.Clone()
} else {
logger = l
}
2019-10-26 10:58:07 +08:00
if logger.config.Path != "" {
if err := logger.SetPath(gfile.Join(logger.config.Path, category)); err != nil {
2021-09-27 21:27:24 +08:00
panic(err)
}
2019-06-19 09:06:52 +08:00
}
return logger
2018-08-28 17:06:49 +08:00
}
2019-06-19 09:06:52 +08:00
// File is a chaining function,
2021-08-08 13:56:26 +08:00
// which sets file name `pattern` for the current logging content output.
func (l *Logger) File(file string) *Logger {
2019-06-19 09:06:52 +08:00
logger := (*Logger)(nil)
if l.parent == nil {
logger = l.Clone()
} else {
logger = l
}
logger.SetFile(file)
return logger
}
2019-06-19 09:06:52 +08:00
// Level is a chaining function,
2019-02-20 11:16:10 +08:00
// which sets logging level for the current logging content output.
func (l *Logger) Level(level int) *Logger {
2019-06-19 09:06:52 +08:00
logger := (*Logger)(nil)
if l.parent == nil {
logger = l.Clone()
} else {
logger = l
}
logger.SetLevel(level)
return logger
}
// LevelStr is a chaining function,
// which sets logging level for the current logging content output using level string.
func (l *Logger) LevelStr(levelStr string) *Logger {
logger := (*Logger)(nil)
if l.parent == nil {
logger = l.Clone()
} else {
logger = l
}
if err := logger.SetLevelStr(levelStr); err != nil {
2021-09-27 21:27:24 +08:00
panic(err)
}
return logger
}
2019-05-23 19:14:16 +08:00
// Skip is a chaining function,
// which sets stack skip for the current logging content output.
2019-05-23 19:14:16 +08:00
// 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.SetStackSkip(skip)
2019-05-23 19:14:16 +08:00
return logger
}
// Stack is a chaining function,
// which sets stack options for the current logging content output .
func (l *Logger) Stack(enabled bool, skip ...int) *Logger {
2019-06-19 09:06:52 +08:00
logger := (*Logger)(nil)
if l.parent == nil {
logger = l.Clone()
} else {
logger = l
}
logger.SetStack(enabled)
2019-06-19 09:06:52 +08:00
if len(skip) > 0 {
logger.SetStackSkip(skip[0])
2019-06-19 09:06:52 +08:00
}
return logger
2018-08-28 19:38:05 +08:00
}
// StackWithFilter is a chaining function,
// which sets stack filter for the current logging content output .
func (l *Logger) StackWithFilter(filter string) *Logger {
logger := (*Logger)(nil)
if l.parent == nil {
logger = l.Clone()
} else {
logger = l
}
logger.SetStack(true)
logger.SetStackFilter(filter)
return logger
}
// Stdout is a chaining function,
2019-02-20 11:16:10 +08:00
// which enables/disables stdout for the current logging content output.
// It's enabled in default.
2019-06-19 09:06:52 +08:00
func (l *Logger) Stdout(enabled ...bool) *Logger {
logger := (*Logger)(nil)
if l.parent == nil {
logger = l.Clone()
} else {
logger = l
}
2021-08-08 13:56:26 +08:00
// stdout printing is enabled if `enabled` is not passed.
2019-06-01 19:34:03 +08:00
if len(enabled) > 0 && !enabled[0] {
2019-10-26 10:58:07 +08:00
logger.config.StdoutPrint = false
2019-06-01 19:34:03 +08:00
} else {
2019-10-26 10:58:07 +08:00
logger.config.StdoutPrint = true
}
return logger
}
2019-06-19 09:06:52 +08:00
// Header is a chaining function,
2019-02-20 11:16:10 +08:00
// which enables/disables log header for the current logging content output.
// It's enabled in default.
2019-06-19 09:06:52 +08:00
func (l *Logger) Header(enabled ...bool) *Logger {
logger := (*Logger)(nil)
if l.parent == nil {
logger = l.Clone()
} else {
logger = l
}
2021-08-08 13:56:26 +08:00
// header is enabled if `enabled` is not passed.
2019-06-19 09:06:52 +08:00
if len(enabled) > 0 && !enabled[0] {
logger.SetHeaderPrint(false)
} else {
logger.SetHeaderPrint(true)
}
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.
2021-08-08 13:56:26 +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-06-19 09:06:52 +08:00
func (l *Logger) Line(long ...bool) *Logger {
2019-05-21 23:57:03 +08:00
logger := (*Logger)(nil)
if l.parent == nil {
logger = l.Clone()
} else {
logger = l
}
if len(long) > 0 && long[0] {
2019-10-26 10:58:07 +08:00
logger.config.Flags |= F_FILE_LONG
2019-05-21 23:57:03 +08:00
} else {
2019-10-26 10:58:07 +08:00
logger.config.Flags |= F_FILE_SHORT
2019-05-21 23:57:03 +08:00
}
return logger
2019-06-01 19:34:03 +08:00
}
// Async is a chaining function,
// which enables/disables async logging output feature.
2019-06-19 09:06:52 +08:00
func (l *Logger) Async(enabled ...bool) *Logger {
2019-06-01 19:34:03 +08:00
logger := (*Logger)(nil)
if l.parent == nil {
logger = l.Clone()
} else {
logger = l
}
2021-08-08 13:56:26 +08:00
// async feature is enabled if `enabled` is not passed.
2019-06-01 19:34:03 +08:00
if len(enabled) > 0 && !enabled[0] {
logger.SetAsync(false)
} else {
logger.SetAsync(true)
}
return logger
2019-06-19 09:06:52 +08:00
}