2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2020-02-26 23:26:24 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
|
|
|
package glog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"github.com/gogf/gf/test/gtest"
|
|
|
|
"github.com/gogf/gf/text/gstr"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_LevelPrefix(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-02-26 23:26:24 +08:00
|
|
|
l := New()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(l.GetLevelPrefix(LEVEL_DEBU), defaultLevelPrefixes[LEVEL_DEBU])
|
|
|
|
t.Assert(l.GetLevelPrefix(LEVEL_INFO), defaultLevelPrefixes[LEVEL_INFO])
|
|
|
|
t.Assert(l.GetLevelPrefix(LEVEL_NOTI), defaultLevelPrefixes[LEVEL_NOTI])
|
|
|
|
t.Assert(l.GetLevelPrefix(LEVEL_WARN), defaultLevelPrefixes[LEVEL_WARN])
|
|
|
|
t.Assert(l.GetLevelPrefix(LEVEL_ERRO), defaultLevelPrefixes[LEVEL_ERRO])
|
|
|
|
t.Assert(l.GetLevelPrefix(LEVEL_CRIT), defaultLevelPrefixes[LEVEL_CRIT])
|
2020-02-26 23:26:24 +08:00
|
|
|
l.SetLevelPrefix(LEVEL_DEBU, "debug")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(l.GetLevelPrefix(LEVEL_DEBU), "debug")
|
2020-02-26 23:26:24 +08:00
|
|
|
l.SetLevelPrefixes(map[int]string{
|
|
|
|
LEVEL_CRIT: "critical",
|
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(l.GetLevelPrefix(LEVEL_DEBU), "debug")
|
|
|
|
t.Assert(l.GetLevelPrefix(LEVEL_INFO), defaultLevelPrefixes[LEVEL_INFO])
|
|
|
|
t.Assert(l.GetLevelPrefix(LEVEL_NOTI), defaultLevelPrefixes[LEVEL_NOTI])
|
|
|
|
t.Assert(l.GetLevelPrefix(LEVEL_WARN), defaultLevelPrefixes[LEVEL_WARN])
|
|
|
|
t.Assert(l.GetLevelPrefix(LEVEL_ERRO), defaultLevelPrefixes[LEVEL_ERRO])
|
|
|
|
t.Assert(l.GetLevelPrefix(LEVEL_CRIT), "critical")
|
2020-02-26 23:26:24 +08:00
|
|
|
})
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-02-26 23:26:24 +08:00
|
|
|
buffer := bytes.NewBuffer(nil)
|
|
|
|
l := New()
|
|
|
|
l.SetWriter(buffer)
|
|
|
|
l.Debug("test1")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(gstr.Contains(buffer.String(), defaultLevelPrefixes[LEVEL_DEBU]), true)
|
2020-02-26 23:26:24 +08:00
|
|
|
|
|
|
|
buffer.Reset()
|
|
|
|
|
|
|
|
l.SetLevelPrefix(LEVEL_DEBU, "debug")
|
|
|
|
l.Debug("test2")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(gstr.Contains(buffer.String(), defaultLevelPrefixes[LEVEL_DEBU]), false)
|
|
|
|
t.Assert(gstr.Contains(buffer.String(), "debug"), true)
|
2020-02-26 23:26:24 +08:00
|
|
|
|
|
|
|
buffer.Reset()
|
|
|
|
l.SetLevelPrefixes(map[int]string{
|
|
|
|
LEVEL_ERRO: "error",
|
|
|
|
})
|
|
|
|
l.Error("test3")
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(gstr.Contains(buffer.String(), defaultLevelPrefixes[LEVEL_ERRO]), false)
|
|
|
|
t.Assert(gstr.Contains(buffer.String(), "error"), true)
|
2020-02-26 23:26:24 +08:00
|
|
|
})
|
|
|
|
}
|