summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/mattermost/logr/v2/stdlogger.go
diff options
context:
space:
mode:
authorWim <wim@42.be>2021-10-16 23:11:32 +0200
committerWim <wim@42.be>2021-10-16 23:23:24 +0200
commit20f6c05ec50739d31f4dbe9fde0d223f2c43f6e8 (patch)
tree230edca06449a8d1755f08aabf45a03e07e6f17c /vendor/github.com/mattermost/logr/v2/stdlogger.go
parent57fce93af7f64f025cec6f3ed6088163086bc9fe (diff)
downloadmatterbridge-msglm-20f6c05ec50739d31f4dbe9fde0d223f2c43f6e8.tar.gz
matterbridge-msglm-20f6c05ec50739d31f4dbe9fde0d223f2c43f6e8.tar.bz2
matterbridge-msglm-20f6c05ec50739d31f4dbe9fde0d223f2c43f6e8.zip
Update vendor
Diffstat (limited to 'vendor/github.com/mattermost/logr/v2/stdlogger.go')
-rw-r--r--vendor/github.com/mattermost/logr/v2/stdlogger.go56
1 files changed, 56 insertions, 0 deletions
diff --git a/vendor/github.com/mattermost/logr/v2/stdlogger.go b/vendor/github.com/mattermost/logr/v2/stdlogger.go
new file mode 100644
index 00000000..50171b3d
--- /dev/null
+++ b/vendor/github.com/mattermost/logr/v2/stdlogger.go
@@ -0,0 +1,56 @@
+package logr
+
+import (
+ "log"
+ "os"
+ "strings"
+)
+
+// NewStdLogger creates a standard logger backed by a Logr instance.
+// All log records are emitted with the specified log level.
+func NewStdLogger(level Level, logger Logger) *log.Logger {
+ adapter := newStdLogAdapter(logger, level)
+ return log.New(adapter, "", 0)
+}
+
+// RedirectStdLog redirects output from the standard library's package-global logger
+// to this logger at the specified level and with zero or more Field's. Since Logr already
+// handles caller annotations, timestamps, etc., it automatically disables the standard
+// library's annotations and prefixing.
+// A function is returned that restores the original prefix and flags and resets the standard
+// library's output to os.Stderr.
+func (lgr *Logr) RedirectStdLog(level Level, fields ...Field) func() {
+ flags := log.Flags()
+ prefix := log.Prefix()
+ log.SetFlags(0)
+ log.SetPrefix("")
+
+ logger := lgr.NewLogger().With(fields...)
+ adapter := newStdLogAdapter(logger, level)
+ log.SetOutput(adapter)
+
+ return func() {
+ log.SetFlags(flags)
+ log.SetPrefix(prefix)
+ log.SetOutput(os.Stderr)
+ }
+}
+
+type stdLogAdapter struct {
+ logger Logger
+ level Level
+}
+
+func newStdLogAdapter(logger Logger, level Level) *stdLogAdapter {
+ return &stdLogAdapter{
+ logger: logger,
+ level: level,
+ }
+}
+
+// Write implements io.Writer
+func (a *stdLogAdapter) Write(p []byte) (int, error) {
+ s := strings.TrimSpace(string(p))
+ a.logger.Log(a.level, s)
+ return len(p), nil
+}