mirror of
https://gitee.com/shikong-sk/golang-study
synced 2025-02-23 15:32:15 +08:00
docs: 手动实现 simple log 日志库
写入指定日志文件
This commit is contained in:
parent
812c4db396
commit
d1fdc3eacb
@ -81,6 +81,10 @@ func (l *Logger) Warn(log string) {
|
|||||||
|
|
||||||
// Error 消息输出
|
// Error 消息输出
|
||||||
func (l *Logger) Error(log string) {
|
func (l *Logger) Error(log string) {
|
||||||
|
if l.flag&FlagError != FlagError {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
pc, file, line, ok := runtime.Caller(1)
|
pc, file, line, ok := runtime.Caller(1)
|
||||||
l.PrintLog(ERROR, log)
|
l.PrintLog(ERROR, log)
|
||||||
if ok {
|
if ok {
|
||||||
|
@ -3,6 +3,7 @@ package simplelog
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@ -34,3 +35,25 @@ func TestLog(t *testing.T) {
|
|||||||
time.Sleep(100 * time.Millisecond)
|
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 日志 测试")
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user