-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
59 lines (51 loc) · 941 Bytes
/
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
package main
import (
"bytes"
"fmt"
"log"
)
const (
LogLevel1 = "Debug"
LogLevel2 = "Info"
LogLevel3 = "Silent"
)
type Logger struct {
logLevel string
logger *log.Logger
}
func NewLogger(logLevel string) *Logger {
switch logLevel {
default:
logLevel = LogLevel3
case LogLevel1:
logLevel = LogLevel1
case LogLevel2:
logLevel = LogLevel2
case LogLevel3:
logLevel = LogLevel3
}
var buf bytes.Buffer
return &Logger{
logLevel: logLevel,
logger: log.New(&buf, "logger: ", log.Ldate|log.Ltime),
}
}
func (l *Logger) debug(msg string) {
if l.logLevel == LogLevel1 || l.logLevel == LogLevel2 {
fmt.Print(msg)
}
}
func (l *Logger) info(msg string) {
if l.logLevel == LogLevel2 {
fmt.Print(msg)
}
}
func (l *Logger) fatal(msg string) {
log.Fatal(msg)
}
func (l *Logger) failOnError(err error, msg string) {
if err != nil {
log.Fatal("%s: %s", msg, err)
panic(fmt.Sprintf("%s: %s", msg, err))
}
}