Skip to content

Commit f09b131

Browse files
committed
输出全部log,当调用log.close之后
1 parent bec037c commit f09b131

File tree

6 files changed

+56
-17
lines changed

6 files changed

+56
-17
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
examples/ConsoleLogWriter_Manual.exe

config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func (log Logger) LoadConfiguration(filename string) {
131131
}
132132
}
133133

134-
func xmlToConsoleLogWriter(filename string, props []xmlProperty, enabled bool) (ConsoleLogWriter, bool) {
134+
func xmlToConsoleLogWriter(filename string, props []xmlProperty, enabled bool) (*ConsoleLogWriter, bool) {
135135
// Parse properties
136136
for _, prop := range props {
137137
switch prop.Name {

examples/ConsoleLogWriter_Manual.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package main
22

33
import (
4-
"time"
4+
//"time"
55
)
66

77
import l4g "github.com/feixiao/log4go"
@@ -18,6 +18,6 @@ func main() {
1818
log.Critical("Critical")
1919
// ConsoleLogWriter 使用带缓存的chan进行输出的管理,所以不进行sleep操作
2020
// 控制台没有输出,因为我们不知道什么时候channel会进行处理
21-
time.Sleep(1*time.Second)
21+
//time.Sleep(1*time.Second)
2222
log.Close()
2323
}

filelog.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ type FileLogWriter struct {
3737

3838
// Keep old logfiles (.001, .002, etc)
3939
rotate bool
40+
41+
wg WaitGroupWrapper
4042
}
4143

4244
// This is the FileLogWriter's output method
@@ -46,6 +48,8 @@ func (w *FileLogWriter) LogWrite(rec *LogRecord) {
4648

4749
func (w *FileLogWriter) Close() {
4850
close(w.rec)
51+
w.wg.Wait()
52+
4953
}
5054

5155
// NewFileLogWriter creates a new LogWriter which writes to the given file and
@@ -72,7 +76,9 @@ func NewFileLogWriter(fname string, rotate bool) *FileLogWriter {
7276
return nil
7377
}
7478

75-
go func() {
79+
80+
81+
worker := func() {
7682
defer func() {
7783
if w.file != nil {
7884
fmt.Fprint(w.file, FormatLogRecord(w.trailer, &LogRecord{Created: time.Now()}))
@@ -113,7 +119,9 @@ func NewFileLogWriter(fname string, rotate bool) *FileLogWriter {
113119
w.maxsize_cursize += n
114120
}
115121
}
116-
}()
122+
}
123+
124+
w.wg.Wrap(worker)
117125

118126
return w
119127
}

termlog.go

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,35 @@ import (
88
"fmt"
99
)
1010

11-
var stdout io.Writer = os.Stdout
11+
1212

1313
// This is the standard writer that prints to standard output.
14-
type ConsoleLogWriter chan *LogRecord
14+
//type ConsoleLogWriter chan *LogRecord
15+
16+
type ConsoleLogWriter struct {
17+
logChan chan *LogRecord
18+
wg WaitGroupWrapper
19+
stdout io.Writer
20+
}
1521

1622
// This creates a new ConsoleLogWriter
17-
func NewConsoleLogWriter() ConsoleLogWriter {
18-
records := make(ConsoleLogWriter, LogBufferLength)
19-
go records.run(stdout)
20-
return records
23+
func NewConsoleLogWriter() *ConsoleLogWriter {
24+
w := &ConsoleLogWriter{
25+
logChan: make(chan *LogRecord, LogBufferLength),
26+
stdout: os.Stdout,
27+
}
28+
w.wg.Wrap(func(){w.run()})
29+
return w
2130
}
2231

23-
func (w ConsoleLogWriter) run(out io.Writer) {
32+
func (w *ConsoleLogWriter) run() {
33+
2434
var timestr string
2535
var timestrAt int64
2636

27-
for rec := range w {
37+
out := w.stdout
38+
39+
for rec := range w.logChan {
2840
if at := rec.Created.UnixNano() / 1e9; at != timestrAt {
2941
timestr, timestrAt = rec.Created.Format("01/02/06 15:04:05"), at
3042
}
@@ -34,12 +46,13 @@ func (w ConsoleLogWriter) run(out io.Writer) {
3446

3547
// This is the ConsoleLogWriter's output method. This will block if the output
3648
// buffer is full.
37-
func (w ConsoleLogWriter) LogWrite(rec *LogRecord) {
38-
w <- rec
49+
func (w *ConsoleLogWriter) LogWrite(rec *LogRecord) {
50+
w.logChan <- rec
3951
}
4052

4153
// Close stops the logger from sending messages to standard output. Attempts to
4254
// send log messages to this logger after a Close have undefined behavior.
43-
func (w ConsoleLogWriter) Close() {
44-
close(w)
55+
func (w *ConsoleLogWriter) Close() {
56+
close(w.logChan)
57+
w.wg.Wait()
4558
}

wait_group_wrapper.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package log4go
2+
3+
import (
4+
"sync"
5+
)
6+
7+
type WaitGroupWrapper struct {
8+
sync.WaitGroup
9+
}
10+
11+
func (w *WaitGroupWrapper) Wrap(cb func()) {
12+
w.Add(1)
13+
go func() {
14+
cb()
15+
w.Done()
16+
}()
17+
}

0 commit comments

Comments
 (0)