mediamtx/internal/logger/destination_file.go
Eugene Marushchenko 9e9fae9a10
Some checks are pending
code_lint / go (push) Waiting to run
code_lint / go_mod (push) Waiting to run
code_lint / docs (push) Waiting to run
code_lint / api_docs (push) Waiting to run
code_test / test_64 (push) Waiting to run
code_test / test_32 (push) Waiting to run
code_test / test_e2e (push) Waiting to run
add structured logging (#5219)
Co-authored-by: aler9 <46489434+aler9@users.noreply.github.com>
2025-12-27 20:42:06 +01:00

54 lines
1.1 KiB
Go

package logger
import (
"bytes"
"fmt"
"os"
"strconv"
"time"
)
type destinationFile struct {
structured bool
file *os.File
buf bytes.Buffer
}
func newDestinationFile(structured bool, filePath string) (destination, error) {
f, err := os.OpenFile(filePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil {
return nil, err
}
return &destinationFile{
structured: structured,
file: f,
}, nil
}
func (d *destinationFile) log(t time.Time, level Level, format string, args ...any) {
d.buf.Reset()
if d.structured {
d.buf.WriteString(`{"timestamp":"`)
d.buf.WriteString(t.Format(time.RFC3339))
d.buf.WriteString(`","level":"`)
writeLevel(&d.buf, level, false)
d.buf.WriteString(`","message":`)
d.buf.WriteString(strconv.Quote(fmt.Sprintf(format, args...)))
d.buf.WriteString(`}`)
d.buf.WriteByte('\n')
} else {
writePlainTime(&d.buf, t, false)
writeLevel(&d.buf, level, false)
d.buf.WriteByte(' ')
fmt.Fprintf(&d.buf, format, args...)
d.buf.WriteByte('\n')
}
d.file.Write(d.buf.Bytes()) //nolint:errcheck
}
func (d *destinationFile) close() {
d.file.Close()
}