-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
58 lines (47 loc) · 1.27 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
package main
import (
"fmt"
"io"
"os"
"github.com/sirupsen/logrus"
"gopkg.in/natefinch/lumberjack.v2"
)
// Log is the central logger instance that can be used throughout the application.
var Log *logrus.Logger
func initalizeLogger() {
// Read the value of the APP_ENV environment variable
env := os.Getenv("APP_ENV")
// Create a new logger instance
log := logrus.New()
// configure logrus to output to a file called server.log along with stdout
log.SetOutput(io.MultiWriter(os.Stdout, &lumberjack.Logger{
Filename: "logfile.log",
MaxSize: 10, // megabytes
MaxBackups: 3,
MaxAge: 28, //days
Compress: true, // disabled by default
}))
// Configure the log level based on the environment
if env == "development" {
// Development log level
log.SetLevel(logrus.DebugLevel)
} else {
// Default log level (e.g., production).
log.SetLevel(logrus.InfoLevel)
}
// Set the log format to text
log.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true,
})
// Set the logger instance to the package-level variable
Log = log
Log.Info(fmt.Sprintf("Running in %s environment", func() string {
if env == "development" {
return "development"
} else {
return "production"
}
}()))
// Log an info message
Log.Info("Logger initialized")
}