This commit is contained in:
Eugene Marushchenko 2025-12-19 14:47:45 +01:00 committed by GitHub
commit f5a757be1f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 49 additions and 4 deletions

View file

@ -73,6 +73,8 @@ components:
type: array
items:
type: string
structuredLog:
type: boolean
logFile:
type: string
sysLogPrefix:

View file

@ -152,6 +152,7 @@ type Conf struct {
// General
LogLevel LogLevel `json:"logLevel"`
LogDestinations LogDestinations `json:"logDestinations"`
StructuredLog bool `json:"structuredLog"`
LogFile string `json:"logFile"`
SysLogPrefix string `json:"sysLogPrefix"`
ReadTimeout Duration `json:"readTimeout"`
@ -318,6 +319,7 @@ func (conf *Conf) setDefaults() {
// General
conf.LogLevel = LogLevel(logger.Info)
conf.LogDestinations = LogDestinations{logger.DestinationStdout}
conf.StructuredLog = false
conf.LogFile = "mediamtx.log"
conf.SysLogPrefix = "mediamtx"
conf.ReadTimeout = 10 * Duration(time.Second)

View file

@ -157,7 +157,7 @@ func New(args []string) (*Core, bool) {
done: make(chan struct{}),
}
tempLogger, _ := logger.New(logger.Warn, []logger.Destination{logger.DestinationStdout}, "", "")
tempLogger, _ := logger.New(logger.Warn, []logger.Destination{logger.DestinationStdout}, "", "", false)
confPaths := append([]string(nil), defaultConfPaths...)
if runtime.GOOS != "windows" {
@ -268,6 +268,7 @@ func (p *Core) createResources(initial bool) error {
p.conf.LogDestinations,
p.conf.LogFile,
p.conf.SysLogPrefix,
p.conf.StructuredLog,
)
if err != nil {
return err
@ -691,7 +692,8 @@ func (p *Core) closeResources(newConf *conf.Conf, calledByAPI bool) {
newConf.LogLevel != p.conf.LogLevel ||
!reflect.DeepEqual(newConf.LogDestinations, p.conf.LogDestinations) ||
newConf.LogFile != p.conf.LogFile ||
newConf.SysLogPrefix != p.conf.SysLogPrefix
newConf.SysLogPrefix != p.conf.SysLogPrefix ||
newConf.StructuredLog != p.conf.StructuredLog
closeAuthManager := newConf == nil ||
newConf.AuthMethod != p.conf.AuthMethod ||

View file

@ -0,0 +1,33 @@
package logger
import (
"bytes"
"fmt"
"os"
"strconv"
"time"
)
type destinationStdoutStructured struct {
buf bytes.Buffer
}
func newDestionationStdoutStructured() destination {
return &destinationStdoutStructured{}
}
func (d *destinationStdoutStructured) log(t time.Time, level Level, format string, args ...any) {
d.buf.Reset()
d.buf.WriteString(`{"ts":"`)
writeTime(&d.buf, t, false)
d.buf.WriteString(`","level":"`)
writeLevel(&d.buf, level, false)
d.buf.WriteString(`","msg":"`)
d.buf.WriteString(strconv.Quote(fmt.Sprintf(format, args...)))
d.buf.WriteString(`"}`)
d.buf.WriteByte('\n')
os.Stdout.Write(d.buf.Bytes()) //nolint:errcheck
}
func (d *destinationStdoutStructured) close() {
}

View file

@ -19,7 +19,7 @@ type Logger struct {
}
// New allocates a log handler.
func New(level Level, destinations []Destination, filePath string, sysLogPrefix string) (*Logger, error) {
func New(level Level, destinations []Destination, filePath string, sysLogPrefix string, structuredLog bool) (*Logger, error) {
lh := &Logger{
level: level,
}
@ -27,7 +27,11 @@ func New(level Level, destinations []Destination, filePath string, sysLogPrefix
for _, destType := range destinations {
switch destType {
case DestinationStdout:
if structuredLog {
lh.destinations = append(lh.destinations, newDestionationStdoutStructured())
} else {
lh.destinations = append(lh.destinations, newDestionationStdout())
}
case DestinationFile:
dest, err := newDestinationFile(filePath)

View file

@ -10,6 +10,8 @@
logLevel: info
# Destinations of log messages; available values are "stdout", "file" and "syslog".
logDestinations: [stdout]
# If "stdout" in logDestination, use json for logs.
structuredLog: no
# If "file" is in logDestinations, this is the file which will receive the logs.
logFile: mediamtx.log
# If "syslog" is in logDestinations, use prefix for logs.