-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogging.go
120 lines (95 loc) · 3.13 KB
/
logging.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package titangoutils
import (
"fmt"
"os"
"time"
"github.com/sirupsen/logrus"
"gopkg.in/natefinch/lumberjack.v2"
)
// LogConfig represents the configuration for logging.
type LogConfig struct {
ProjectName string `json:"project_name" yaml:"project_name"`
LogBaseDirectory string `json:"log_base_directory" yaml:"log_base_directory"`
LogRotationInterval time.Duration `json:"log_rotation_interval" yaml:"log_rotation_interval"`
LogMaxAgeDays int `json:"log_max_age_days" yaml:"log_max_age_days"`
LogCompress bool `json:"log_compress" yaml:"log_compress"`
LogToStdOut bool `json:"log_to_std_out" yaml:"log_to_std_out"`
DebugMode bool `json:"debug_mode" yaml:"debug_mode"`
}
// logrus logger object
// const (
// projectName = "test"
// logBaseDirectory = "/logs"
// logRotationInterval = 1 * time.Hour
// logMaxAgeDays = 7
// logCompress = true
// )
func initLogger(config *LogConfig) *logrus.Logger {
// default log level
var log = logrus.New()
log.Level = logrus.InfoLevel
// set log level to debug level if debug is true
if config.DebugMode {
log.Level = logrus.DebugLevel
}
// set log output to stdout if stdout is true
if config.LogToStdOut {
log.Out = os.Stdout
return log
}
logFile := config.ProjectName + ".log"
logDirectory := config.LogBaseDirectory + "/" + config.ProjectName
logPath := logDirectory + "/" + logFile
// create log directory
if err := os.MkdirAll(logDirectory, 0744); err != nil {
log.Fatalf("can't create log directory=%s, error=%s", logDirectory, err)
}
// create log file
file, err := os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("failed to set logging to file=%s, error=%s", logPath, err)
}
// log on stdout
log.WithFields(logrus.Fields{"filename": logPath, "max_age": config.LogMaxAgeDays, "compress": config.LogCompress}).Info("logger settings")
// set logging to file
log.Out = file
// set the lumberjack logger
lumberjackLogger := &lumberjack.Logger{
Filename: logPath,
MaxAge: config.LogMaxAgeDays,
Compress: config.LogCompress,
MaxSize: 1024,
}
// Set the Lumberjack logger
log.SetOutput(lumberjackLogger)
// disable color coding while logging to file
log.SetFormatter(&logrus.TextFormatter{ForceColors: false, FullTimestamp: true, TimestampFormat: time.RFC3339Nano})
if config.LogRotationInterval > 0 {
go rotateLogsPeriodically(lumberjackLogger, config.LogRotationInterval)
}
return log
}
func rotateLogsPeriodically(logger *lumberjack.Logger, interval time.Duration) {
now := time.Now()
nextHour := now.Truncate(time.Hour).Add(time.Hour)
durationUntilNextHour := time.Until(nextHour)
timer := time.NewTimer(durationUntilNextHour)
<-timer.C
rotateLogs(logger)
ticker := time.NewTicker(interval)
defer ticker.Stop()
for range ticker.C {
rotateLogs(logger)
}
}
func rotateLogs(logger *lumberjack.Logger) {
err := logger.Rotate()
var msg string
if err != nil {
msg = fmt.Sprintf("log rotation failed: %v", err)
fmt.Println(msg)
} else {
msg = "log rotated successfully"
}
fmt.Println(msg)
}