forked from git-chglog/git-chglog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
59 lines (50 loc) · 1.06 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
package chglog
import (
"fmt"
"io"
"log"
"regexp"
"github.com/fatih/color"
"github.com/kyokomi/emoji/v2"
)
// Logger ...
type Logger struct {
stdout io.Writer
stderr io.Writer
silent bool
noEmoji bool
reEmoji *regexp.Regexp
}
// NewLogger ...
func NewLogger(stdout, stderr io.Writer, silent, noEmoji bool) *Logger {
return &Logger{
stdout: stdout,
stderr: stderr,
silent: silent,
noEmoji: noEmoji,
reEmoji: regexp.MustCompile(`:[\w\+_\-]+:\s?`),
}
}
// Log ...
func (l *Logger) Log(msg string) {
if !l.silent {
l.log(l.stdout, msg+"\n")
}
}
// Error ...
func (l *Logger) Error(msg string) {
prefix := color.New(color.FgWhite, color.BgRed, color.Bold).SprintFunc()
l.log(l.stderr, fmt.Sprintf("%s %s\n", prefix(" ERROR "), color.RedString(msg)))
}
func (l *Logger) log(w io.Writer, msg string) {
var printer func(io.Writer, ...interface{}) (int, error)
if l.noEmoji {
msg = l.reEmoji.ReplaceAllString(msg, "")
printer = fmt.Fprint
} else {
printer = emoji.Fprint
}
if _, err := printer(w, msg); err != nil {
log.Fatalln(err)
}
}