mirror of
https://github.com/bluenviron/mediamtx.git
synced 2026-01-24 12:29:50 -08:00
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
Co-authored-by: aler9 <46489434+aler9@users.noreply.github.com>
54 lines
1.1 KiB
Go
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()
|
|
}
|