-
Notifications
You must be signed in to change notification settings - Fork 3
/
logging.go
131 lines (110 loc) · 2.96 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
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"errors"
"flag"
"fmt"
"log"
"os"
"strings"
)
type Priority int
const (
LOG_EMERG Priority = iota
LOG_ALERT
LOG_CRIT
LOG_ERR
LOG_WARNING
LOG_NOTICE
LOG_INFO
LOG_DEBUG
)
var priorityMap = map[Priority]string{
LOG_EMERG: "EMERG",
LOG_ALERT: "ALERT",
LOG_CRIT: "CRIT",
LOG_ERR: "ERR",
LOG_WARNING: "WARNING",
LOG_NOTICE: "NOTICE",
LOG_INFO: "INFO",
LOG_DEBUG: "DEBUG",
}
var logFile *log.Logger
var logStderr *log.Logger
var minPriority = LOG_DEBUG
func addPriority(priority Priority, message string) string {
return fmt.Sprintf("%s: %s", priorityMap[priority], message)
}
func addPrefix(prefix string, message string) string {
return fmt.Sprintf("%s: %s", prefix, message)
}
// Print calls Output to print to the logFile.
// Arguments are handled in the manner of fmt.Print.
func Log(priority Priority, v ...interface{}) {
if priority <= minPriority {
logFile.Output(2, addPriority(priority, fmt.Sprint(v...)))
}
}
// Printf calls Output to print to the logFile.
// Arguments are handled in the manner of fmt.Printf.
func Logf(priority Priority, format string, v ...interface{}) {
if priority <= minPriority {
logFile.Output(2, addPriority(priority, fmt.Sprintf(format, v...)))
}
}
// Fatal is like Log() but also prints to stderr and is followed by a call to os.Exit(1).
func Fatal(v ...interface{}) {
s := addPrefix("FATAL", fmt.Sprint(v...))
logStderr.Output(2, s)
logFile.Output(2, s)
os.Exit(1)
}
// Fatalf is like to Logf() but also prints to stderr and is followed by a call to os.Exit(1).
func Fatalf(format string, v ...interface{}) {
s := addPrefix("FATAL", fmt.Sprintf(format, v...))
logStderr.Output(2, s)
logFile.Output(2, s)
os.Exit(1)
}
// Panic is like to Log() but also prints to stderr and is followed by a call to panic().
func Panic(v ...interface{}) {
s := addPrefix("PANIC", fmt.Sprint(v...))
logStderr.Output(2, s)
logFile.Output(2, s)
panic(s)
}
// Panicf is like to Logf() but also prints to stderr and is followed by a call to panic()
func Panicf(format string, v ...interface{}) {
s := addPrefix("PANIC", fmt.Sprintf(format, v...))
logStderr.Output(2, s)
logFile.Output(2, s)
panic(s)
}
func (p *Priority) String() string {
value, ok := priorityMap[*p]
if ok {
return value
}
return "UNKNOWN"
}
func (p *Priority) Set(value string) error {
input := strings.ToUpper(value)
for key, value := range priorityMap {
if value == input {
*p = key
return nil
}
}
return errors.New(fmt.Sprintf("Unknown loglevel: %s", input))
}
func init() {
filename := flag.String("logfile", "/var/log/proxy.log", "File for log messages")
flag.Var(&minPriority, "loglevel", "Minimum log level")
logFileHandle, err := os.OpenFile(*filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
if err != nil {
log.Fatal(err)
}
logPrefix := ""
logFlags := log.Ldate|log.Lmicroseconds|log.Lshortfile
logFile = log.New(logFileHandle, logPrefix, logFlags)
logStderr = log.New(os.Stderr, logPrefix, logFlags)
}