add logger test case for console log format for component (#2162)
This commit is contained in:
parent
a4574f72a3
commit
1ef0553929
2 changed files with 44 additions and 4 deletions
|
|
@ -11,6 +11,7 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
|
"golang.org/x/term"
|
||||||
)
|
)
|
||||||
|
|
||||||
type LogLevel = zerolog.Level
|
type LogLevel = zerolog.Level
|
||||||
|
|
@ -46,6 +47,8 @@ func init() {
|
||||||
once.Do(func() {
|
once.Do(func() {
|
||||||
zerolog.SetGlobalLevel(zerolog.InfoLevel)
|
zerolog.SetGlobalLevel(zerolog.InfoLevel)
|
||||||
|
|
||||||
|
isTTY := term.IsTerminal(int(os.Stdout.Fd()))
|
||||||
|
|
||||||
consoleWriter := zerolog.ConsoleWriter{
|
consoleWriter := zerolog.ConsoleWriter{
|
||||||
Out: os.Stdout,
|
Out: os.Stdout,
|
||||||
TimeFormat: "15:04:05", // TODO: make it configurable???
|
TimeFormat: "15:04:05", // TODO: make it configurable???
|
||||||
|
|
@ -61,9 +64,12 @@ func init() {
|
||||||
},
|
},
|
||||||
FieldsExclude: []string{Component},
|
FieldsExclude: []string{Component},
|
||||||
FormatPrepare: func(fields map[string]any) error {
|
FormatPrepare: func(fields map[string]any) error {
|
||||||
fields[Component] = fmt.Sprintf("\x1b[33m%v\x1b[0m", fields[Component])
|
if isTTY {
|
||||||
|
fields[Component] = fmt.Sprintf("\x1b[33m%v\x1b[0m", fields[Component])
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
|
NoColor: !isTTY,
|
||||||
}
|
}
|
||||||
|
|
||||||
logger = zerolog.New(consoleWriter).With().Timestamp().Caller().Logger()
|
logger = zerolog.New(consoleWriter).With().Timestamp().Caller().Logger()
|
||||||
|
|
@ -207,16 +213,25 @@ func ConfigureFromEnv() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
locUnknown = "<unknown>"
|
||||||
|
)
|
||||||
|
|
||||||
func getPackageNameFromFile(filePath string) string {
|
func getPackageNameFromFile(filePath string) string {
|
||||||
dir := filepath.Dir(filePath)
|
dir := filepath.Dir(filePath)
|
||||||
importPath := filepath.ToSlash(dir)
|
importPath := filepath.ToSlash(dir)
|
||||||
|
|
||||||
parts := strings.Split(importPath, "/")
|
parts := strings.Split(importPath, "/")
|
||||||
if len(parts) == 0 {
|
if len(parts) == 0 {
|
||||||
return ""
|
return locUnknown
|
||||||
}
|
}
|
||||||
|
|
||||||
return parts[len(parts)-1]
|
pkg := parts[len(parts)-1]
|
||||||
|
if pkg == "." {
|
||||||
|
return "<main>"
|
||||||
|
}
|
||||||
|
|
||||||
|
return pkg
|
||||||
}
|
}
|
||||||
|
|
||||||
func getCallerSkip() (int, string) {
|
func getCallerSkip() (int, string) {
|
||||||
|
|
@ -246,7 +261,7 @@ func getCallerSkip() (int, string) {
|
||||||
return i - 1, getPackageNameFromFile(file)
|
return i - 1, getPackageNameFromFile(file)
|
||||||
}
|
}
|
||||||
|
|
||||||
return 3, "<unknown>"
|
return 3, locUnknown
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:zerologlint
|
//nolint:zerologlint
|
||||||
|
|
|
||||||
|
|
@ -406,3 +406,28 @@ func TestConfigureFromEnvNoEnv(t *testing.T) {
|
||||||
os.Unsetenv("PICOCLAW_LOG_FILE")
|
os.Unsetenv("PICOCLAW_LOG_FILE")
|
||||||
ConfigureFromEnv()
|
ConfigureFromEnv()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetPackageNameFromFile(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
path string
|
||||||
|
want string
|
||||||
|
}{
|
||||||
|
{"normal package path", "/home/user/project/pkg/logger/logger.go", "logger"},
|
||||||
|
{"nested package", "/home/user/project/internal/service/auth/handler.go", "auth"},
|
||||||
|
{"cmd package", "/home/user/project/cmd/server/main.go", "server"},
|
||||||
|
{"project root returns main", "./main.go", "<main>"},
|
||||||
|
{"single dot returns main", ".", "<main>"},
|
||||||
|
{"single directory", "mypkg/file.go", "mypkg"},
|
||||||
|
{"deep nesting", "/a/b/c/d/e/f.go", "e"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
got := getPackageNameFromFile(tt.path)
|
||||||
|
if got != tt.want {
|
||||||
|
t.Errorf("getPackageNameFromFile(%q) = %q, want %q", tt.path, got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue