1 Star 0 Fork 0

simplexyz / simplelog-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
log_test.go 3.78 KB
一键复制 编辑 原始数据 按行查看 历史
package log
import (
"bytes"
"fmt"
"os"
"path/filepath"
"runtime/pprof"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/fatih/color"
)
func Test_Color(t *testing.T) {
rb := new(bytes.Buffer)
color.Output = rb
color.NoColor = false
testColors := []struct {
text string
code color.Attribute
}{
{text: "black", code: color.FgBlack},
{text: "red", code: color.FgRed},
{text: "green", code: color.FgGreen},
{text: "yellow", code: color.FgYellow},
{text: "blue", code: color.FgBlue},
{text: "magent", code: color.FgMagenta},
{text: "cyan", code: color.FgCyan},
{text: "white", code: color.FgWhite},
{text: "hblack", code: color.FgHiBlack},
{text: "hred", code: color.FgHiRed},
{text: "hgreen", code: color.FgHiGreen},
{text: "hyellow", code: color.FgHiYellow},
{text: "hblue", code: color.FgHiBlue},
{text: "hmagent", code: color.FgHiMagenta},
{text: "hcyan", code: color.FgHiCyan},
{text: "hwhite", code: color.FgHiWhite},
}
for _, c := range testColors {
color.New(c.code).Print(c.text)
line, _ := rb.ReadString('\n')
scannedLine := fmt.Sprintf("%q", line)
colored := fmt.Sprintf("\x1b[%dm%s\x1b[0m", c.code, c.text)
escapedForm := fmt.Sprintf("%q", colored)
fmt.Printf("%s\t: %s\n", c.text, line)
if scannedLine != escapedForm {
t.Errorf("Expecting %s, got '%s'\n", escapedForm, scannedLine)
}
}
for _, c := range testColors {
line := color.New(c.code).Sprintf("%s", c.text)
scannedLine := fmt.Sprintf("%q", line)
colored := fmt.Sprintf("\x1b[%dm%s\x1b[0m", c.code, c.text)
escapedForm := fmt.Sprintf("%q", colored)
fmt.Printf("%s\t: %s\n", c.text, line)
if scannedLine != escapedForm {
t.Errorf("Expecting %s, got '%s'\n", escapedForm, scannedLine)
}
}
}
func TestLog_Console(t *testing.T) {
Init(&LoggerOption{
Name: "test",
Caller: true,
CallerSkip: 2,
Console: true,
ConsoleColorful: true,
}, nil)
//begin := time.Now()
count := atomic.Int32{}
defer func() {
Final()
var c = count.Load()
t.Logf("count=%d", c)
//t.Logf("consume: %dms\n", time.Since(begin).Milliseconds())
}()
l := Default()
wg := sync.WaitGroup{}
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
for i := 0; i < 10000; i++ {
l.Debugf("%d hello %s", i+1, "world")
l.Infof("%d hello %s", i+1, "world")
l.Warnf("%d hello %s", i+1, "world")
l.Errorf("%d hello %s", i+1, "world")
count.Add(4)
}
wg.Done()
}()
}
wg.Wait()
}
func TestLog_File(t *testing.T) {
Init(
&LoggerOption{
Name: "test",
//Caller: true,
//CallerSkip: 2,
},
MustCreateFileWriter(&FileOption{
Name: "test",
}),
)
f, _ := os.OpenFile("TestLog_File.cpu.profile", os.O_CREATE|os.O_RDWR, 0644)
defer f.Close()
_ = pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
//begin := time.Now()
count := atomic.Int32{}
defer func() {
Final()
var c = count.Load()
t.Logf("count=%d", c)
//t.Logf("consume: %dms\n", time.Since(begin).Milliseconds())
}()
l := Default()
wg := sync.WaitGroup{}
num := 100000
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
for i := 0; i < num; i++ {
tag := (i + 1) * 10
l.Debugf("%d hello %s", tag+1, "world")
l.Infof("%d hello %s", tag+2, "world")
l.Warnf("%d hello %s", tag+3, "world")
l.Errorf("%d hello %s", tag+4, "world")
count.Add(4)
}
wg.Done()
}()
}
wg.Wait()
}
func TestLog_Idle(t *testing.T) {
Init(nil, nil)
defer func() {
Final()
}()
time.Sleep(3 * time.Second)
}
func TestLog_RemoveTimeout(t *testing.T) {
Init(
&LoggerOption{
Name: "test",
},
MustCreateFileWriter(&FileOption{
Name: "test",
MaxAge: 6 * time.Second,
}))
defer func() {
Final()
}()
time.Sleep(5 * time.Second)
}
func Test_JoinPath(t *testing.T) {
t.Log(filepath.Join(".", "test"+".log"))
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/simplexyz/simplelog-go.git
git@gitee.com:simplexyz/simplelog-go.git
simplexyz
simplelog-go
simplelog-go
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891