diff options
author | Arne Juul <arnej@yahooinc.com> | 2022-11-14 14:40:02 +0000 |
---|---|---|
committer | Arne Juul <arnej@yahooinc.com> | 2022-11-14 14:47:42 +0000 |
commit | 7b063026d3ab58c0faf5cb775ee5ef68d61493ba (patch) | |
tree | b3ec5f4ff5f985f99c00e222e6fdc61e38900585 /client | |
parent | 38be56d69183fc5356f8ad2cb1972fc386acc99c (diff) |
use vespa log format for trace logging
Diffstat (limited to 'client')
-rw-r--r-- | client/go/trace/log.go | 54 | ||||
-rw-r--r-- | client/go/trace/trace.go | 22 |
2 files changed, 64 insertions, 12 deletions
diff --git a/client/go/trace/log.go b/client/go/trace/log.go new file mode 100644 index 00000000000..ba81d38acf6 --- /dev/null +++ b/client/go/trace/log.go @@ -0,0 +1,54 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +// Author: arnej + +// handling of informational output +package trace + +import ( + "fmt" + "os" + "strings" + "time" +) + +// make a vespa-format log line + +func logMessage(l outputLevel, msg string) { + out := os.Stderr + unixTime := float64(time.Now().UnixMicro()) * 1.0e-6 + hostname := os.Getenv("VESPA_HOSTNAME") + pid := os.Getpid() + service := os.Getenv("VESPA_SERVICE_NAME") + component := "stderr" + level := "error" + switch l { + case levelWarning: + level = "warning" + case levelInfo: + level = "info" + case levelTrace: + level = "info" + msg = fmt.Sprintf("[trace] %s", msg) + case levelDebug: + level = "debug" + case levelSpam: + level = "spam" + } + if !strings.HasSuffix(msg, "\n") { + msg = msg + "\n" + } + fmt.Fprintf(out, "%.6f\t%s\t%d\t%s\t%s\t%s\t%s", + unixTime, hostname, pid, service, component, level, msg) +} + +func LogInfo(msg string) { + logMessage(levelInfo, msg) +} + +func LogDebug(msg string) { + logMessage(levelDebug, msg) +} + +func LogWarning(msg string) { + logMessage(levelWarning, msg) +} diff --git a/client/go/trace/trace.go b/client/go/trace/trace.go index 9f642b79562..31eeeca841c 100644 --- a/client/go/trace/trace.go +++ b/client/go/trace/trace.go @@ -1,19 +1,18 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. // Author: arnej +// handling of informational output package trace import ( "fmt" - "os" ) -// handling of informational output - type outputLevel int const ( - levelWarning outputLevel = iota - 1 + levelError outputLevel = iota - 2 + levelWarning levelNone levelInfo levelTrace @@ -31,27 +30,26 @@ func Silent() { currentOutputLevel = levelNone } -func outputTracing(l outputLevel, n string, v ...interface{}) { +func outputTracing(l outputLevel, v ...interface{}) { if l > currentOutputLevel { return } - out := os.Stderr - fmt.Fprintf(out, "%s\t", n) - fmt.Fprintln(out, v...) + msg := fmt.Sprintln(v...) + logMessage(l, msg) } func Info(v ...interface{}) { - outputTracing(levelInfo, "info", v...) + outputTracing(levelInfo, v...) } func Trace(v ...interface{}) { - outputTracing(levelTrace, "info", v...) + outputTracing(levelTrace, v...) } func Debug(v ...interface{}) { - outputTracing(levelDebug, "debug", v...) + outputTracing(levelDebug, v...) } func Warning(v ...interface{}) { - outputTracing(levelWarning, "warning", v...) + outputTracing(levelWarning, v...) } |