gf/g/os/glog/glog.go

197 lines
4.2 KiB
Go
Raw Normal View History

2017-12-29 16:03:30 +08:00
// Copyright 2017 gf Author(https://gitee.com/johng/gf). All Rights Reserved.
//
// 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://gitee.com/johng/gf.
2018-01-03 11:04:47 +08:00
// 日志模块.
// 直接文件/输出操作,没有异步逻辑,没有使用缓存或者通道
2017-11-23 10:21:28 +08:00
package glog
import (
"io"
2018-04-19 16:24:48 +08:00
"sync"
"gitee.com/johng/gf/g/container/gtype"
2017-11-23 10:21:28 +08:00
)
type Logger struct {
2018-08-28 17:06:49 +08:00
mu sync.RWMutex
pr *Logger // 父级Logger
io io.Writer // 日志内容写入的IO接口
path *gtype.String // 日志写入的目录路径
debug *gtype.Bool // 是否允许输出DEBUG信息
btSkip *gtype.Int // 错误产生时的backtrace回调信息skip条数
btEnabled *gtype.Bool // 是否当打印错误时同时开启backtrace打印
allowMulti *gtype.Bool // 控制台打印开关,当输出到文件时也同时打印到终端
2017-11-23 10:21:28 +08:00
}
2018-08-24 23:41:58 +08:00
var (
// 默认的日志对象
2018-08-28 17:06:49 +08:00
logger = New()
2018-08-24 23:41:58 +08:00
)
2017-11-23 10:21:28 +08:00
// 日志日志目录绝对路径
2018-04-19 16:24:48 +08:00
func SetPath(path string) {
logger.SetPath(path)
2017-11-23 10:21:28 +08:00
}
// 设置是否允许输出DEBUG信息
2017-11-23 10:21:28 +08:00
func SetDebug(debug bool) {
logger.SetDebug(debug)
}
// 获取日志目录绝对路径
2018-04-19 16:24:48 +08:00
func GetPath() string {
return logger.path.Val()
2017-11-23 10:21:28 +08:00
}
2018-08-24 23:41:58 +08:00
// 设置下一次输出的分类,支持多级分类设置
func Cat(category string) *Logger {
return logger.Cat(category)
}
// 打印文件调用回溯信息
func PrintBacktrace(skip...int) {
customSkip := 1
if len(skip) > 0 {
customSkip += skip[0]
}
logger.PrintBacktrace(customSkip)
}
// 获取文件调用回溯信息
func GetBacktrace(skip...int) string {
customSkip := 1
if len(skip) > 0 {
customSkip += skip[0]
}
return logger.GetBacktrace(customSkip)
}
2018-05-24 12:37:55 +08:00
// 设置写日志的同时开启or关闭控制台打印默认是关闭的
// @author zseeker
// @date 2018-05-24
func SetStdPrint(open bool) {
logger.SetStdPrint(open)
}
2017-11-23 10:21:28 +08:00
func Print(v ...interface{}) {
logger.Print(v ...)
}
func Printf(format string, v ...interface{}) {
logger.Printf(format, v ...)
}
func Println(v ...interface{}) {
logger.Println(v ...)
}
func Printfln(format string, v ...interface{}) {
logger.Printfln(format, v ...)
}
func Fatal(v ...interface{}) {
logger.Fatal(v ...)
}
func Fatalf(format string, v ...interface{}) {
logger.Fatalf(format, v ...)
}
func Fatalln(v ...interface{}) {
logger.Fatalln(v ...)
}
func Fatalfln(format string, v ...interface{}) {
logger.Fatalfln(format, v ...)
}
func Panic(v ...interface{}) {
logger.Panic(v ...)
}
func Panicf(format string, v ...interface{}) {
logger.Panicf(format, v ...)
}
func Panicln(v ...interface{}) {
logger.Panicln(v ...)
}
func Panicfln(format string, v ...interface{}) {
logger.Panicfln(format, v ...)
}
func Info(v ...interface{}) {
logger.Info(v...)
}
func Debug(v ...interface{}) {
logger.Debug(v...)
}
func Notice(v ...interface{}) {
logger.Notice(v...)
}
func Warning(v ...interface{}) {
logger.Warning(v...)
}
func Error(v ...interface{}) {
logger.Error(v...)
}
func Critical(v ...interface{}) {
logger.Critical(v...)
}
func Infof(format string, v ...interface{}) {
logger.Infof(format, v...)
}
func Debugf(format string, v ...interface{}) {
logger.Debugf(format, v...)
}
func Noticef(format string, v ...interface{}) {
logger.Noticef(format, v...)
}
func Warningf(format string, v ...interface{}) {
logger.Warningf(format, v...)
}
func Errorf(format string, v ...interface{}) {
logger.Errorf(format, v...)
}
func Criticalf(format string, v ...interface{}) {
logger.Criticalf(format, v...)
}
func Infofln(format string, v ...interface{}) {
logger.Infofln(format, v...)
}
func Debugfln(format string, v ...interface{}) {
logger.Debugfln(format, v...)
}
func Noticefln(format string, v ...interface{}) {
logger.Noticefln(format, v...)
}
func Warningfln(format string, v ...interface{}) {
logger.Warningfln(format, v...)
}
func Errorfln(format string, v ...interface{}) {
logger.Errorfln(format, v...)
}
func Criticalfln(format string, v ...interface{}) {
logger.Criticalfln(format, v...)
}