gf/g/os/glog/glog.go

134 lines
3.8 KiB
Go
Raw Normal View History

// 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,
// 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-05-22 09:19:21 +08:00
"github.com/gogf/gf/g/internal/cmdenv"
2019-06-01 19:34:03 +08:00
"github.com/gogf/gf/g/os/grpool"
2019-05-22 09:19:21 +08:00
"io"
2017-11-23 10:21:28 +08:00
)
2018-08-30 13:00:49 +08:00
const (
LEVEL_ALL = LEVEL_DEBU | LEVEL_INFO | LEVEL_NOTI | LEVEL_WARN | LEVEL_ERRO | LEVEL_CRIT
2019-03-19 13:58:18 +08:00
LEVEL_DEV = LEVEL_ALL
LEVEL_PROD = LEVEL_WARN | LEVEL_ERRO | LEVEL_CRIT
2018-08-30 13:00:49 +08:00
LEVEL_DEBU = 1 << iota
LEVEL_INFO
LEVEL_NOTI
LEVEL_WARN
LEVEL_ERRO
LEVEL_CRIT
)
2018-08-24 23:41:58 +08:00
var (
// Default logger object, for package method usage
2018-08-28 17:06:49 +08:00
logger = New()
2019-06-01 19:34:03 +08:00
// Goroutine pool for async logging output.
asyncPool = grpool.New(1)
2018-08-24 23:41:58 +08:00
)
2017-11-23 10:21:28 +08:00
func init() {
SetDebug(cmdenv.Get("gf.glog.debug", true).Bool())
}
2019-02-20 11:16:10 +08:00
// SetPath sets the directory path for file logging.
2018-04-19 16:24:48 +08:00
func SetPath(path string) {
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) {
logger.SetFile(pattern)
}
2019-02-20 11:16:10 +08:00
// SetLevel sets the default logging level.
func SetLevel(level int) {
logger.SetLevel(level)
2019-05-22 09:19:21 +08:00
}
// GetLevel returns the default logging level value.
func GetLevel() int {
return logger.GetLevel()
}
// 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.
// 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.
func SetWriter(writer io.Writer) {
logger.SetWriter(writer)
}
// GetWriter returns the customized writer object, which implements the io.Writer interface.
// It returns nil if no customized writer set.
func GetWriter() io.Writer {
return logger.GetWriter()
}
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) {
logger.SetDebug(debug)
}
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-05-23 11:49:33 +08:00
// SetStdoutPrint sets whether ouptput the logging contents to stdout, which is false in default.
func SetStdoutPrint(enabled bool) {
logger.SetStdoutPrint(enabled)
}
// 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)
}
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-02-20 11:16:10 +08:00
// PrintBacktrace prints the caller backtrace,
// the optional parameter <skip> specify the skipped backtrace offset from the end point.
func PrintBacktrace(skip...int) {
logger.PrintBacktrace(skip...)
}
2019-02-20 11:16:10 +08:00
// GetBacktrace returns the caller backtrace content,
// the optional parameter <skip> specify the skipped backtrace offset from the end point.
func GetBacktrace(skip...int) string {
return logger.GetBacktrace(skip...)
}
2019-02-20 11:16:10 +08:00
// SetBacktrace enables/disables the backtrace feature in failure logging outputs.
func SetBacktrace(enabled bool) {
logger.SetBacktrace(enabled)
}