-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogging.go
112 lines (99 loc) · 2.81 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
/*
* Copyright (c) 2024 Johan Stenstam, johan.stenstam@internetstiftelsen.se
*/
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"github.com/spf13/viper"
"gopkg.in/natefinch/lumberjack.v2"
)
func SetupLogging(conf *Config) {
logfile := viper.GetString("log.file")
debug := viper.GetString("log.mode") == "debug"
logoptions := log.Ldate | log.Ltime
if debug {
log.Println("Logging in debug mode (showing file and line number)")
logoptions |= log.Lshortfile
}
prefix := ""
if logfile != "" {
log.SetOutput(&lumberjack.Logger{
Filename: logfile,
MaxSize: 20,
MaxBackups: 3,
MaxAge: 14,
})
fmt.Printf("TAPIR-POP standard logging to: %s\n", logfile)
} else {
POPExiter("Error: standard log (key log.file) not specified")
}
logfile = viper.GetString("policy.logfile")
if logfile != "" {
logfile = filepath.Clean(logfile)
f, err := os.OpenFile(logfile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644) // #nosec G302
if err != nil {
POPExiter("error opening TAPIR-POP policy logfile '%s': %v", logfile, err)
}
if debug {
prefix = "policy: "
}
conf.Loggers.Policy = log.New(f, prefix, logoptions)
conf.Loggers.Policy.SetOutput(&lumberjack.Logger{
Filename: logfile,
MaxSize: 20,
MaxBackups: 3,
MaxAge: 14,
})
fmt.Printf("TAPIR-POP policy logging to: %s\n", logfile)
} else {
log.Println("No policy logfile specified, using default")
conf.Loggers.Policy = log.Default()
}
logfile = viper.GetString("dnsengine.logfile")
if logfile != "" {
logfile = filepath.Clean(logfile)
f, err := os.OpenFile(logfile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644) // #nosec G302
if err != nil {
POPExiter("error opening TAPIR-POP dnsengine logfile '%s': %v", logfile, err)
}
if debug {
prefix = "dnsengine: "
}
conf.Loggers.Dnsengine = log.New(f, prefix, logoptions)
conf.Loggers.Dnsengine.SetOutput(&lumberjack.Logger{
Filename: logfile,
MaxSize: 20,
MaxBackups: 3,
MaxAge: 14,
})
fmt.Printf("TAPIR-POP dnsengine logging to: %s\n", logfile)
} else {
log.Println("No dnsengine logfile specified, using default")
conf.Loggers.Dnsengine = log.Default()
}
logfile = viper.GetString("tapir.mqtt.logfile")
if logfile != "" {
logfile = filepath.Clean(logfile)
f, err := os.OpenFile(logfile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644) // #nosec G302
if err != nil {
POPExiter("error opening TAPIR-POP MQTT logfile '%s': %v", logfile, err)
}
if debug {
prefix = "mqtt: "
}
conf.Loggers.Mqtt = log.New(f, prefix, logoptions)
conf.Loggers.Mqtt.SetOutput(&lumberjack.Logger{
Filename: logfile,
MaxSize: 20,
MaxBackups: 3,
MaxAge: 14,
})
fmt.Printf("TAPIR-POP MQTT logging to: %s\n", logfile)
} else {
log.Println("No MQTT logfile specified, using default")
conf.Loggers.Mqtt = log.Default()
}
}