fix dead loop
This commit is contained in:
parent
20e440f79d
commit
6d89afc6e3
|
@ -15,6 +15,11 @@ const (
|
||||||
type ConsoleAppender struct {
|
type ConsoleAppender struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Close implements LoggerAppender.
|
||||||
|
func (c *ConsoleAppender) Close() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func (c *ConsoleAppender) GetName() string {
|
func (c *ConsoleAppender) GetName() string {
|
||||||
return "console"
|
return "console"
|
||||||
}
|
}
|
||||||
|
|
15
file.go
15
file.go
|
@ -9,6 +9,13 @@ type FileAppender struct {
|
||||||
filePath string
|
filePath string
|
||||||
lchan chan LogEvent
|
lchan chan LogEvent
|
||||||
file *os.File
|
file *os.File
|
||||||
|
stopChan chan struct{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close implements LoggerAppender.
|
||||||
|
func (f *FileAppender) Close() {
|
||||||
|
//send stop signal
|
||||||
|
f.stopChan <- struct{}{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *FileAppender) GetName() string {
|
func (f *FileAppender) GetName() string {
|
||||||
|
@ -17,6 +24,7 @@ func (f *FileAppender) GetName() string {
|
||||||
|
|
||||||
func (f *FileAppender) start() {
|
func (f *FileAppender) start() {
|
||||||
f.lchan = make(chan LogEvent, 10)
|
f.lchan = make(chan LogEvent, 10)
|
||||||
|
f.stopChan = make(chan struct{})
|
||||||
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)
|
||||||
|
@ -29,10 +37,15 @@ func (f *FileAppender) start() {
|
||||||
go func() {
|
go func() {
|
||||||
defer f.file.Close()
|
defer f.file.Close()
|
||||||
for {
|
for {
|
||||||
logEvent := <-f.lchan
|
select {
|
||||||
|
case <-f.stopChan:
|
||||||
|
return
|
||||||
|
case logEvent := <-f.lchan:
|
||||||
logMsg := format(logEvent)
|
logMsg := format(logEvent)
|
||||||
f.file.WriteString(logMsg)
|
f.file.WriteString(logMsg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
func (f *FileAppender) Append(logEvent LogEvent) {
|
func (f *FileAppender) Append(logEvent LogEvent) {
|
||||||
|
|
1
main.go
1
main.go
|
@ -56,6 +56,7 @@ type LogEvent struct {
|
||||||
type LoggerAppender interface {
|
type LoggerAppender interface {
|
||||||
GetName() string
|
GetName() string
|
||||||
Append(logEvent LogEvent)
|
Append(logEvent LogEvent)
|
||||||
|
Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
var consoleAppender LoggerAppender = &ConsoleAppender{}
|
var consoleAppender LoggerAppender = &ConsoleAppender{}
|
||||||
|
|
Loading…
Reference in New Issue