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 日志 测试") +}