gf/g/os/glog/glog_logger_chaining.go

193 lines
4.8 KiB
Go
Raw Normal View History

// 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,
// You can obtain one at https://github.com/gogf/gf.
2018-08-28 17:06:49 +08:00
package glog
import (
"github.com/gogf/gf/g/os/gfile"
"io"
)
2019-02-20 11:16:10 +08:00
// To is a chaining function,
// which redirects current logging content output to the specified <writer>.
func (l *Logger) To(writer io.Writer) *Logger {
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,
// which sets the directory path to <path> for current logging content output.
func (l *Logger) Path(path string) *Logger {
logger := (*Logger)(nil)
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.
// 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)
if l.parent == nil {
2018-08-28 17:06:49 +08:00
logger = l.Clone()
} else {
logger = l
}
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.
func (l *Logger) File(file string) *Logger {
logger := (*Logger)(nil)
if l.parent == 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.
func (l *Logger) Level(level int) *Logger {
logger := (*Logger)(nil)
if l.parent == nil {
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 .
func (l *Logger) Backtrace(enabled bool, skip...int) *Logger {
2018-08-28 17:06:49 +08:00
logger := (*Logger)(nil)
if l.parent == nil {
2018-08-28 17:06:49 +08:00
logger = l.Clone()
} else {
logger = l
}
logger.SetBacktrace(enabled)
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
}
// 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.
func (l *Logger) Stdout(enabled...bool) *Logger {
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] {
logger.stdoutPrint = false
2019-06-01 19:34:03 +08:00
} else {
logger.stdoutPrint = true
}
return logger
}
// See Stdout.
// Deprecated.
func (l *Logger) StdPrint(enabled...bool) *Logger {
return l.Stdout(enabled...)
}
2019-02-20 11:16:10 +08:00
// Header is a chaining function,
// which enables/disables log header for the current logging content output.
// It's enabled in default.
func (l *Logger) Header(enabled...bool) *Logger {
logger := (*Logger)(nil)
if l.parent == nil {
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] {
logger.SetHeaderPrint(false)
2019-06-01 19:34:03 +08:00
} 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.
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
}