Compare commits
4 Commits
Author | SHA1 | Date |
---|---|---|
kingecg | 74b7feac5b | |
kingecg | 45c66585ad | |
kingecg | d13d851819 | |
kingecg | 8515465587 |
|
@ -15,3 +15,4 @@
|
||||||
# Dependency directories (remove the comment below to include it)
|
# Dependency directories (remove the comment below to include it)
|
||||||
# vendor/
|
# vendor/
|
||||||
|
|
||||||
|
log/
|
|
@ -0,0 +1,9 @@
|
||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2024 kingecg
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
34
format.go
34
format.go
|
@ -2,13 +2,21 @@ package gologger
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
const logTemplate = "[%s] %s : %s - %s\n"
|
const logTemplate = "[%s] %s : %s - %s\n"
|
||||||
|
|
||||||
func format(logEvent LogEvent) string {
|
func format(logEvent LogEvent) string {
|
||||||
data := logEvent.Ts.Format("2006-01-02 15:04:05")
|
data := logEvent.Ts.Format("2006-01-02 15:04:05")
|
||||||
msg := fmt.Sprint(logEvent.Data...)
|
msg := ""
|
||||||
|
firstMsg := logEvent.Data[0]
|
||||||
|
if isFormatString(firstMsg) {
|
||||||
|
msg = fmt.Sprintf(firstMsg.(string), logEvent.Data[1:]...)
|
||||||
|
} else {
|
||||||
|
msg = sprint(logEvent.Data)
|
||||||
|
}
|
||||||
|
|
||||||
ret := fmt.Sprintf(logTemplate, data, logEvent.Category, getLogLevelStr(logEvent.Level), msg)
|
ret := fmt.Sprintf(logTemplate, data, logEvent.Category, getLogLevelStr(logEvent.Level), msg)
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
@ -16,8 +24,30 @@ func format(logEvent LogEvent) string {
|
||||||
func getLogLevelStr(level int) string {
|
func getLogLevelStr(level int) string {
|
||||||
for name, slevel := range logLevelMap {
|
for name, slevel := range logLevelMap {
|
||||||
if slevel == level {
|
if slevel == level {
|
||||||
return name
|
return strings.ToUpper(name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return "Unknown"
|
return "Unknown"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isFormatString(f interface{}) bool {
|
||||||
|
s, ok := f.(string)
|
||||||
|
if !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
// 尝试使用空接口来格式化字符串
|
||||||
|
m := fmt.Sprintf(s, []interface{}{}...)
|
||||||
|
return strings.Index(m, "MISSING") != -1
|
||||||
|
}
|
||||||
|
|
||||||
|
func sprint(s []interface{}) string {
|
||||||
|
str := make([]any, len(s))
|
||||||
|
for i, v := range s {
|
||||||
|
if i > 0 {
|
||||||
|
str[i] = fmt.Sprintf(" %v", v)
|
||||||
|
} else {
|
||||||
|
str[i] = fmt.Sprintf("%v", v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return fmt.Sprint(str...)
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,54 @@
|
||||||
|
package gologger
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestFormat(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
logEvent LogEvent
|
||||||
|
want string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "Info level with format string",
|
||||||
|
logEvent: LogEvent{
|
||||||
|
Ts: time.Now(),
|
||||||
|
Category: "test_category",
|
||||||
|
Level: Info,
|
||||||
|
Data: []interface{}{"%s: %v", "key", "value"},
|
||||||
|
},
|
||||||
|
want: "test_category : INFO - key: value",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Info level with one format string",
|
||||||
|
logEvent: LogEvent{
|
||||||
|
Ts: time.Now(),
|
||||||
|
Category: "test_category",
|
||||||
|
Level: Info,
|
||||||
|
Data: []interface{}{"%s", "key"},
|
||||||
|
},
|
||||||
|
want: "test_category : INFO - key",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Error level without format string",
|
||||||
|
logEvent: LogEvent{
|
||||||
|
Ts: time.Now(),
|
||||||
|
Category: "test_category",
|
||||||
|
Level: Error,
|
||||||
|
Data: []interface{}{"error occurred", "additional info"},
|
||||||
|
},
|
||||||
|
want: "test_category : ERROR - error occurred additional info",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
if got := format(tt.logEvent); strings.Index(got, tt.want) == -1 {
|
||||||
|
t.Errorf("format() = %v, want %v", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,7 +4,7 @@ import (
|
||||||
logger "git.pyer.club/kingecg/gologger"
|
logger "git.pyer.club/kingecg/gologger"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func aqmain() {
|
||||||
logger.Configure(logger.LoggersConfig{
|
logger.Configure(logger.LoggersConfig{
|
||||||
Categories: map[string]logger.LogConfig{
|
Categories: map[string]logger.LogConfig{
|
||||||
"default": {
|
"default": {
|
||||||
|
@ -40,5 +40,4 @@ func main() {
|
||||||
defaultLogger.Debug("debug again")
|
defaultLogger.Debug("debug again")
|
||||||
fatLogger.Debug("debug again")
|
fatLogger.Debug("debug again")
|
||||||
fatLogger.Error("This is error")
|
fatLogger.Error("This is error")
|
||||||
select {}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue