mirror of
https://github.com/tailscale/tailscale.git
synced 2025-12-08 22:12:21 +00:00
types/logger: add adapters for Logf to std Logger/Writer
This commit is contained in:
@@ -7,6 +7,11 @@
|
||||
// types around.
|
||||
package logger
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log"
|
||||
)
|
||||
|
||||
// Logf is the basic Tailscale logger type: a printf-like func.
|
||||
type Logf func(format string, args ...interface{})
|
||||
|
||||
@@ -16,3 +21,20 @@ func WithPrefix(f Logf, prefix string) Logf {
|
||||
f(prefix+format, args...)
|
||||
}
|
||||
}
|
||||
|
||||
// FuncWriter returns an io.Writer that writes to f.
|
||||
func FuncWriter(f Logf) io.Writer {
|
||||
return funcWriter{f}
|
||||
}
|
||||
|
||||
// StdLogger returns a standard library logger from a Logf.
|
||||
func StdLogger(f Logf) *log.Logger {
|
||||
return log.New(FuncWriter(f), "", 0)
|
||||
}
|
||||
|
||||
type funcWriter struct{ f Logf }
|
||||
|
||||
func (w funcWriter) Write(p []byte) (int, error) {
|
||||
w.f("%s", p)
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user