docs: 手动实现 simple log 日志库

写入指定日志文件
This commit is contained in:
Shikong 2021-10-06 17:18:20 +08:00
parent 812c4db396
commit d1fdc3eacb
2 changed files with 27 additions and 0 deletions

View File

@ -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 {

View File

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