-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
50 lines (46 loc) · 1.26 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
package main
import (
"github.com/rs/zerolog"
_ "github.com/rs/zerolog/log"
"gopkg.in/natefinch/lumberjack.v2"
"os"
"time"
)
// Logger struct to hold refs
type Logger struct {
Logger *zerolog.Logger
fileWriter *lumberjack.Logger
}
// Initialize logger with config file location as string
func (l *Logger) Initialize(f string) {
// Create console writer
consoleWriter := zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339}
// Create file writer
fileWriter := &lumberjack.Logger{
Filename: f,
MaxSize: 10, // Megabytes
MaxBackups: 5,
MaxAge: 28, // Days
LocalTime: true,
}
multi := zerolog.MultiLevelWriter(consoleWriter, fileWriter)
//
logger := zerolog.New(multi).With().Timestamp().Logger()
// Set default loglevel
zerolog.SetGlobalLevel(zerolog.InfoLevel)
// Set logger to struct
l.fileWriter = fileWriter
l.Logger = &logger
}
// EnableDebug enable the debugging mode of the logger depending on bool
func (l *Logger) EnableDebug(debug bool) {
if l.Logger != nil {
if debug {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
l.Logger.Info().Str("loglevel", "debug").Msg("LogLevel changed.")
} else {
zerolog.SetGlobalLevel(zerolog.InfoLevel)
l.Logger.Info().Str("loglevel", "info").Msg("LogLevel changed.")
}
}
}