Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ components:
type: array
items:
type: string
structuredLog:
type: boolean
logFile:
type: string
sysLogPrefix:
Expand Down
2 changes: 2 additions & 0 deletions internal/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -312,6 +313,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)
Expand Down
6 changes: 4 additions & 2 deletions internal/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,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" {
Expand Down Expand Up @@ -267,6 +267,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
Expand Down Expand Up @@ -690,7 +691,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 ||
Expand Down
33 changes: 33 additions & 0 deletions internal/logger/destination_stdout_structured.go
Original file line number Diff line number Diff line change
@@ -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() {
}
8 changes: 6 additions & 2 deletions internal/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,19 @@ 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,
}

for _, destType := range destinations {
switch destType {
case DestinationStdout:
lh.destinations = append(lh.destinations, newDestionationStdout())
if structuredLog {
lh.destinations = append(lh.destinations, newDestionationStdoutStructured())
} else {
lh.destinations = append(lh.destinations, newDestionationStdout())
}

case DestinationFile:
dest, err := newDestinationFile(filePath)
Expand Down
2 changes: 2 additions & 0 deletions mediamtx.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down