From 8a4b9ba7c7a16aa92a29e2513d36c5877339220a Mon Sep 17 00:00:00 2001 From: zseeker <675211118@qq.com> Date: Thu, 24 May 2018 12:37:55 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E5=A2=9E=E5=8A=A0glog=E6=8E=A7=E5=88=B6?= =?UTF-8?q?=E5=8F=B0=E6=89=93=E5=8D=B0=E5=BC=80=E5=85=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/os/glog/glog.go | 7 +++++++ g/os/glog/glog_logger.go | 14 +++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/g/os/glog/glog.go b/g/os/glog/glog.go index 00f825d81..6f1f6f2d1 100644 --- a/g/os/glog/glog.go +++ b/g/os/glog/glog.go @@ -50,6 +50,13 @@ func GetPath() string { return logger.path.Val() } +// 设置写日志的同时开启or关闭控制台打印,默认是关闭的 +// @author zseeker +// @date 2018-05-24 +func SetStdPrint(open bool) { + logger.SetStdPrint(open) +} + func Print(v ...interface{}) { logger.Print(v ...) } diff --git a/g/os/glog/glog_logger.go b/g/os/glog/glog_logger.go index cc9ef47eb..8e97778ed 100644 --- a/g/os/glog/glog_logger.go +++ b/g/os/glog/glog_logger.go @@ -27,6 +27,9 @@ const ( // 默认的日志换行符 var ln = "\n" +// 控制台打印开关 +var stdprint = false + // 初始化日志换行符 // @author zseeker // @date 2018-05-23 @@ -95,12 +98,21 @@ func (l *Logger) SetPath(path string) error { return nil } +// 设置写日志时开启or关闭控制台打印,默认是关闭的 +// @author zseeker +func (l *Logger) SetStdPrint(open bool) { + stdprint = open +} + // 这里的写锁保证统一时刻只会写入一行日志,防止串日志的情况 func (l *Logger) print(defaultIO io.Writer, s string) { w := l.GetIO() if w == nil { if v := l.getFileByPool(); v != nil { - w = v.File() + // 同时输出到文件和终端 @author zseeker + if stdprint { + w = io.MultiWriter(v.File(), os.Stdout) + } defer v.Close() } else { w = defaultIO From bc6a61f1623987c7a3d12ed9cdf93515f4cb21b9 Mon Sep 17 00:00:00 2001 From: zseeker <675211118@qq.com> Date: Thu, 24 May 2018 12:59:14 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=96=B0=E5=A2=9Eglog=E6=8E=A7=E5=88=B6?= =?UTF-8?q?=E5=8F=B0=E6=89=93=E5=8D=B0=E5=BC=80=E5=85=B3=EF=BC=8C=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3=E4=B8=80=E5=A4=84=E7=BB=86=E8=8A=82=E9=81=97=E6=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/os/glog/glog_logger.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/g/os/glog/glog_logger.go b/g/os/glog/glog_logger.go index 8e97778ed..61ce2c34e 100644 --- a/g/os/glog/glog_logger.go +++ b/g/os/glog/glog_logger.go @@ -109,9 +109,10 @@ func (l *Logger) print(defaultIO io.Writer, s string) { w := l.GetIO() if w == nil { if v := l.getFileByPool(); v != nil { + w = v.File() // 同时输出到文件和终端 @author zseeker if stdprint { - w = io.MultiWriter(v.File(), os.Stdout) + w = io.MultiWriter(w, os.Stdout) } defer v.Close() } else { From 71d8be8b87675dff4c158b545e09b88d8369a663 Mon Sep 17 00:00:00 2001 From: zseeker <675211118@qq.com> Date: Thu, 24 May 2018 15:50:35 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=96=B0=E5=A2=9Eglog=E6=8E=A7=E5=88=B6?= =?UTF-8?q?=E5=8F=B0=E6=89=93=E5=8D=B0=E5=BC=80=E5=85=B3=EF=BC=8C=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E5=8F=98=E9=87=8F=E5=B9=B6=E5=8F=91=E5=AE=89=E5=85=A8?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/os/glog/glog.go | 2 ++ g/os/glog/glog_logger.go | 12 +++++------- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/g/os/glog/glog.go b/g/os/glog/glog.go index 6f1f6f2d1..c042c4d5b 100644 --- a/g/os/glog/glog.go +++ b/g/os/glog/glog.go @@ -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), } } diff --git a/g/os/glog/glog_logger.go b/g/os/glog/glog_logger.go index 61ce2c34e..b00f56a14 100644 --- a/g/os/glog/glog_logger.go +++ b/g/os/glog/glog_logger.go @@ -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 {