完成orm的调试模式下日志自动输出改进,改进glog增加日志等级管理功能、增加链式操作、日志分类管理

This commit is contained in:
john
2018-08-30 13:54:45 +08:00
parent fa5d25c6dd
commit c1e0a1620a
6 changed files with 135 additions and 86 deletions

4
TODO
View File

@ -13,7 +13,6 @@ gtime增加对时区转换的封装并简化失去转换时对类似+80500时
orm增加sqlite对Save方法的支持(去掉触发器语句);
ghttp.Server增加Ip访问控制功能(DenyIps&AllowIps)
ghttp路由功能增加分组路由特性
解决glog串日志情况
ghttp增加返回数据压缩机制
gview中的template标签失效问题
gfile文件stat信息使用gfsnotify进行缓存更新改进
@ -63,4 +62,5 @@ DONE:
37. ghttp获取参数支持直接转struct功能
38. gfsnotify增加对于目录的监控
39. 检查windows下的平滑重启失效问题
40. ghttp.Server的Cookie及Session锁机制优化(去掉map锁机制);
40. ghttp.Server的Cookie及Session锁机制优化(去掉map锁机制);
41. 解决glog串日志情况

1
g/g.go
View File

@ -32,7 +32,6 @@ type Map = map[string]interface{}
// 常用list数据结构(使用别名)
type List = []Map
// 阻塞等待HTTPServer执行完成(同一进程多HTTPServer情况下)
func Wait() {
ghttp.Wait()

View File

@ -3,6 +3,7 @@
// 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.
// @author john, zseeker
// 日志模块.
// 直接文件/输出操作,没有异步逻辑,没有使用缓存或者通道
@ -36,6 +37,9 @@ const (
)
var (
// glog默认的日志等级影响全局
defaultLevel = gtype.NewInt(LEVEL_ALL)
// 默认的日志对象
logger = New()
)
@ -45,11 +49,27 @@ func SetPath(path string) {
logger.SetPath(path)
}
// 设置全局的日志记录等级
func SetLevel(level int) {
logger.SetLevel(level)
defaultLevel.Set(level)
}
// 获取全局的日志记录等级
func GetLevel() int {
return defaultLevel.Val()
}
// 设置是否允许输出DEBUG信息
func SetDebug(debug bool) {
logger.SetDebug(debug)
}
// 设置写日志的同时开启or关闭控制台打印默认是关闭的
func SetStdPrint(open bool) {
logger.SetStdPrint(open)
}
// 获取日志目录绝对路径
func GetPath() string {
return logger.path.Val()
@ -73,18 +93,16 @@ func GetBacktrace(skip...int) string {
return logger.GetBacktrace(customSkip)
}
// 设置写日志的同时开启or关闭控制台打印默认是关闭的
// @author zseeker
// @date 2018-05-24
func SetStdPrint(open bool) {
logger.SetStdPrint(open)
}
// 设置下一次输出的分类,支持多级分类设置
func Cat(category string) *Logger {
return logger.Cat(category)
}
// 设置日志打印等级
func Level(level int) *Logger {
return logger.Level(level)
}
// 设置文件调用回溯信息
func Backtrace(enabled bool, skip...int) *Logger {
return logger.Backtrace(enabled, skip...)

View File

@ -42,7 +42,7 @@ func New() *Logger {
return &Logger {
io : nil,
path : gtype.NewString(),
level : gtype.NewInt(LEVEL_ALL),
level : gtype.NewInt(defaultLevel.Val()),
btSkip : gtype.NewInt(),
btEnabled : gtype.NewBool(true),
alsoStdPrint : gtype.NewBool(true),
@ -67,6 +67,11 @@ func (l *Logger) SetLevel(level int) {
l.level.Set(level)
}
// 获取日志记录等级
func (l *Logger) GetLevel() int {
return l.level.Val()
}
// 快捷方法打开或关闭DEBU日志信息
func (l *Logger) SetDebug(debug bool) {
l.level.Set(l.level.Val()|LEVEL_DEBU)
@ -164,7 +169,7 @@ func (l *Logger) stdPrint(s string) {
func (l *Logger) errPrint(s string) {
// 记录调用回溯信息
if l.btEnabled.Val() {
backtrace := l.GetBacktrace(3)
backtrace := l.GetBacktrace(2)
if s[len(s) - 1] == byte('\n') {
s = s + backtrace + ln
} else {
@ -270,114 +275,114 @@ func (l *Logger) Panicfln(format string, v ...interface{}) {
}
func (l *Logger) Info(v ...interface{}) {
if l.level.Val() & LEVEL_INFO > 0 {
if l.checkLevel(LEVEL_INFO) {
l.stdPrint("[INFO] " + fmt.Sprint(v...) + ln)
}
}
func (l *Logger) Debug(v ...interface{}) {
if l.level.Val() & LEVEL_DEBU > 0 {
l.stdPrint("[DEBU] " + fmt.Sprint(v...) + ln)
}
}
func (l *Logger) Notice(v ...interface{}) {
if l.level.Val() & LEVEL_NOTI > 0 {
}
l.errPrint("[NOTI] " + fmt.Sprint(v...) + ln)
}
func (l *Logger) Warning(v ...interface{}) {
if l.level.Val() & LEVEL_INFO > 0 {
}
l.errPrint("[WARN] " + fmt.Sprint(v...) + ln)
}
func (l *Logger) Error(v ...interface{}) {
if l.level.Val() & LEVEL_INFO > 0 {
}
l.errPrint("[ERRO] " + fmt.Sprint(v...) + ln)
}
func (l *Logger) Critical(v ...interface{}) {
if l.level.Val() & LEVEL_INFO > 0 {
}
l.errPrint("[CRIT] " + fmt.Sprint(v...) + ln)
}
func (l *Logger) Infof(format string, v ...interface{}) {
if l.level.Val() & LEVEL_INFO > 0 {
}
l.stdPrint("[INFO] " + fmt.Sprintf(format, v...))
}
func (l *Logger) Debugf(format string, v ...interface{}) {
if l.level.Val() & LEVEL_INFO > 0 {
l.stdPrint("[DEBU] " + fmt.Sprintf(format, v...))
}
}
func (l *Logger) Noticef(format string, v ...interface{}) {
if l.level.Val() & LEVEL_NOTI > 0 {
l.errPrint("[NOTI] " + fmt.Sprintf(format, v...))
}
}
func (l *Logger) Warningf(format string, v ...interface{}) {
if l.level.Val() & LEVEL_WARN > 0 {
l.errPrint("[WARN] " + fmt.Sprintf(format, v...))
}
}
func (l *Logger) Errorf(format string, v ...interface{}) {
if l.level.Val() & LEVEL_ERRO > 0 {
l.errPrint("[ERRO] " + fmt.Sprintf(format, v...))
}
}
func (l *Logger) Criticalf(format string, v ...interface{}) {
if l.level.Val() & LEVEL_CRIT > 0 {
l.errPrint("[CRIT] " + fmt.Sprintf(format, v...))
if l.checkLevel(LEVEL_INFO) {
l.stdPrint("[INFO] " + fmt.Sprintf(format, v...))
}
}
func (l *Logger) Infofln(format string, v ...interface{}) {
if l.level.Val() & LEVEL_INFO > 0 {
if l.checkLevel(LEVEL_INFO) {
l.stdPrint("[INFO] " + fmt.Sprintf(format, v...) + ln)
}
}
func (l *Logger) Debug(v ...interface{}) {
if l.checkLevel(LEVEL_DEBU) {
l.stdPrint("[DEBU] " + fmt.Sprint(v...) + ln)
}
}
func (l *Logger) Debugf(format string, v ...interface{}) {
if l.checkLevel(LEVEL_DEBU) {
l.stdPrint("[DEBU] " + fmt.Sprintf(format, v...))
}
}
func (l *Logger) Debugfln(format string, v ...interface{}) {
if l.level.Val() & LEVEL_DEBU > 0 {
if l.checkLevel(LEVEL_DEBU) {
l.stdPrint("[DEBU] " + fmt.Sprintf(format, v...) + ln)
}
}
func (l *Logger) Notice(v ...interface{}) {
if l.checkLevel(LEVEL_NOTI) {
l.errPrint("[NOTI] " + fmt.Sprint(v...) + ln)
}
}
func (l *Logger) Noticef(format string, v ...interface{}) {
if l.checkLevel(LEVEL_NOTI) {
l.errPrint("[NOTI] " + fmt.Sprintf(format, v...))
}
}
func (l *Logger) Noticefln(format string, v ...interface{}) {
if l.level.Val() & LEVEL_NOTI > 0 {
if l.checkLevel(LEVEL_NOTI) {
l.errPrint("[NOTI] " + fmt.Sprintf(format, v...) + ln)
}
}
func (l *Logger) Warning(v ...interface{}) {
if l.checkLevel(LEVEL_WARN) {
l.errPrint("[WARN] " + fmt.Sprint(v...) + ln)
}
}
func (l *Logger) Warningf(format string, v ...interface{}) {
if l.checkLevel(LEVEL_WARN) {
l.errPrint("[WARN] " + fmt.Sprintf(format, v...))
}
}
func (l *Logger) Warningfln(format string, v ...interface{}) {
if l.level.Val() & LEVEL_WARN > 0 {
if l.checkLevel(LEVEL_WARN) {
l.errPrint("[WARN] " + fmt.Sprintf(format, v...) + ln)
}
}
func (l *Logger) Error(v ...interface{}) {
if l.checkLevel(LEVEL_ERRO) {
l.errPrint("[ERRO] " + fmt.Sprint(v...) + ln)
}
}
func (l *Logger) Errorf(format string, v ...interface{}) {
if l.checkLevel(LEVEL_ERRO) {
l.errPrint("[ERRO] " + fmt.Sprintf(format, v...))
}
}
func (l *Logger) Errorfln(format string, v ...interface{}) {
if l.level.Val() & LEVEL_ERRO > 0 {
if l.checkLevel(LEVEL_ERRO) {
l.errPrint("[ERRO] " + fmt.Sprintf(format, v...) + ln)
}
}
func (l *Logger) Critical(v ...interface{}) {
if l.checkLevel(LEVEL_CRIT) {
l.errPrint("[CRIT] " + fmt.Sprint(v...) + ln)
}
}
func (l *Logger) Criticalf(format string, v ...interface{}) {
if l.checkLevel(LEVEL_CRIT) {
l.errPrint("[CRIT] " + fmt.Sprintf(format, v...))
}
}
func (l *Logger) Criticalfln(format string, v ...interface{}) {
if l.level.Val() & LEVEL_CRIT > 0 {
if l.checkLevel(LEVEL_CRIT) {
l.errPrint("[CRIT] " + fmt.Sprintf(format, v...) + ln)
}
}
// 判断给定level是否满足
func (l *Logger) checkLevel(level int) bool {
return l.level.Val() & level > 0
}

View File

@ -25,6 +25,18 @@ func (l *Logger) Cat(category string) *Logger {
return logger
}
// 设置日志打印等级
func (l *Logger) Level(level int) *Logger {
logger := (*Logger)(nil)
if l.pr == nil {
logger = l.Clone()
} else {
logger = l
}
logger.SetLevel(level)
return logger
}
// 设置文件调用回溯信息
func (l *Logger) Backtrace(enabled bool, skip...int) *Logger {
logger := (*Logger)(nil)

15
geg/os/glog/glog_level.go Normal file
View File

@ -0,0 +1,15 @@
package main
import (
"gitee.com/johng/gf/g/os/glog"
)
// 设置日志等级
func main() {
l := glog.New()
l.Info("info1")
l.SetLevel(glog.LEVEL_ALL^glog.LEVEL_INFO)
l.Info("info2")
}