summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2021-08-25 01:32:42 +0700
committerShulhan <ms@kilabit.info>2021-08-25 01:32:42 +0700
commitf6ab1ba5cc81f5551d3f9acf6aed9f72f0eb996b (patch)
treee5dac79f60d0dfcff61dc8ccaae73020a94de79a
parent6465e073f836f534aaa845dfc68da96c192093fb (diff)
downloadpakakeh.go-f6ab1ba5cc81f5551d3f9acf6aed9f72f0eb996b.tar.xz
lib/mlog: implement io.Writer and add function ErrorWriter
The ErrorWriter will return the internal default MultiLogger. A call to Write() on returned io.Writer will forward it to all registered error writers. A Write method on MultiLogger write the b to all error writers. It will always return the length of b without an error.
-rw-r--r--lib/mlog/mlog.go10
-rw-r--r--lib/mlog/multi_logger.go9
2 files changed, 19 insertions, 0 deletions
diff --git a/lib/mlog/mlog.go b/lib/mlog/mlog.go
index 6626c048..db474730 100644
--- a/lib/mlog/mlog.go
+++ b/lib/mlog/mlog.go
@@ -31,6 +31,7 @@
package mlog
import (
+ "io"
"os"
)
@@ -142,3 +143,12 @@ func UnregisterErrorWriter(name string) {
func UnregisterOutputWriter(name string) {
defaultMLog.UnregisterOutputWriter(name)
}
+
+//
+// ErrorWriter return the internal default MultiLogger.
+// A call to Write() on returned io.Writer will forward it to all registered
+// error writers.
+//
+func ErrorWriter() io.Writer {
+ return defaultMLog
+}
diff --git a/lib/mlog/multi_logger.go b/lib/mlog/multi_logger.go
index 14ed0ee3..5c7f35d0 100644
--- a/lib/mlog/multi_logger.go
+++ b/lib/mlog/multi_logger.go
@@ -200,6 +200,15 @@ func (mlog *MultiLogger) UnregisterOutputWriter(name string) {
delete(mlog.outs, name)
}
+//
+// Write write the b to all error writers.
+// It will always return the length of b without an error.
+//
+func (mlog *MultiLogger) Write(b []byte) (n int, err error) {
+ mlog.qerr <- libbytes.Copy(b)
+ return len(b), nil
+}
+
func (mlog *MultiLogger) processErrorQueue() {
var err error
for {