Skip to content

Commit

Permalink
pkg/newlog: suppress syslog/kernel logging using the log level settings
Browse files Browse the repository at this point in the history
Patch uses log level settings for the syslog and kernel introduced
in the previous patch in pillar. Syslog /dev/log and kernel /dev/kmsg
ring buffer are always populated with the log messages regardless
of specified log level. The task of newlogd filter out log messages
which log levels higher than specified in configuration settings.

The default log level set is "info".

Signed-off-by: Roman Penyaev <r.peniaev@gmail.com>
  • Loading branch information
rouming committed Apr 15, 2024
1 parent abda93c commit cd79f9c
Showing 1 changed file with 43 additions and 5 deletions.
48 changes: 43 additions & 5 deletions pkg/newlog/cmd/newlogd.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"sort"
"strconv"
"strings"
"sync/atomic"
"syscall"
"time"
"unicode"
Expand Down Expand Up @@ -103,8 +104,12 @@ var (

//domainUUID
domainUUID *base.LockedStringMap // App log, from domain-id to appDomain
// syslog/kmsg priority string definition
priorityStr = [8]string{"emerg", "alert", "crit", "err", "warning", "notice", "info", "debug"}
// Default log levels for some subsystems. Variables are updated and used
// from different goroutines, so in order to push the changes out of the
// goroutines local caches and correctly observe changed values in another
// goroutine sync/atomic synchronization is used. You've been warned.
syslogPrio = types.SyslogKernelLogLevelNum[types.SyslogKernelDefaultLogLevel]
kernelPrio = types.SyslogKernelLogLevelNum[types.SyslogKernelDefaultLogLevel]
)

// for app Domain-ID mapping into UUID and DisplayName
Expand Down Expand Up @@ -154,6 +159,16 @@ type devMeta struct {
curPart string
}

// parse log level string
func parseLogLevel(loglevel string) uint32 {
prio, ok := types.SyslogKernelLogLevelNum[loglevel]
if !ok {
prio = types.SyslogKernelLogLevelNum[types.SyslogKernelDefaultLogLevel]
}

return prio
}

// newlogd program
func main() {
restartPtr := flag.Bool("r", false, "Restart")
Expand Down Expand Up @@ -499,10 +514,27 @@ func handleGlobalConfigImp(ctxArg interface{}, key string, statusArg interface{}
if limitGzipFilesMbyts > uint32(persistMbytes/10) {
limitGzipFilesMbyts = uint32(persistMbytes / 10)
}

// parse syslog log level
syslogPrioStr := gcp.GlobalValueString(types.SyslogLogLevel)
atomic.StoreUint32(&syslogPrio, parseLogLevel(syslogPrioStr))

// parse kernel log level
kernelPrioStr := gcp.GlobalValueString(types.KernelLogLevel)
atomic.StoreUint32(&kernelPrio, parseLogLevel(kernelPrioStr))
}
log.Tracef("handleGlobalConfigModify done for %s, fastupload enabled %v", key, enableFastUpload)
}

func suppressMsg(entry inputEntry, cfgPrio uint32) bool {
pri, ok := types.SyslogKernelLogLevelNum[entry.severity]
if !ok {
pri, _ = types.SyslogKernelLogLevelNum[types.SyslogKernelDefaultLogLevel]

Check failure on line 532 in pkg/newlog/cmd/newlogd.go

View workflow job for this annotation

GitHub Actions / yetus

golangcilint: S1005: unnecessary assignment to the blank identifier (gosimple)
}

return pri > cfgPrio
}

// getKmessages - goroutine to get from /dev/kmsg
func getKmessages(loggerChan chan inputEntry) {
parser, err := kmsgparser.NewParser()
Expand All @@ -515,12 +547,15 @@ func getKmessages(loggerChan chan inputEntry) {
for msg := range kmsg {
entry := inputEntry{
source: "kernel",
severity: "info",
severity: types.SyslogKernelDefaultLogLevel,
content: msg.Message,
timestamp: msg.Timestamp.Format(time.RFC3339Nano),
}
if msg.Priority >= 0 {
entry.severity = priorityStr[msg.Priority%8]
entry.severity = types.SyslogKernelLogLevelStr[msg.Priority%8]
}
if suppressMsg(entry, atomic.LoadUint32(&kernelPrio)) {
continue
}

logmetrics.NumKmessages++
Expand Down Expand Up @@ -1608,6 +1643,9 @@ func getSyslogMsg(loggerChan chan inputEntry) {
log.Error(err)
continue
}
if suppressMsg(entry, atomic.LoadUint32(&syslogPrio)) {
continue
}

logmetrics.NumSyslogMessages++
logmetrics.DevMetrics.NumInputEvent++
Expand Down Expand Up @@ -1652,7 +1690,7 @@ func newMessage(pkt []byte, size int, sysfmt *regexp.Regexp) (inputEntry, error)

msgReceived := time.Now()
p, _ := strconv.ParseInt(string(res[1]), 10, 64)
msgPriority = priorityStr[p%8]
msgPriority = types.SyslogKernelLogLevelStr[p%8]
misc := res[3]
// Check for either "hostname tagpid" or "tagpid"
a := bytes.SplitN(misc, []byte(" "), 2)
Expand Down

0 comments on commit cd79f9c

Please sign in to comment.