Compare commits
No commits in common. "master" and "v1.0.1" have entirely different histories.
|
@ -15,4 +15,3 @@
|
|||
# Dependency directories (remove the comment below to include it)
|
||||
# vendor/
|
||||
|
||||
log/
|
|
@ -9,7 +9,7 @@
|
|||
"type": "go",
|
||||
"request": "launch",
|
||||
"mode": "auto",
|
||||
"program": "${workspaceFolder}/test",
|
||||
"program": "${fileDirname}",
|
||||
"cwd": "${workspaceFolder}",
|
||||
}
|
||||
]
|
||||
|
|
9
LICENSE
9
LICENSE
|
@ -1,9 +0,0 @@
|
|||
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.
|
|
@ -15,11 +15,6 @@ const (
|
|||
type ConsoleAppender struct {
|
||||
}
|
||||
|
||||
// Close implements LoggerAppender.
|
||||
func (c *ConsoleAppender) Close() {
|
||||
|
||||
}
|
||||
|
||||
func (c *ConsoleAppender) GetName() string {
|
||||
return "console"
|
||||
}
|
||||
|
@ -40,9 +35,9 @@ func (c *ConsoleAppender) Append(logEvent LogEvent) {
|
|||
fmt.Printf(TraceTemplate, logMsg)
|
||||
}
|
||||
}
|
||||
func makeConsoleAppender(appenderConfig LogAppenderConfig) *LoggerAppender {
|
||||
func makeConsoleAppender(appenderConfig LogAppenderConfig) LoggerAppender {
|
||||
var appender LoggerAppender = &ConsoleAppender{}
|
||||
return &appender
|
||||
return appender
|
||||
}
|
||||
func init() {
|
||||
RegistAppender("console", makeConsoleAppender)
|
||||
|
|
37
file.go
37
file.go
|
@ -7,24 +7,14 @@ import (
|
|||
|
||||
type FileAppender struct {
|
||||
filePath string
|
||||
lchan chan LogEvent
|
||||
file *os.File
|
||||
stopChan chan struct{}
|
||||
}
|
||||
|
||||
// Close implements LoggerAppender.
|
||||
func (f *FileAppender) Close() {
|
||||
//send stop signal
|
||||
f.stopChan <- struct{}{}
|
||||
}
|
||||
|
||||
func (f *FileAppender) GetName() string {
|
||||
return "FileAppender:" + f.filePath
|
||||
}
|
||||
|
||||
func (f *FileAppender) start() {
|
||||
f.lchan = make(chan LogEvent, 10)
|
||||
f.stopChan = make(chan struct{})
|
||||
func (f *FileAppender) Append(logEvent LogEvent) {
|
||||
if f.file == nil || int(f.file.Fd()) == -1 {
|
||||
dirName := filepath.Dir(f.filePath)
|
||||
_, err := os.Stat(dirName)
|
||||
|
@ -34,26 +24,12 @@ func (f *FileAppender) start() {
|
|||
f.file, _ = os.OpenFile(f.filePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||
}
|
||||
|
||||
go func() {
|
||||
defer f.file.Close()
|
||||
for {
|
||||
select {
|
||||
case <-f.stopChan:
|
||||
return
|
||||
case logEvent := <-f.lchan:
|
||||
logMsg := format(logEvent)
|
||||
f.file.WriteString(logMsg)
|
||||
}
|
||||
logMsg := format(logEvent)
|
||||
f.file.WriteString(logMsg)
|
||||
|
||||
}
|
||||
}()
|
||||
}
|
||||
func (f *FileAppender) Append(logEvent LogEvent) {
|
||||
|
||||
f.lchan <- logEvent
|
||||
}
|
||||
|
||||
func makeFileAppender(appenderConfig LogAppenderConfig) *LoggerAppender {
|
||||
func makeFileAppender(appenderConfig LogAppenderConfig) LoggerAppender {
|
||||
var logfile interface{}
|
||||
var ok bool
|
||||
logfile, ok = appenderConfig.Options["file"]
|
||||
|
@ -63,10 +39,7 @@ func makeFileAppender(appenderConfig LogAppenderConfig) *LoggerAppender {
|
|||
var ret LoggerAppender = &FileAppender{
|
||||
filePath: logfile.(string),
|
||||
}
|
||||
|
||||
ret.(*FileAppender).start()
|
||||
|
||||
return &ret
|
||||
return ret
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
|
34
format.go
34
format.go
|
@ -2,21 +2,13 @@ package gologger
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const logTemplate = "[%s] %s : %s - %s\n"
|
||||
|
||||
func format(logEvent LogEvent) string {
|
||||
data := logEvent.Ts.Format("2006-01-02 15:04:05")
|
||||
msg := ""
|
||||
firstMsg := logEvent.Data[0]
|
||||
if isFormatString(firstMsg) {
|
||||
msg = fmt.Sprintf(firstMsg.(string), logEvent.Data[1:]...)
|
||||
} else {
|
||||
msg = sprint(logEvent.Data)
|
||||
}
|
||||
|
||||
msg := fmt.Sprint(logEvent.Data...)
|
||||
ret := fmt.Sprintf(logTemplate, data, logEvent.Category, getLogLevelStr(logEvent.Level), msg)
|
||||
return ret
|
||||
}
|
||||
|
@ -24,30 +16,8 @@ func format(logEvent LogEvent) string {
|
|||
func getLogLevelStr(level int) string {
|
||||
for name, slevel := range logLevelMap {
|
||||
if slevel == level {
|
||||
return strings.ToUpper(name)
|
||||
return name
|
||||
}
|
||||
}
|
||||
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...)
|
||||
}
|
||||
|
|
|
@ -1,54 +0,0 @@
|
|||
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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
38
main.go
38
main.go
|
@ -22,9 +22,9 @@ var logLevelMap map[string]int = map[string]int{
|
|||
"debug": Debug,
|
||||
"trace": Trace,
|
||||
}
|
||||
var loggerMap map[string]*Logger = map[string]*Logger{}
|
||||
var appenderFactoryMap map[string]func(LogAppenderConfig) *LoggerAppender = map[string]func(LogAppenderConfig) *LoggerAppender{}
|
||||
var appenders map[string]*LoggerAppender = map[string]*LoggerAppender{}
|
||||
var loggerMap map[string]Logger = map[string]Logger{}
|
||||
var appenderFactoryMap map[string]func(LogAppenderConfig) LoggerAppender = map[string]func(LogAppenderConfig) LoggerAppender{}
|
||||
var appenders map[string]LoggerAppender = map[string]LoggerAppender{}
|
||||
var loggerConfig LoggersConfig
|
||||
|
||||
type LogAppenderConfig struct {
|
||||
|
@ -43,7 +43,7 @@ type LoggersConfig struct {
|
|||
type Logger struct {
|
||||
category string
|
||||
level int
|
||||
appenders []*LoggerAppender
|
||||
appenders []LoggerAppender
|
||||
}
|
||||
|
||||
type LogEvent struct {
|
||||
|
@ -56,14 +56,13 @@ type LogEvent struct {
|
|||
type LoggerAppender interface {
|
||||
GetName() string
|
||||
Append(logEvent LogEvent)
|
||||
Close()
|
||||
}
|
||||
|
||||
var consoleAppender LoggerAppender = &ConsoleAppender{}
|
||||
var defaultLogger = &Logger{
|
||||
|
||||
level: Error,
|
||||
appenders: []*LoggerAppender{&consoleAppender},
|
||||
appenders: []LoggerAppender{consoleAppender},
|
||||
}
|
||||
|
||||
func (l *Logger) log(Level int, msg []interface{}) {
|
||||
|
@ -72,7 +71,7 @@ func (l *Logger) log(Level int, msg []interface{}) {
|
|||
now := time.Now()
|
||||
logEvent := LogEvent{l.category, now, Level, msg}
|
||||
for _, appender := range l.appenders {
|
||||
(*appender).Append(logEvent)
|
||||
appender.Append(logEvent)
|
||||
}
|
||||
// l.Appender.Append(logEvent)
|
||||
// fmt.Println(now.Format("2006-01-02 15:04:05"), " ", l.Name, ": ", msg)
|
||||
|
@ -98,7 +97,7 @@ func (l *Logger) Debug(msg ...interface{}) {
|
|||
func (l *Logger) Trace(msg ...interface{}) {
|
||||
l.log(Trace, msg)
|
||||
}
|
||||
func GetLogger(name string) *Logger {
|
||||
func GetLogger(name string) Logger {
|
||||
if logger, ok := loggerMap[name]; ok {
|
||||
return logger
|
||||
} else {
|
||||
|
@ -107,30 +106,29 @@ func GetLogger(name string) *Logger {
|
|||
return makeLogger(name, logConfig)
|
||||
}
|
||||
if name == "default" {
|
||||
return defaultLogger
|
||||
return *defaultLogger
|
||||
}
|
||||
l := *GetLogger("default")
|
||||
l.category = name
|
||||
loggerMap[name] = &l
|
||||
return &l
|
||||
ret := GetLogger("default")
|
||||
ret.category = name
|
||||
return ret
|
||||
}
|
||||
}
|
||||
|
||||
func makeLogger(name string, config LogConfig) *Logger {
|
||||
func makeLogger(name string, config LogConfig) Logger {
|
||||
logger := &Logger{category: name}
|
||||
levelstr := strings.ToLower(config.Level)
|
||||
logger.level = logLevelMap[levelstr]
|
||||
|
||||
if config.Appenders == nil || len(config.Appenders) == 0 {
|
||||
logger.appenders = []*LoggerAppender{&consoleAppender}
|
||||
logger.appenders = []LoggerAppender{consoleAppender}
|
||||
} else {
|
||||
logger.appenders = make([]*LoggerAppender, len(config.Appenders))
|
||||
logger.appenders = make([]LoggerAppender, len(config.Appenders))
|
||||
for i, appenderName := range config.Appenders {
|
||||
logger.appenders[i] = appenders[appenderName]
|
||||
}
|
||||
}
|
||||
loggerMap[name] = logger
|
||||
return logger
|
||||
loggerMap[name] = *logger
|
||||
return *logger
|
||||
}
|
||||
func Configure(config LoggersConfig) {
|
||||
loggerConfig = config
|
||||
|
@ -139,7 +137,7 @@ func Configure(config LoggersConfig) {
|
|||
if ok {
|
||||
appenders[name] = appenderFactory(appenderConfig)
|
||||
} else {
|
||||
appenders[name] = &consoleAppender
|
||||
appenders[name] = &ConsoleAppender{}
|
||||
}
|
||||
}
|
||||
for name, _ := range loggerConfig.Categories {
|
||||
|
@ -147,7 +145,7 @@ func Configure(config LoggersConfig) {
|
|||
}
|
||||
}
|
||||
|
||||
func RegistAppender(typeName string, appenderCreatCb func(LogAppenderConfig) *LoggerAppender) {
|
||||
func RegistAppender(typeName string, appenderCreatCb func(LogAppenderConfig) LoggerAppender) {
|
||||
|
||||
appenderFactoryMap[typeName] = appenderCreatCb
|
||||
}
|
||||
|
|
26
main_test.go
26
main_test.go
|
@ -1,26 +0,0 @@
|
|||
// TestGetLogger tests the GetLogger function
|
||||
package gologger
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestGetLogger(t *testing.T) {
|
||||
// Initialize loggerMap and loggerConfig
|
||||
|
||||
dl := GetLogger("default")
|
||||
if dl != defaultLogger {
|
||||
t.Errorf("GetLogger(\"defult\") should return defaultLogger")
|
||||
}
|
||||
|
||||
al := GetLogger("app")
|
||||
|
||||
if al == dl {
|
||||
t.Errorf("GetLogger(\"app\") should return a new logger")
|
||||
}
|
||||
|
||||
al2 := GetLogger("app")
|
||||
|
||||
if al2 != al {
|
||||
t.Errorf("GetLogger(\"app\") should return the same logger")
|
||||
}
|
||||
|
||||
}
|
|
@ -4,7 +4,7 @@ import (
|
|||
logger "git.pyer.club/kingecg/gologger"
|
||||
)
|
||||
|
||||
func aqmain() {
|
||||
func main() {
|
||||
logger.Configure(logger.LoggersConfig{
|
||||
Categories: map[string]logger.LogConfig{
|
||||
"default": {
|
||||
|
|
Loading…
Reference in New Issue