2019-02-02 16:18:25 +08:00
|
|
|
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
2017-12-29 16:03:30 +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.
|
2017-12-29 16:03:30 +08:00
|
|
|
|
2019-01-16 09:00:23 +08:00
|
|
|
// Package glog implements powerful and easy-to-use levelled logging functionality.
|
2017-11-23 10:21:28 +08:00
|
|
|
package glog
|
|
|
|
|
|
|
|
import (
|
2019-06-29 10:45:50 +08:00
|
|
|
"io"
|
|
|
|
|
2019-07-29 21:01:19 +08:00
|
|
|
"github.com/gogf/gf/internal/cmdenv"
|
|
|
|
"github.com/gogf/gf/os/grpool"
|
2017-11-23 10:21:28 +08:00
|
|
|
)
|
|
|
|
|
2018-08-30 13:00:49 +08:00
|
|
|
const (
|
2019-06-19 09:06:52 +08:00
|
|
|
LEVEL_ALL = LEVEL_DEBU | LEVEL_INFO | LEVEL_NOTI | LEVEL_WARN | LEVEL_ERRO | LEVEL_CRIT
|
|
|
|
LEVEL_DEV = LEVEL_ALL
|
|
|
|
LEVEL_PROD = LEVEL_WARN | LEVEL_ERRO | LEVEL_CRIT
|
|
|
|
LEVEL_DEBU = 1 << iota
|
|
|
|
LEVEL_INFO
|
|
|
|
LEVEL_NOTI
|
|
|
|
LEVEL_WARN
|
|
|
|
LEVEL_ERRO
|
|
|
|
LEVEL_CRIT
|
2018-08-30 13:00:49 +08:00
|
|
|
)
|
|
|
|
|
2018-08-24 23:41:58 +08:00
|
|
|
var (
|
2019-06-19 09:06:52 +08:00
|
|
|
// Default logger object, for package method usage
|
|
|
|
logger = New()
|
|
|
|
// Goroutine pool for async logging output.
|
2019-10-28 17:16:50 +08:00
|
|
|
// It uses only one asynchronize worker to ensure log sequence.
|
2019-06-01 19:34:03 +08:00
|
|
|
asyncPool = grpool.New(1)
|
2018-08-24 23:41:58 +08:00
|
|
|
)
|
2017-11-23 10:21:28 +08:00
|
|
|
|
2019-01-23 13:01:58 +08:00
|
|
|
func init() {
|
2019-06-19 09:06:52 +08:00
|
|
|
SetDebug(cmdenv.Get("gf.glog.debug", true).Bool())
|
2019-01-23 13:01:58 +08:00
|
|
|
}
|
|
|
|
|
2019-08-26 19:39:04 +08:00
|
|
|
// Default returns the default logger.
|
2019-10-26 10:58:07 +08:00
|
|
|
func DefaultLogger() *Logger {
|
2019-08-26 19:39:04 +08:00
|
|
|
return logger
|
|
|
|
}
|
|
|
|
|
2019-02-20 11:16:10 +08:00
|
|
|
// SetPath sets the directory path for file logging.
|
2019-06-18 08:37:21 +08:00
|
|
|
func SetPath(path string) error {
|
2019-06-19 09:06:52 +08:00
|
|
|
return logger.SetPath(path)
|
2017-11-23 10:21:28 +08:00
|
|
|
}
|
|
|
|
|
2019-05-22 09:19:21 +08:00
|
|
|
// GetPath returns the logging directory path for file logging.
|
|
|
|
// It returns empty string if no directory path set.
|
|
|
|
func GetPath() string {
|
|
|
|
return logger.GetPath()
|
|
|
|
}
|
|
|
|
|
2019-02-20 11:16:10 +08:00
|
|
|
// 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) {
|
2019-06-19 09:06:52 +08:00
|
|
|
logger.SetFile(pattern)
|
2018-10-09 21:41:07 +08:00
|
|
|
}
|
|
|
|
|
2019-02-20 11:16:10 +08:00
|
|
|
// SetLevel sets the default logging level.
|
2018-08-30 13:54:45 +08:00
|
|
|
func SetLevel(level int) {
|
2019-06-19 09:06:52 +08:00
|
|
|
logger.SetLevel(level)
|
2019-05-22 09:19:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetLevel returns the default logging level value.
|
|
|
|
func GetLevel() int {
|
|
|
|
return logger.GetLevel()
|
2018-08-30 13:54:45 +08:00
|
|
|
}
|
|
|
|
|
2019-04-17 23:50:37 +08:00
|
|
|
// SetWriter sets the customized logging <writer> for logging.
|
2019-02-20 11:16:10 +08:00
|
|
|
// The <writer> object should implements the io.Writer interface.
|
2019-06-19 09:06:52 +08:00
|
|
|
// Developer can use customized logging <writer> to redirect logging output to another service,
|
2019-02-20 11:16:10 +08:00
|
|
|
// eg: kafka, mysql, mongodb, etc.
|
2018-11-06 18:53:25 +08:00
|
|
|
func SetWriter(writer io.Writer) {
|
2019-06-19 09:06:52 +08:00
|
|
|
logger.SetWriter(writer)
|
2018-11-06 18:53:25 +08:00
|
|
|
}
|
|
|
|
|
2019-04-17 23:50:37 +08:00
|
|
|
// GetWriter returns the customized writer object, which implements the io.Writer interface.
|
|
|
|
// It returns nil if no customized writer set.
|
2018-11-06 18:53:25 +08:00
|
|
|
func GetWriter() io.Writer {
|
2019-06-19 09:06:52 +08:00
|
|
|
return logger.GetWriter()
|
2018-11-06 18:53:25 +08:00
|
|
|
}
|
|
|
|
|
2019-02-20 11:16:10 +08:00
|
|
|
// SetDebug enables/disables the debug level for default logger.
|
|
|
|
// The debug level is enbaled in default.
|
2017-11-23 10:21:28 +08:00
|
|
|
func SetDebug(debug bool) {
|
2019-06-19 09:06:52 +08:00
|
|
|
logger.SetDebug(debug)
|
2017-11-23 10:21:28 +08:00
|
|
|
}
|
|
|
|
|
2019-06-01 19:34:03 +08:00
|
|
|
// SetAsync enables/disables async logging output feature for default logger.
|
|
|
|
func SetAsync(enabled bool) {
|
|
|
|
logger.SetAsync(enabled)
|
|
|
|
}
|
|
|
|
|
2019-06-18 08:37:21 +08:00
|
|
|
// SetStdoutPrint sets whether ouptput the logging contents to stdout, which is true in default.
|
2019-05-21 21:52:17 +08:00
|
|
|
func SetStdoutPrint(enabled bool) {
|
2019-06-19 09:06:52 +08:00
|
|
|
logger.SetStdoutPrint(enabled)
|
2019-05-21 21:52:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetHeaderPrint sets whether output header of the logging contents, which is true in default.
|
|
|
|
func SetHeaderPrint(enabled bool) {
|
|
|
|
logger.SetHeaderPrint(enabled)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetPrefix sets prefix string for every logging content.
|
|
|
|
// Prefix is part of header, which means if header output is shut, no prefix will be output.
|
|
|
|
func SetPrefix(prefix string) {
|
|
|
|
logger.SetPrefix(prefix)
|
2018-08-30 13:54:45 +08:00
|
|
|
}
|
|
|
|
|
2019-05-22 09:19:21 +08:00
|
|
|
// SetFlags sets extra flags for logging output features.
|
|
|
|
func SetFlags(flags int) {
|
|
|
|
logger.SetFlags(flags)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFlags returns the flags of logger.
|
|
|
|
func GetFlags() int {
|
|
|
|
return logger.GetFlags()
|
2017-11-23 10:21:28 +08:00
|
|
|
}
|
|
|
|
|
2019-06-29 10:45:50 +08:00
|
|
|
// PrintStack prints the caller stack,
|
|
|
|
// the optional parameter <skip> specify the skipped stack offset from the end point.
|
|
|
|
func PrintStack(skip ...int) {
|
|
|
|
logger.PrintStack(skip...)
|
2018-08-27 23:58:32 +08:00
|
|
|
}
|
|
|
|
|
2019-06-29 10:45:50 +08:00
|
|
|
// GetStack returns the caller stack content,
|
|
|
|
// the optional parameter <skip> specify the skipped stack offset from the end point.
|
|
|
|
func GetStack(skip ...int) string {
|
|
|
|
return logger.GetStack(skip...)
|
2018-08-27 23:58:32 +08:00
|
|
|
}
|
|
|
|
|
2019-06-29 10:45:50 +08:00
|
|
|
// SetStack enables/disables the stack feature in failure logging outputs.
|
|
|
|
func SetStack(enabled bool) {
|
|
|
|
logger.SetStack(enabled)
|
2018-10-24 17:38:07 +08:00
|
|
|
}
|