From 7f894dc715c8176ba9ef18baaeb0be1d591c6d46 Mon Sep 17 00:00:00 2001 From: alex cai Date: Sun, 6 Nov 2016 17:48:13 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=AE=BE=E7=BD=AE=E8=BE=93?= =?UTF-8?q?=E5=87=BA=E7=AD=89=E7=BA=A7=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 25 ++++++++++++++----------- level.go | 4 ++++ 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index d098b66..f3d1d54 100644 --- a/README.md +++ b/README.md @@ -30,27 +30,30 @@ go get -u github.com/ibbd-dev/go-async-log 普通写入日志文件 ```go - lf := asyncLog.NewLogFile("/tmp/test.log") +lf := asyncLog.NewLogFile("/tmp/test.log") - // 设置按天切割文件,如果默认则是按小时 - lf.SetRotate(asyncLog.RotateDate) +// 设置按天切割文件,如果默认则是按小时 +lf.SetRotate(asyncLog.RotateDate) - _ = lf.Write("lf: hello world") +_ = lf.Write("lf: hello world") - // 注意:因为是每秒写入一次,所以这里需要暂停一下 - time.Sleep(time.Second * 2) +// 注意:因为是每秒写入一次,所以这里需要暂停一下 +time.Sleep(time.Second * 2) ``` 写入错误等级文件 ```go - infoFile := asyncLog.NewLevelLog("/tmp/test-info.log", asyncLog.LevelInfo) // 只有Info级别或者以上级别的日志才会被记录 - infoFile.Debug("hello world") // 该日志不会写入文件 - infoFile.Info("hello world") - infoFile.Error("hello world") +infoFile := asyncLog.NewLevelLog("/tmp/test-info.log", asyncLog.LevelInfo) // 只有Info级别或者以上级别的日志才会被记录 +infoFile.Debug("hello world") // 该日志不会写入文件 +infoFile.Info("hello world") +infoFile.Error("hello world") - time.Sleep(time.Second * 2) +// 需要改变日志写入等级时,例如测试阶段 +infoFile.SetLevel(asyncLog.LevelDebug) + +time.Sleep(time.Second * 2) ``` ## 性能数据 diff --git a/level.go b/level.go index 787bccc..c8cd1f2 100644 --- a/level.go +++ b/level.go @@ -33,6 +33,10 @@ func NewLevelLog(filename string, logLevel Priority) *LogFile { return lf } +func (lf *LogFile) SetLevel(logLevel Priority) { + lf.level = logLevel +} + func (lf *LogFile) Debug(msg string) error { return lf.writeLevelMsg(msg, LevelDebug) }