mirror of
https://github.com/bluenviron/mediamtx.git
synced 2025-12-20 02:00:05 -08:00
Add structured logging
This commit is contained in:
parent
14ab95f39c
commit
16f4c9d3d0
6 changed files with 49 additions and 4 deletions
|
|
@ -63,6 +63,8 @@ components:
|
||||||
type: array
|
type: array
|
||||||
items:
|
items:
|
||||||
type: string
|
type: string
|
||||||
|
structuredLog:
|
||||||
|
type: boolean
|
||||||
logFile:
|
logFile:
|
||||||
type: string
|
type: string
|
||||||
sysLogPrefix:
|
sysLogPrefix:
|
||||||
|
|
|
||||||
|
|
@ -152,6 +152,7 @@ type Conf struct {
|
||||||
// General
|
// General
|
||||||
LogLevel LogLevel `json:"logLevel"`
|
LogLevel LogLevel `json:"logLevel"`
|
||||||
LogDestinations LogDestinations `json:"logDestinations"`
|
LogDestinations LogDestinations `json:"logDestinations"`
|
||||||
|
StructuredLog bool `json:"structuredLog"`
|
||||||
LogFile string `json:"logFile"`
|
LogFile string `json:"logFile"`
|
||||||
SysLogPrefix string `json:"sysLogPrefix"`
|
SysLogPrefix string `json:"sysLogPrefix"`
|
||||||
ReadTimeout Duration `json:"readTimeout"`
|
ReadTimeout Duration `json:"readTimeout"`
|
||||||
|
|
@ -312,6 +313,7 @@ func (conf *Conf) setDefaults() {
|
||||||
// General
|
// General
|
||||||
conf.LogLevel = LogLevel(logger.Info)
|
conf.LogLevel = LogLevel(logger.Info)
|
||||||
conf.LogDestinations = LogDestinations{logger.DestinationStdout}
|
conf.LogDestinations = LogDestinations{logger.DestinationStdout}
|
||||||
|
conf.StructuredLog = false
|
||||||
conf.LogFile = "mediamtx.log"
|
conf.LogFile = "mediamtx.log"
|
||||||
conf.SysLogPrefix = "mediamtx"
|
conf.SysLogPrefix = "mediamtx"
|
||||||
conf.ReadTimeout = 10 * Duration(time.Second)
|
conf.ReadTimeout = 10 * Duration(time.Second)
|
||||||
|
|
|
||||||
|
|
@ -156,7 +156,7 @@ func New(args []string) (*Core, bool) {
|
||||||
done: make(chan struct{}),
|
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...)
|
confPaths := append([]string(nil), defaultConfPaths...)
|
||||||
if runtime.GOOS != "windows" {
|
if runtime.GOOS != "windows" {
|
||||||
|
|
@ -267,6 +267,7 @@ func (p *Core) createResources(initial bool) error {
|
||||||
p.conf.LogDestinations,
|
p.conf.LogDestinations,
|
||||||
p.conf.LogFile,
|
p.conf.LogFile,
|
||||||
p.conf.SysLogPrefix,
|
p.conf.SysLogPrefix,
|
||||||
|
p.conf.StructuredLog,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
@ -690,7 +691,8 @@ func (p *Core) closeResources(newConf *conf.Conf, calledByAPI bool) {
|
||||||
newConf.LogLevel != p.conf.LogLevel ||
|
newConf.LogLevel != p.conf.LogLevel ||
|
||||||
!reflect.DeepEqual(newConf.LogDestinations, p.conf.LogDestinations) ||
|
!reflect.DeepEqual(newConf.LogDestinations, p.conf.LogDestinations) ||
|
||||||
newConf.LogFile != p.conf.LogFile ||
|
newConf.LogFile != p.conf.LogFile ||
|
||||||
newConf.SysLogPrefix != p.conf.SysLogPrefix
|
newConf.SysLogPrefix != p.conf.SysLogPrefix ||
|
||||||
|
newConf.StructuredLog != p.conf.StructuredLog
|
||||||
|
|
||||||
closeAuthManager := newConf == nil ||
|
closeAuthManager := newConf == nil ||
|
||||||
newConf.AuthMethod != p.conf.AuthMethod ||
|
newConf.AuthMethod != p.conf.AuthMethod ||
|
||||||
|
|
|
||||||
33
internal/logger/destination_stdout_structured.go
Normal file
33
internal/logger/destination_stdout_structured.go
Normal 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() {
|
||||||
|
}
|
||||||
|
|
@ -19,7 +19,7 @@ type Logger struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// New allocates a log handler.
|
// 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{
|
lh := &Logger{
|
||||||
level: level,
|
level: level,
|
||||||
}
|
}
|
||||||
|
|
@ -27,7 +27,11 @@ func New(level Level, destinations []Destination, filePath string, sysLogPrefix
|
||||||
for _, destType := range destinations {
|
for _, destType := range destinations {
|
||||||
switch destType {
|
switch destType {
|
||||||
case DestinationStdout:
|
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:
|
case DestinationFile:
|
||||||
dest, err := newDestinationFile(filePath)
|
dest, err := newDestinationFile(filePath)
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,8 @@
|
||||||
logLevel: info
|
logLevel: info
|
||||||
# Destinations of log messages; available values are "stdout", "file" and "syslog".
|
# Destinations of log messages; available values are "stdout", "file" and "syslog".
|
||||||
logDestinations: [stdout]
|
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.
|
# If "file" is in logDestinations, this is the file which will receive the logs.
|
||||||
logFile: mediamtx.log
|
logFile: mediamtx.log
|
||||||
# If "syslog" is in logDestinations, use prefix for logs.
|
# If "syslog" is in logDestinations, use prefix for logs.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue