mirror of
https://github.com/syncthing/syncthing.git
synced 2025-12-23 14:08:06 -05:00
This adds several options for configuring the log format of timestamps
and severity levels, making it more suitable for integration with log
systems like systemd.
--log-format-timestamp="2006-01-02 15:04:05"
Format for timestamp, set to empty to disable timestamps ($STLOGFORMATTIMESTAMP)
--[no-]log-format-level-string
Whether to include level string in log line ($STLOGFORMATLEVELSTRING)
--[no-]log-format-level-syslog
Whether to include level as syslog prefix in log line ($STLOGFORMATLEVELSYSLOG)
So, to get a timestamp suitable for systemd (syslog prefix, no level
string, no timestamp) we can pass `--log-format-timestamp=""
--no-log-format-level-string --log-format-level-syslog` or,
equivalently, set `STLOGFORMATTIMESTAMP="" STLOGFORMATLEVELSTRING=false
STLOGFORMATLEVELSYSLOG=true`.
Signed-off-by: Jakob Borg <jakob@kastelo.net>
48 lines
1.1 KiB
Go
48 lines
1.1 KiB
Go
// Copyright (C) 2025 The Syncthing Authors.
|
|
//
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
package slogutil
|
|
|
|
import (
|
|
"io"
|
|
"log/slog"
|
|
"os"
|
|
"time"
|
|
)
|
|
|
|
var (
|
|
GlobalRecorder = &lineRecorder{level: -1000}
|
|
ErrorRecorder = &lineRecorder{level: slog.LevelError}
|
|
DefaultLineFormat = LineFormat{
|
|
TimestampFormat: time.DateTime,
|
|
LevelString: true,
|
|
}
|
|
globalLevels = &levelTracker{
|
|
levels: make(map[string]slog.Level),
|
|
descrs: make(map[string]string),
|
|
}
|
|
globalFormatter = &formattingOptions{
|
|
LineFormat: DefaultLineFormat,
|
|
recs: []*lineRecorder{GlobalRecorder, ErrorRecorder},
|
|
out: logWriter(),
|
|
}
|
|
slogDef = slog.New(&formattingHandler{opts: globalFormatter})
|
|
)
|
|
|
|
func logWriter() io.Writer {
|
|
if os.Getenv("LOGGER_DISCARD") != "" {
|
|
// Hack to completely disable logging, for example when running
|
|
// benchmarks.
|
|
return io.Discard
|
|
}
|
|
|
|
return os.Stdout
|
|
}
|
|
|
|
func init() {
|
|
slog.SetDefault(slogDef)
|
|
}
|