Skip to content
This repository has been archived by the owner on Mar 27, 2024. It is now read-only.

Commit

Permalink
logger: skip time when logging to journal (#1427)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyam8 authored Nov 22, 2023
1 parent b0fbc2e commit d8ecb75
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
3 changes: 3 additions & 0 deletions logger/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ func newTextHandler() slog.Handler {
return slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
Level: Level.lvl,
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
if a.Key == slog.TimeKey && isJournal {
return slog.Attr{}
}
if a.Key == slog.LevelKey {
v := a.Value.Any().(slog.Level)
a.Value = slog.StringValue(strings.ToLower(v.String()))
Expand Down
26 changes: 26 additions & 0 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import (
"fmt"
"log/slog"
"os"
"strconv"
"strings"
"sync/atomic"
"syscall"

"github.com/netdata/go.d.plugin/agent/executable"

Expand All @@ -16,6 +19,8 @@ import (

var isTerm = isatty.IsTerminal(os.Stderr.Fd())

var isJournal = isStderrConnectedToJournal()

var pluginAttr = slog.String("plugin", executable.Name)

func New() *Logger {
Expand Down Expand Up @@ -74,3 +79,24 @@ func (l *Logger) mute(v bool) {
func (l *Logger) isNil() bool { return l == nil || l.sl == nil }

var nilLogger = New()

func isStderrConnectedToJournal() bool {
stream := os.Getenv("JOURNAL_STREAM")
if stream == "" {
return false
}

idx := strings.IndexByte(stream, ':')
if idx <= 0 {
return false
}

dev, ino := stream[:idx], stream[idx+1:]

var stat syscall.Stat_t
if err := syscall.Fstat(int(os.Stderr.Fd()), &stat); err != nil {
return false
}

return dev == strconv.Itoa(int(stat.Dev)) && ino == strconv.FormatUint(stat.Ino, 10)
}

0 comments on commit d8ecb75

Please sign in to comment.