mirror of
https://github.com/owncast/owncast.git
synced 2025-11-02 03:54:54 +08:00
Save owncast logs to log file. Closes #908
This commit is contained in:
@ -7,7 +7,10 @@ import (
|
||||
"math"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
|
||||
"github.com/rifflock/lfshook"
|
||||
"github.com/sirupsen/logrus"
|
||||
logger "github.com/sirupsen/logrus"
|
||||
)
|
||||
@ -21,14 +24,50 @@ type OCLogger struct {
|
||||
}
|
||||
|
||||
var Logger *OCLogger
|
||||
var _level logrus.Level
|
||||
|
||||
// Setup configures our custom logging destinations.
|
||||
func Setup() {
|
||||
logger.SetOutput(os.Stdout) // Send all logs to console
|
||||
func Setup(enableDebugOptions bool, enableVerboseLogging bool) {
|
||||
// Write logs to a file
|
||||
path := getLogFilePath()
|
||||
writer, _ := rotatelogs.New(
|
||||
path+".%Y%m%d%H%M",
|
||||
rotatelogs.WithLinkName(path),
|
||||
rotatelogs.WithMaxAge(time.Duration(86400)*time.Second),
|
||||
rotatelogs.WithRotationTime(time.Duration(604800)*time.Second),
|
||||
)
|
||||
|
||||
logMapping := lfshook.WriterMap{
|
||||
logrus.InfoLevel: writer,
|
||||
logrus.DebugLevel: writer,
|
||||
logrus.TraceLevel: writer,
|
||||
logrus.WarnLevel: writer,
|
||||
logrus.ErrorLevel: writer,
|
||||
logrus.FatalLevel: writer,
|
||||
}
|
||||
|
||||
logger.AddHook(lfshook.NewHook(
|
||||
logMapping,
|
||||
&logger.TextFormatter{},
|
||||
))
|
||||
|
||||
if enableVerboseLogging {
|
||||
logrus.SetLevel(logrus.TraceLevel)
|
||||
} else {
|
||||
logrus.SetLevel(logrus.InfoLevel)
|
||||
}
|
||||
|
||||
// Write to stdout console
|
||||
logger.SetOutput(os.Stdout)
|
||||
|
||||
// Write to our custom logging hook for the log API
|
||||
_logger := new(OCLogger)
|
||||
logger.AddHook(_logger)
|
||||
|
||||
if enableDebugOptions {
|
||||
logrus.SetReportCaller(true)
|
||||
}
|
||||
|
||||
Logger = _logger
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user