-
Notifications
You must be signed in to change notification settings - Fork 2
/
logging.go
68 lines (58 loc) · 1.57 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
package main
import (
"io"
"os"
"path/filepath"
logrus "github.com/sirupsen/logrus"
)
// setupLogging configures logging settings.
func setupLogging(config *Config) error {
// Ensure the database directory exists.
if _, err := os.Stat(config.Log.LogDirPath); os.IsNotExist(err) {
err := os.Mkdir(config.Log.LogDirPath, LogDirPermissions)
if err != nil {
return err
}
}
// Construct the full path to the database file.
logFilePath := filepath.Join(config.Log.LogDirPath, config.Log.LogFile)
// Create and open the log file with strict permissions.
logFile, err := os.OpenFile(
logFilePath, os.O_CREATE|os.O_WRONLY|os.O_APPEND,
LogFilePermissions,
)
if err != nil {
return err
}
// Create a multi-writer to write to both standard output and the log
// file.
multiWriter := io.MultiWriter(os.Stdout, logFile)
logrus.SetOutput(multiWriter)
// Set log format to include date and time.
logrus.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true,
})
// Set the log level based on the config.
logLevel := convertLogLevel(config.Log.LogLevel)
logrus.SetLevel(logLevel)
return nil
}
// convertLogLevel converts a log level string from the config to a logrus log
// level.
func convertLogLevel(level string) logrus.Level {
switch level {
case "fatal":
return logrus.FatalLevel
case "error":
return logrus.ErrorLevel
case "warn", "warning":
return logrus.WarnLevel
case "info":
return logrus.InfoLevel
case "debug":
return logrus.DebugLevel
default:
// Default to info level if the provided level is unrecognized.
return logrus.InfoLevel
}
}