diff --git a/filelog.go b/filelog.go index adbc1f7..f64888f 100644 --- a/filelog.go +++ b/filelog.go @@ -47,7 +47,7 @@ func (w *FileLogWriter) LogWrite(rec *LogRecord) { func (w *FileLogWriter) Close() { close(w.rec) - w.file.Sync() + w.file.Sync() //The file can be closed properly, this is not necessary } // NewFileLogWriter creates a new LogWriter which writes to the given file and @@ -81,6 +81,7 @@ func NewFileLogWriter(fname string, rotate bool) *FileLogWriter { fmt.Fprint(w.file, FormatLogRecord(w.trailer, &LogRecord{Created: time.Now()})) w.file.Close() } + wg.Done() }() for { diff --git a/log4go.go b/log4go.go index 822e890..be1988e 100644 --- a/log4go.go +++ b/log4go.go @@ -52,6 +52,7 @@ import ( "runtime" "strings" "time" + "sync" ) // Version information @@ -81,6 +82,7 @@ const ( // Logging level strings var ( levelStrings = [...]string{"FNST", "FINE", "DEBG", "TRAC", "INFO", "WARN", "EROR", "CRIT"} + wg sync.WaitGroup ) func (l Level) String() string { @@ -165,10 +167,12 @@ func NewDefaultLogger(lvl Level) Logger { // all filters (and thus all LogWriters) from the logger. func (log Logger) Close() { // Close all open loggers + wg.Add(len(log)) for name, filt := range log { filt.Close() delete(log, name) } + wg.Wait() } // Add a new LogWriter to the Logger which will only log messages at lvl or diff --git a/pattlog.go b/pattlog.go index 72faf91..537f70b 100644 --- a/pattlog.go +++ b/pattlog.go @@ -123,6 +123,7 @@ func (w FormatLogWriter) run(out io.Writer, format string) { for rec := range w { fmt.Fprint(out, FormatLogRecord(format, rec)) } + wg.Done() } // This is the FormatLogWriter's output method. This will block if the output diff --git a/socklog.go b/socklog.go index 1d224a9..0d64827 100644 --- a/socklog.go +++ b/socklog.go @@ -35,6 +35,7 @@ func NewSocketLogWriter(proto, hostport string) SocketLogWriter { if sock != nil && proto == "tcp" { sock.Close() } + wg.Done() }() for rec := range w { diff --git a/termlog.go b/termlog.go index 8a941e2..330d44e 100644 --- a/termlog.go +++ b/termlog.go @@ -33,6 +33,7 @@ func (c *ConsoleLogWriter) run(out io.Writer) { for rec := range c.w { fmt.Fprint(out, FormatLogRecord(c.format, rec)) } + wg.Done() } // This is the ConsoleLogWriter's output method. This will block if the output @@ -45,5 +46,4 @@ func (c *ConsoleLogWriter) LogWrite(rec *LogRecord) { // send log messages to this logger after a Close have undefined behavior. func (c *ConsoleLogWriter) Close() { close(c.w) - time.Sleep(50 * time.Millisecond) // Try to give console I/O time to complete }