forked from Vasiliy-Bondarenko/ozzo-log
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole.go
88 lines (78 loc) · 2.1 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
// Copyright 2016 Qiang Xue. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package log
import (
"errors"
"fmt"
"io"
"os"
"runtime"
)
type consoleBrush func(string) string
func newConsoleBrush(format string) consoleBrush {
return func(text string) string {
return "\033[" + format + "m" + text + "\033[0m"
}
}
var brushes = map[Level]consoleBrush{
LevelDebug: newConsoleBrush("39"), // default
LevelInfo: newConsoleBrush("32"), // green
LevelNotice: newConsoleBrush("36"), // cyan
LevelWarning: newConsoleBrush("33"), // yellow
LevelError: newConsoleBrush("31"), // red
LevelCritical: newConsoleBrush("35"), // magenta
LevelAlert: newConsoleBrush("1;91"), // bold light red
LevelEmergency: newConsoleBrush("1;95"), // bold light magenta
}
// ConsoleTarget writes filtered log messages to console window.
type ConsoleTarget struct {
*Filter
ColorMode bool // whether to use colors to differentiate log levels
Writer io.Writer // the writer to write log messages
close chan bool
}
// NewConsoleTarget creates a ConsoleTarget.
// The new ConsoleTarget takes these default options:
// MaxLevel: LevelDebug, ColorMode: true, Writer: os.Stdout
func NewConsoleTarget() *ConsoleTarget {
return &ConsoleTarget{
Filter: &Filter{MaxLevel: LevelDebug},
ColorMode: true,
Writer: os.Stdout,
close: make(chan bool, 0),
}
}
// Open prepares ConsoleTarget for processing log messages.
func (t *ConsoleTarget) Open(io.Writer) error {
t.Filter.Init()
if t.Writer == nil {
return errors.New("ConsoleTarget.Writer cannot be nil")
}
if runtime.GOOS == "windows" {
t.ColorMode = false
}
return nil
}
// Process writes a log message using Writer.
func (t *ConsoleTarget) Process(e *Entry) {
if e == nil {
t.close <- true
return
}
if !t.Allow(e) {
return
}
msg := e.String()
if t.ColorMode {
brush, ok := brushes[e.Level]
if ok {
msg = brush(msg)
}
}
fmt.Fprintln(t.Writer, msg)
}
// Close closes the console target.
func (t *ConsoleTarget) Close() {
<-t.close
}