Compare commits

..

No commits in common. "20e440f79ddd4ad00fef2e0fe9e98e7e0fe85b5c" and "4811fbbbe528c77b9b448184d503258be8c4ba8d" have entirely different histories.

6 changed files with 26 additions and 68 deletions

2
.vscode/launch.json vendored
View File

@ -9,7 +9,7 @@
"type": "go", "type": "go",
"request": "launch", "request": "launch",
"mode": "auto", "mode": "auto",
"program": "${workspaceFolder}/test", "program": "${fileDirname}",
"cwd": "${workspaceFolder}", "cwd": "${workspaceFolder}",
} }
] ]

View File

@ -35,9 +35,9 @@ func (c *ConsoleAppender) Append(logEvent LogEvent) {
fmt.Printf(TraceTemplate, logMsg) fmt.Printf(TraceTemplate, logMsg)
} }
} }
func makeConsoleAppender(appenderConfig LogAppenderConfig) *LoggerAppender { func makeConsoleAppender(appenderConfig LogAppenderConfig) LoggerAppender {
var appender LoggerAppender = &ConsoleAppender{} var appender LoggerAppender = &ConsoleAppender{}
return &appender return appender
} }
func init() { func init() {
RegistAppender("console", makeConsoleAppender) RegistAppender("console", makeConsoleAppender)

20
file.go
View File

@ -7,7 +7,6 @@ import (
type FileAppender struct { type FileAppender struct {
filePath string filePath string
lchan chan LogEvent
file *os.File file *os.File
} }
@ -15,8 +14,7 @@ func (f *FileAppender) GetName() string {
return "FileAppender:" + f.filePath return "FileAppender:" + f.filePath
} }
func (f *FileAppender) start() { func (f *FileAppender) Append(logEvent LogEvent) {
f.lchan = make(chan LogEvent, 10)
if f.file == nil || int(f.file.Fd()) == -1 { if f.file == nil || int(f.file.Fd()) == -1 {
dirName := filepath.Dir(f.filePath) dirName := filepath.Dir(f.filePath)
_, err := os.Stat(dirName) _, err := os.Stat(dirName)
@ -26,21 +24,12 @@ func (f *FileAppender) start() {
f.file, _ = os.OpenFile(f.filePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) f.file, _ = os.OpenFile(f.filePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
} }
go func() {
defer f.file.Close()
for {
logEvent := <-f.lchan
logMsg := format(logEvent) logMsg := format(logEvent)
f.file.WriteString(logMsg) 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 logfile interface{}
var ok bool var ok bool
logfile, ok = appenderConfig.Options["file"] logfile, ok = appenderConfig.Options["file"]
@ -50,10 +39,7 @@ func makeFileAppender(appenderConfig LogAppenderConfig) *LoggerAppender {
var ret LoggerAppender = &FileAppender{ var ret LoggerAppender = &FileAppender{
filePath: logfile.(string), filePath: logfile.(string),
} }
return ret
ret.(*FileAppender).start()
return &ret
} }
func init() { func init() {

37
main.go
View File

@ -22,9 +22,9 @@ var logLevelMap map[string]int = map[string]int{
"debug": Debug, "debug": Debug,
"trace": Trace, "trace": Trace,
} }
var loggerMap map[string]*Logger = map[string]*Logger{} var loggerMap map[string]Logger = map[string]Logger{}
var appenderFactoryMap map[string]func(LogAppenderConfig) *LoggerAppender = map[string]func(LogAppenderConfig) *LoggerAppender{} var appenderFactoryMap map[string]func(LogAppenderConfig) LoggerAppender = map[string]func(LogAppenderConfig) LoggerAppender{}
var appenders map[string]*LoggerAppender = map[string]*LoggerAppender{} var appenders map[string]LoggerAppender = map[string]LoggerAppender{}
var loggerConfig LoggersConfig var loggerConfig LoggersConfig
type LogAppenderConfig struct { type LogAppenderConfig struct {
@ -43,7 +43,7 @@ type LoggersConfig struct {
type Logger struct { type Logger struct {
category string category string
level int level int
appenders []*LoggerAppender appenders []LoggerAppender
} }
type LogEvent struct { type LogEvent struct {
@ -62,7 +62,7 @@ var consoleAppender LoggerAppender = &ConsoleAppender{}
var defaultLogger = &Logger{ var defaultLogger = &Logger{
level: Error, level: Error,
appenders: []*LoggerAppender{&consoleAppender}, appenders: []LoggerAppender{consoleAppender},
} }
func (l *Logger) log(Level int, msg []interface{}) { func (l *Logger) log(Level int, msg []interface{}) {
@ -71,7 +71,7 @@ func (l *Logger) log(Level int, msg []interface{}) {
now := time.Now() now := time.Now()
logEvent := LogEvent{l.category, now, Level, msg} logEvent := LogEvent{l.category, now, Level, msg}
for _, appender := range l.appenders { for _, appender := range l.appenders {
(*appender).Append(logEvent) appender.Append(logEvent)
} }
// l.Appender.Append(logEvent) // l.Appender.Append(logEvent)
// fmt.Println(now.Format("2006-01-02 15:04:05"), " ", l.Name, ": ", msg) // fmt.Println(now.Format("2006-01-02 15:04:05"), " ", l.Name, ": ", msg)
@ -97,7 +97,7 @@ func (l *Logger) Debug(msg ...interface{}) {
func (l *Logger) Trace(msg ...interface{}) { func (l *Logger) Trace(msg ...interface{}) {
l.log(Trace, msg) l.log(Trace, msg)
} }
func GetLogger(name string) *Logger { func GetLogger(name string) Logger {
if logger, ok := loggerMap[name]; ok { if logger, ok := loggerMap[name]; ok {
return logger return logger
} else { } else {
@ -106,30 +106,29 @@ func GetLogger(name string) *Logger {
return makeLogger(name, logConfig) return makeLogger(name, logConfig)
} }
if name == "default" { if name == "default" {
return defaultLogger return *defaultLogger
} }
l := *GetLogger("default") ret := GetLogger("default")
l.category = name ret.category = name
loggerMap[name] = &l return ret
return &l
} }
} }
func makeLogger(name string, config LogConfig) *Logger { func makeLogger(name string, config LogConfig) Logger {
logger := &Logger{category: name} logger := &Logger{category: name}
levelstr := strings.ToLower(config.Level) levelstr := strings.ToLower(config.Level)
logger.level = logLevelMap[levelstr] logger.level = logLevelMap[levelstr]
if config.Appenders == nil || len(config.Appenders) == 0 { if config.Appenders == nil || len(config.Appenders) == 0 {
logger.appenders = []*LoggerAppender{&consoleAppender} logger.appenders = []LoggerAppender{consoleAppender}
} else { } else {
logger.appenders = make([]*LoggerAppender, len(config.Appenders)) logger.appenders = make([]LoggerAppender, len(config.Appenders))
for i, appenderName := range config.Appenders { for i, appenderName := range config.Appenders {
logger.appenders[i] = appenders[appenderName] logger.appenders[i] = appenders[appenderName]
} }
} }
loggerMap[name] = logger loggerMap[name] = *logger
return logger return *logger
} }
func Configure(config LoggersConfig) { func Configure(config LoggersConfig) {
loggerConfig = config loggerConfig = config
@ -138,7 +137,7 @@ func Configure(config LoggersConfig) {
if ok { if ok {
appenders[name] = appenderFactory(appenderConfig) appenders[name] = appenderFactory(appenderConfig)
} else { } else {
appenders[name] = &consoleAppender appenders[name] = &ConsoleAppender{}
} }
} }
for name, _ := range loggerConfig.Categories { for name, _ := range loggerConfig.Categories {
@ -146,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 appenderFactoryMap[typeName] = appenderCreatCb
} }

View File

@ -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")
}
}

View File

@ -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 {}
} }