logpolicy: don't log timestamp when running under systemd

It'll do it anyway.

Makes journalctl -fu cleaner.

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick 2020-03-25 20:40:24 -07:00 committed by Brad Fitzpatrick
parent 2d48f92a82
commit 7fb8d873ac

View File

@ -8,6 +8,7 @@
package logpolicy
import (
"bytes"
"context"
"encoding/json"
"fmt"
@ -108,6 +109,15 @@ func logsDir() string {
return ""
}
// runningUnderSystemd reports whether we're running under systemd.
func runningUnderSystemd() bool {
if runtime.GOOS == "linux" && os.Getppid() == 1 {
slurp, _ := ioutil.ReadFile("/proc/1/stat")
return bytes.HasPrefix(slurp, []byte("1 (systemd) "))
}
return false
}
// New returns a new log policy (a logger and its instance ID) for a
// given collection name.
func New(collection string) *Policy {
@ -117,6 +127,11 @@ func New(collection string) *Policy {
} else {
lflags = log.LstdFlags
}
if runningUnderSystemd() {
// If journalctl is going to prepend its own timestamp
// anyway, no need to add one.
lflags = 0
}
console := log.New(stderrWriter{}, "", lflags)
dir := logsDir()