forked from lakhinsu/rabbitmq-go-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
37 lines (29 loc) · 834 Bytes
/
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
package utils
import (
"os"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
// Modify the init function if you change logging library
// Find and replace the imports in the rest of the app
func init() {
// Read log level
logLevel := GetEnvVar("LOG_LEVEL")
// Replace with switch or add more levels as per need
if logLevel == "debug" {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
} else {
zerolog.SetGlobalLevel(zerolog.InfoLevel)
}
// Set time format
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
// Set Global fields
host, err := os.Hostname()
if err != nil {
log.Logger = log.With().Str("host", "unknown").Logger()
} else {
log.Logger = log.With().Str("host", host).Logger()
}
log.Logger = log.With().Str("service", "rabbitmq-consumer").Logger()
log.Logger = log.With().Caller().Logger()
}