-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
192 lines (169 loc) · 4.13 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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package logtamer
import (
"io/ioutil"
"log"
"os"
"github.com/gotamer/mail/send"
)
// Configuration
type LogCfg struct {
appName string
File string
SendMailTo string // Application Admin Email
Smtp struct {
Hostname string // smtp.example.com
Hostport int // 587
Username string // usally email address username@example.com
Password string
}
}
type bool_type bool
const (
FAKEHOSTNAME = "example.com"
APPNAME = "LogTamer"
EXIT_YES bool_type = true
EXIT_NO bool_type = false
)
var (
o = new(LogCfg)
mail *send.SmtpTamer
file *os.File
Debug = *log.Default()
Info = *log.Default()
Warn = *log.Default()
Error = *log.Default()
)
func init() {
// Config file defaults
o.File = "LogTamer.log"
o.appName = APPNAME
o.SendMailTo = "admin@example.com"
o.Smtp.Hostname = FAKEHOSTNAME
o.Smtp.Hostport = 587
o.Smtp.Username = "user@example.com"
o.Smtp.Password = "Long-Complicated-Secret-Pass-Word"
Level(1)
}
func FileClose() {
if file == nil {
return
}
Info.Println("Closing log file")
if err := file.Close(); err != nil {
log.Fatal("Close Log File err: ", err)
}
}
func Config() *LogCfg {
return o
}
// Set defaults
// filename is filename including path
func Default(appname, filename string) {
if appname != "" {
o.appName = appname
}
if filename != "" {
o.File = filename
}
}
func Err(err error, exit bool_type, msg string) {
if err != nil {
if msg != "" {
Error.Printf("%s: %w", msg, err)
} else {
Error.Println(err)
}
if exit {
os.Exit(1)
}
}
}
// Switch between log levels
// Level 0 No Logging at all
// Level 1 output all to screen, is also default
// Level 2 info, warn & error to screen, debug to file
// Level 3 warn & error to screen, debug and info to file
// Level 4 warn & error to file
// Level 5 error to file
// Level 6 warn & error to mail
// Level 7 error to mail
func Level(level uint8) {
Debug = *log.Default()
Info = *log.Default()
Warn = *log.Default()
Error = *log.Default()
Debug.SetPrefix("DBG ")
Info.SetPrefix("INF ")
Warn.SetPrefix("WRN ")
Error.SetPrefix("ERR ")
Info.SetFlags(log.Ltime | log.Lshortfile)
Debug.SetFlags(log.Ltime | log.Lshortfile)
Warn.SetFlags(log.Ltime | log.Lshortfile)
Error.SetFlags(log.Ltime | log.Lshortfile)
switch level {
case 0:
// Level 0 No Logging at all
Debug.SetOutput(ioutil.Discard)
Info.SetOutput(ioutil.Discard)
Warn.SetOutput(ioutil.Discard)
Error.SetOutput(ioutil.Discard)
case 1:
// Level 1 output all to screen, is also default
case 2:
// Level 2 info, warn & error to screen, debug to file
// Log all but debug to screen aka debug mode. Debug goes to file
openFile()
Debug.SetOutput(file)
case 3:
// Level 3 warn & error to screen, debug and info to file
openFile()
Info.SetOutput(file)
Debug.SetOutput(file)
case 4:
// Level 4 warn & error to file
openFile()
Warn.SetOutput(file)
Error.SetOutput(file)
case 5:
// Level 5 error to file
openFile()
Error.SetOutput(file)
case 6:
// Level 6 warn & error to mail
if o.Smtp.Hostname == "" || o.Smtp.Hostname == FAKEHOSTNAME {
log.Fatal("ERR smtp config not set")
}
// email errors to admin
mail = send.NewSMTP(o.Smtp.Hostname, o.Smtp.Username, o.Smtp.Password)
mail.Envelop.SetFrom(o.appName, o.Smtp.Username)
mail.Envelop.SetTo("", o.SendMailTo)
mail.Envelop.Subject = "ERROR " + o.appName
Debug.SetOutput(ioutil.Discard)
Info.SetOutput(ioutil.Discard)
Warn.SetOutput(mail)
Error.SetOutput(mail)
case 7:
// Level 7 error to mail
if o.Smtp.Hostname == "" || o.Smtp.Hostname == FAKEHOSTNAME {
log.Fatal("ERR smtp config not set")
}
// email errors to admin
mail = send.NewSMTP(o.Smtp.Hostname, o.Smtp.Username, o.Smtp.Password)
mail.Envelop.SetFrom(o.appName, o.Smtp.Username)
mail.Envelop.SetTo("", o.SendMailTo)
mail.Envelop.Subject = "ERROR " + o.appName
Debug.SetOutput(ioutil.Discard)
Info.SetOutput(ioutil.Discard)
Warn.SetOutput(ioutil.Discard)
Error.SetOutput(mail)
default:
// Same as Level 1, output all to screen
}
}
func openFile() {
var err error
file, err = os.OpenFile(o.File, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
log.Fatal(err)
}
}