From d1fdc3eacb7d2d871188f0267a50a299606a02d7 Mon Sep 17 00:00:00 2001 From: Shikong <919411476@qq.com> Date: Wed, 6 Oct 2021 17:18:20 +0800 Subject: [PATCH] =?UTF-8?q?docs:=20=E6=89=8B=E5=8A=A8=E5=AE=9E=E7=8E=B0=20?= =?UTF-8?q?simple=20log=20=E6=97=A5=E5=BF=97=E5=BA=93=20=E5=86=99=E5=85=A5?= =?UTF-8?q?=E6=8C=87=E5=AE=9A=E6=97=A5=E5=BF=97=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- simplelog/simplelog.go | 4 ++++ simplelog/simplelog_test.go | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/simplelog/simplelog.go b/simplelog/simplelog.go index c59730b..111fe9d 100644 --- a/simplelog/simplelog.go +++ b/simplelog/simplelog.go @@ -81,6 +81,10 @@ func (l *Logger) Warn(log string) { // Error 消息输出 func (l *Logger) Error(log string) { + if l.flag&FlagError != FlagError { + return + } + pc, file, line, ok := runtime.Caller(1) l.PrintLog(ERROR, log) if ok { diff --git a/simplelog/simplelog_test.go b/simplelog/simplelog_test.go index 1c88501..d253342 100644 --- a/simplelog/simplelog_test.go +++ b/simplelog/simplelog_test.go @@ -3,6 +3,7 @@ package simplelog import ( "fmt" "os" + "path/filepath" "testing" "time" ) @@ -34,3 +35,25 @@ func TestLog(t *testing.T) { time.Sleep(100 * time.Millisecond) } } + +func TestLogToFile(t *testing.T) { + logFilePath, _ := filepath.Abs(filepath.Join(os.TempDir(), "./log.log")) + + f, err := os.OpenFile(logFilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + if err != nil { + t.Fatal(err) + } + defer func() { + _ = f.Close() + }() + + fmt.Printf("写入日志文件 %s\n", logFilePath) + log := NewLog( + WithWriter(f), + WithLogFlag(FlagInfo|FlagDebug|FlagWarn), + ) + + log.Debug("Debug 日志 测试") + log.Error("Error 日志 测试") + log.Info("Info 日志 测试") +}