Logging: Add output writer wrapper so the default logger can use Logrus

Signed-off-by: Michael Mayer <michael@photoprism.app>
This commit is contained in:
Michael Mayer 2022-12-23 01:35:59 +01:00
parent 709683ef59
commit cab5efba36
3 changed files with 43 additions and 0 deletions

View file

@ -1,6 +1,7 @@
package event
import (
"log"
"os"
"github.com/sirupsen/logrus"
@ -24,6 +25,13 @@ func init() {
ReportCaller: false,
}
// Default logger shall use Logrus.
log.SetFlags(0)
log.SetOutput(&LogWriter{
Log: Log,
Level: logrus.DebugLevel,
})
// Create dummy audit logger.
AuditLog = dummy.NewLogger()
}

View file

@ -0,0 +1,24 @@
package event
import "github.com/sirupsen/logrus"
// LogWriter is an output writer wrapper for using Logrus with the standard logger.
type LogWriter struct {
Log Logger
Level logrus.Level
}
// Write implements io.Writer.
func (w *LogWriter) Write(b []byte) (int, error) {
n := len(b)
if n > 0 && b[n-1] == '\n' {
b = b[:n-1]
}
if w.Log != nil {
w.Log.Log(w.Level, string(b))
}
return n, nil
}

View file

@ -0,0 +1,11 @@
package event
import (
"log"
"testing"
)
func TestLogWriter(t *testing.T) {
l := log.Default()
l.Println("Test 123")
}