新增glog控制台打印开关,修复变量并发安全问题

This commit is contained in:
zseeker
2018-05-24 15:50:35 +08:00
parent 96433e8d8f
commit ac7645134c
2 changed files with 7 additions and 7 deletions

View File

@ -20,6 +20,7 @@ type Logger struct {
path *gtype.String // 日志写入的目录路径
debug *gtype.Bool // 是否允许输出DEBUG信息
btSkip *gtype.Int // 错误产生时的backtrace回调信息skip条数
stdprint *gtype.Bool // 控制台打印开关 @author zseeker
}
// 默认的日志对象
@ -32,6 +33,7 @@ func New() *Logger {
path : gtype.NewString(),
debug : gtype.NewBool(true),
btSkip : gtype.NewInt(3),
stdprint : gtype.NewBool(false),
}
}

View File

@ -27,16 +27,13 @@ const (
// 默认的日志换行符
var ln = "\n"
// 控制台打印开关
var stdprint = false
// 初始化日志换行符
// @author zseeker
// @date 2018-05-23
func init() {
if runtime.GOOS == "windows" {
ln = "\r\n"
}
}
}
// 设置BacktraceSkip
@ -100,8 +97,9 @@ func (l *Logger) SetPath(path string) error {
// 设置写日志时开启or关闭控制台打印默认是关闭的
// @author zseeker
// @date 2018-05-24
func (l *Logger) SetStdPrint(open bool) {
stdprint = open
l.stdprint.Set(open)
}
// 这里的写锁保证统一时刻只会写入一行日志,防止串日志的情况
@ -111,8 +109,8 @@ func (l *Logger) print(defaultIO io.Writer, s string) {
if v := l.getFileByPool(); v != nil {
w = v.File()
// 同时输出到文件和终端 @author zseeker
if stdprint {
w = io.MultiWriter(w, os.Stdout)
if l.stdprint.Val() {
w = io.MultiWriter(w, defaultIO)
}
defer v.Close()
} else {