-
Notifications
You must be signed in to change notification settings - Fork 259
/
console.go
127 lines (112 loc) · 2.78 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
// Copyright 2013, Örjan Persson. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package logging
import (
"fmt"
"io"
"log"
"os"
)
type color int
// color values
const (
ColorBlack = iota + 30
ColorRed
ColorGreen
ColorYellow
ColorBlue
ColorMagenta
ColorCyan
ColorWhite
)
// background color values
const (
ColorBlackBg = iota + 40
ColorRedBg
ColorGreenBg
ColorYellowBg
ColorBlueBg
ColorMagentaBg
ColorCyanBg
ColorWhiteBg
)
var (
// colors = []string{
// CRITICAL: ColorSeq(ColorMagenta),
// ERROR: ColorSeq(ColorRed),
// WARNING: ColorSeq(ColorYellow),
// NOTICE: ColorSeq(ColorGreen),
// DEBUG: ColorSeq(ColorCyan),
// }
colors = []string{
CRITICAL: ColorSeq(ColorMagentaBg),
ERROR: ColorSeq(ColorRedBg),
WARNING: ColorSeq(ColorYellowBg),
NOTICE: ColorSeq(ColorGreenBg),
DEBUG: ColorSeq(ColorCyanBg),
}
boldcolors = []string{
CRITICAL: ColorSeqBold(ColorMagenta),
ERROR: ColorSeqBold(ColorRed),
WARNING: ColorSeqBold(ColorYellow),
NOTICE: ColorSeqBold(ColorGreen),
DEBUG: ColorSeqBold(ColorCyan),
}
)
// LogBackend utilizes the standard log module.
type LogBackend struct {
Logger *log.Logger
Color bool
}
// NewLogBackend creates a new LogBackend.
func NewLogBackend(out io.Writer, prefix string, flag int) *LogBackend {
return &LogBackend{Logger: log.New(out, prefix, flag)}
}
// Log implements the Backend interface.
func (b *LogBackend) Log(calldepth int, rec *Record) {
var msg string
if b.Color {
msg = rec.Formatted(calldepth+1, true)
} else {
msg = rec.Formatted(calldepth+1, false)
}
err := b.Logger.Output(calldepth+2, msg)
if err != nil {
fmt.Fprintf(os.Stderr, "unable to Console Log msg:%s [error]%s\n", msg, err.Error())
}
}
// Close closes the log service.
func (b *LogBackend) Close() {}
// ConvertColors takes a list of ints representing colors for log levels and
// converts them into strings for ANSI color formatting
func ConvertColors(colors []int, bold bool) []string {
converted := []string{}
for _, i := range colors {
if bold {
converted = append(converted, ColorSeqBold(color(i)))
} else {
converted = append(converted, ColorSeq(color(i)))
}
}
return converted
}
// ColorSeq adds color identifier
func ColorSeq(color color) string {
return fmt.Sprintf("\033[%dm", int(color))
}
// ColorSeqBold adds blod color identifier
func ColorSeqBold(color color) string {
return fmt.Sprintf("\033[%d;1m", int(color))
}
func doFmtVerbLevelColor(layout string, colorful bool, level Level, output io.Writer) {
if colorful {
if layout == "bold" {
output.Write([]byte(boldcolors[level]))
} else if layout == "reset" {
output.Write([]byte("\033[0m"))
} else {
output.Write([]byte(colors[level]))
}
}
}