mirror of
https://gitee.com/shikong-sk/golang-study
synced 2025-02-22 23:12:15 +08:00
61 lines
1.2 KiB
Go
61 lines
1.2 KiB
Go
package simplelog
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestLog(t *testing.T) {
|
|
log := NewLog()
|
|
|
|
log2 := NewLog(
|
|
WithWriter(os.Stdout),
|
|
WithTimeFormat("2006-01-02"),
|
|
WithLogFormat("%-10s %s %v %s =-=-> %s\n"),
|
|
WithLogFlag(FlagInfo|FlagWarn|FlagError),
|
|
)
|
|
|
|
fmt.Printf("simpleLog => %#v\n", log)
|
|
|
|
fmt.Println("=========================================================")
|
|
|
|
for i := 0; i < 5; i++ {
|
|
log.Debug("测试 Debug 输出")
|
|
time.Sleep(100 * time.Millisecond)
|
|
}
|
|
|
|
fmt.Println("=========================================================")
|
|
|
|
for i := 0; i < 5; i++ {
|
|
log2.Debug("测试 Debug 输出")
|
|
log2.Error("测试 Error 输出")
|
|
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(
|
|
WithLogFile(logFilePath),
|
|
WithLogFlag(FlagInfo|FlagDebug|FlagWarn),
|
|
WithSplitSize(1*K),
|
|
)
|
|
|
|
log.Debug("Debug 日志 测试")
|
|
log.Error("Error 日志 测试")
|
|
log.Info("Info 日志 测试")
|
|
}
|