-
Notifications
You must be signed in to change notification settings - Fork 61
/
logger.go
76 lines (62 loc) · 1.26 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
// Copyright 2020 VMware, Inc.
// SPDX-License-Identifier: Apache-2.0
package helpers
import (
"bytes"
"fmt"
"math/rand"
"time"
)
type LogLevel int
const (
LogTrace LogLevel = iota
LogDebug LogLevel = iota
)
type Logger struct {
LogLevel LogLevel
Buf *bytes.Buffer
}
func (l Logger) Section(msg string, f func()) {
fmt.Printf("==> %s\n", msg)
f()
}
func (l Logger) Errorf(msg string, args ...interface{}) {
if l.Buf != nil {
l.Buf.Write([]byte(fmt.Sprintf(msg, args...)))
return
}
fmt.Printf(msg, args...)
}
func (l Logger) Warnf(msg string, args ...interface{}) {
if l.Buf != nil {
l.Buf.Write([]byte(fmt.Sprintf(msg, args...)))
return
}
fmt.Printf(msg, args...)
}
func (l Logger) Logf(msg string, args ...interface{}) {
if l.Buf != nil {
l.Buf.Write([]byte(fmt.Sprintf(msg, args...)))
return
}
fmt.Printf(msg, args...)
}
func (l Logger) Debugf(msg string, args ...interface{}) {
if l.Buf != nil {
l.Buf.Write([]byte(fmt.Sprintf(msg, args...)))
return
}
fmt.Printf(msg, args...)
}
func (l Logger) Tracef(msg string, args ...interface{}) {
if l.Buf != nil {
l.Buf.Write([]byte(fmt.Sprintf(msg, args...)))
return
}
if l.LogLevel == LogTrace {
fmt.Printf(msg, args...)
}
}
func init() {
rand.Seed(time.Now().UnixNano())
}