summaryrefslogtreecommitdiffstats
path: root/client/go/trace/log.go
blob: ba81d38acf67ece09be83df1ffe886791e136111 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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)
}