-
Notifications
You must be signed in to change notification settings - Fork 43
/
logger.go
63 lines (45 loc) · 1.1 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
package envplate
import (
"errors"
"fmt"
"io"
_log "log"
"os"
)
const (
// RAW indicates a log message should be send directly to stdout
RAW logLevel = "RAW"
// DEBUG indicates log message that are only visible when the verbose flag it set
DEBUG = "DEBUG"
// INFO indicates regular log messages
INFO = "INFO"
// ERROR indicates error log messages
ERROR = "ERROR"
)
type logLevel string
type logger struct {
Out io.Writer
Verbose bool
}
// Log exposes a logger with a custom envplate formatting syntax
var Logger = &logger{Out: os.Stdout}
// Log emits log messages and filters based on the set verbosity - if an error
// is logged, the error msg is returned as error object
func Log(lvl logLevel, msg string, args ...interface{}) error {
return Logger.log(lvl, msg, args...)
}
func (l *logger) log(lvl logLevel, msg string, args ...interface{}) error {
if lvl == DEBUG && !l.Verbose {
return nil
}
msg = fmt.Sprintf(msg, args...)
if lvl == RAW {
fmt.Fprintf(l.Out, msg)
return nil
}
_log.Printf("[ %s ] %s", lvl, msg)
if lvl == ERROR {
return errors.New(msg)
}
return nil
}