-
Notifications
You must be signed in to change notification settings - Fork 16
/
logger.go
56 lines (50 loc) · 1.49 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
package main
import (
"os"
"strings"
"time"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
func initLogger() {
// Set time format based on environment variable
timeFormat := os.Getenv("LOG_TIME_FORMAT")
switch strings.ToLower(timeFormat) {
case "unix":
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
case "unixms":
zerolog.TimeFieldFormat = zerolog.TimeFormatUnixMs
case "unixmicro":
zerolog.TimeFieldFormat = zerolog.TimeFormatUnixMicro
case "iso8601":
zerolog.TimeFieldFormat = time.RFC3339
default:
// If not set or invalid, default to RFC3339 (zerolog default)
zerolog.TimeFieldFormat = time.RFC3339
}
// Get log level from environment variable
logLevel := strings.ToLower(os.Getenv("LOG_LEVEL"))
switch logLevel {
case "trace":
zerolog.SetGlobalLevel(zerolog.TraceLevel)
case "debug":
zerolog.SetGlobalLevel(zerolog.DebugLevel)
case "info":
zerolog.SetGlobalLevel(zerolog.InfoLevel)
case "warn":
zerolog.SetGlobalLevel(zerolog.WarnLevel)
case "error":
zerolog.SetGlobalLevel(zerolog.ErrorLevel)
case "fatal":
zerolog.SetGlobalLevel(zerolog.FatalLevel)
case "panic":
zerolog.SetGlobalLevel(zerolog.PanicLevel)
default:
// If not set or invalid, default to Info
zerolog.SetGlobalLevel(zerolog.InfoLevel)
}
log.Info().Msg("Logging initialized")
log.Info().Msg("Log level set to: " + zerolog.GlobalLevel().String())
log.Info().Msg("Time format set to: " + zerolog.TimeFieldFormat)
log.Info().Msg("================================================")
}