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:
parent
709683ef59
commit
cab5efba36
3 changed files with 43 additions and 0 deletions
|
@ -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()
|
||||
}
|
||||
|
|
24
internal/event/log_writer.go
Normal file
24
internal/event/log_writer.go
Normal 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
|
||||
}
|
11
internal/event/log_writer_test.go
Normal file
11
internal/event/log_writer_test.go
Normal file
|
@ -0,0 +1,11 @@
|
|||
package event
|
||||
|
||||
import (
|
||||
"log"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestLogWriter(t *testing.T) {
|
||||
l := log.Default()
|
||||
l.Println("Test 123")
|
||||
}
|
Loading…
Reference in a new issue