-
Notifications
You must be signed in to change notification settings - Fork 11
/
logger.go
94 lines (83 loc) · 2.51 KB
/
logger.go
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package j8a
import (
"crypto/md5"
"encoding/hex"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"os"
"strings"
)
const dwnReqRemoteAddr = "dwnReqRemoteAddr"
const dwnReqPort = "dwnReqPort"
const dwnReqPath = "dwnReqPath"
const dwnReqHost = "dwnReqHost"
const dwnReqMethod = "dwnReqMethod"
const dwnReqUserAgent = "dwnReqUserAgent"
const dwnReqHttpVer = "dwnReqHttpVer"
const dwnReqTlsVer = "dwnReqTlsVer"
const dwnReqListnr = "dwnReqListnr"
const upBytesRead = "upBytesRead"
const upBytesWrite = "upBytesWrite"
const dwnElpsdMicros = "dwnElpsdMicros"
const dwnResErrMsg = "dwnResErrMsg"
const dwnResCode = "dwnResCode"
const dwnResCntntEnc = "dwnResCntntEnc"
const dwnResCntntLen = "dwnResCntntLen"
const dwnResElpsdMicros = "dwnResElpsdMicros"
const dwnBytesRead = "dwnBytesRead"
const dwnBytesWrite = "dwnBytesWrite"
const upReqURI = "upReqURI"
const upAtmtpElpsdMicros = "upAtmptElpsdMicros"
const upAtmpt = "upAtmpt"
const upLabel = "upLabel"
const upAtmptResCode = "upAtmptResCode"
const upAtmptResBodyBytes = "upAtmptResBodyBytes"
const upAtmptElpsdMicros = "upAtmptElpsdMicros"
const upAtmptAbort = "upAtmptAbort"
// ServerID is a unique identifier made up as md5 of hostname and version.
// initServerId creates a unique ID for the server log.
func initServerID() {
hasher := md5.New()
hasher.Write([]byte(getHost() + getVersion()))
ID = hex.EncodeToString(hasher.Sum(nil))[0:8]
log.Info().Str("srvID", ID).Msg("srvID determined")
log.Logger = log.With().Str("srvId", ID).Logger()
}
// ServerID is a unique identifier made up as md5 of hostname and version.
// initServerId creates a unique ID for the server log.
func initPID() {
pid := os.Getpid()
log.Info().Int("pid", pid).Msg("pid determined")
log.Logger = log.With().Int("pid", pid).Logger()
}
func getHost() string {
host := os.Getenv("HOSTNAME")
if len(host) == 0 {
host, _ = os.Hostname()
}
log.Info().Str("hostName", host).Msg("hostName determined")
return host
}
func getVersion() string {
log.Info().Str("version", Version).Msg("version determined")
return Version
}
// Init sets up a global logger instance
func initLogger() {
defaultLevel := zerolog.InfoLevel
zerolog.SetGlobalLevel(defaultLevel)
logColor := strings.ToUpper(os.Getenv("LOGCOLOR"))
switch logColor {
case "TRUE", "YES", "Y":
w := zerolog.ConsoleWriter{
Out: os.Stderr,
NoColor: false,
}
log.Logger = log.Output(w)
default:
//no color logging
}
initServerID()
initPID()
log.Info().Msgf("setting global log level to %s", strings.ToUpper(defaultLevel.String()))
}