This repository has been archived by the owner on Nov 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
console.go
132 lines (117 loc) · 2.82 KB
/
console.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
132
package log
import (
"fmt"
"io"
"os"
"strings"
"sync"
"github.com/go-logfmt/logfmt"
"github.com/livebud/color"
)
// Debugger log
func Debugger() *Logger {
return New(Console(color.Default(), os.Stderr))
}
// Default log
func Default() *Logger {
return New(Filter(LevelInfo, Console(color.Default(), os.Stderr)))
}
// Parse the logger with a given filter
func Parse(filter string) (*Logger, error) {
level, err := ParseLevel(filter)
if err != nil {
return nil, err
}
return New(Filter(level, Console(color.Default(), os.Stderr))), nil
}
// Console handler
func Console(color color.Writer, writer io.Writer) Handler {
return &console{color, sync.Mutex{}, writer, prefixes(color)}
}
// console logger
type console struct {
color color.Writer
mu sync.Mutex
writer io.Writer
prefixes map[Level]string
}
// Prefixes
func prefixes(color color.Writer) map[Level]string {
if color.Enabled() {
return map[Level]string{
LevelDebug: "|",
LevelInfo: "|",
LevelWarn: "|",
LevelError: "|",
}
}
return map[Level]string{
LevelDebug: "debug:",
LevelInfo: "info:",
LevelWarn: "warn:",
LevelError: "error:",
}
}
func (c *console) format(level Level, msg string) string {
switch level {
case LevelDebug:
return c.color.Dim(msg)
case LevelInfo:
return c.color.Blue(msg)
case LevelNotice:
return c.color.Pink(msg)
case LevelWarn:
return c.color.Yellow(msg)
case LevelError:
return c.color.Red(msg)
default:
return ""
}
}
// Log implements Logger
func (c *console) Log(entry *Entry) error {
// Format the message
msg := new(strings.Builder)
msg.WriteString(c.format(entry.Level, c.prefixes[entry.Level]) + " " + entry.Message)
// Format and log the fields
if len(entry.Fields) > 0 {
keys := entry.Fields.Keys()
fields := new(strings.Builder)
enc := logfmt.NewEncoder(fields)
for _, key := range keys {
enc.EncodeKeyval(key, entry.Fields.Get(key))
}
enc.Reset()
msg.WriteString(" " + c.color.Dim(fields.String()))
}
msg.WriteString("\n")
// Write out
c.mu.Lock()
fmt.Fprint(c.writer, msg.String())
c.mu.Unlock()
return nil
}
// Stderr is a console log singleton that writes to stderr
var stderr = Default()
var (
// Debug message is written to the console
Debug = stderr.Debug
// Debugf message is written to the console
Debugf = stderr.Debugf
// Info message is written to the console
Info = stderr.Info
// Infof message is written to the console
Infof = stderr.Infof
// Notice message is written to the console
Notice = stderr.Notice
// Noticef message is written to the console
Noticef = stderr.Noticef
// Warn message is written to the console
Warn = stderr.Warn
// Warnf message is written to the console
Warnf = stderr.Warnf
// Error message is written to the console
Error = stderr.Error
// Errorf message is written to the console
Errorf = stderr.Errorf
)