forked from g3n/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
console.go
75 lines (62 loc) · 1.4 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
// Copyright 2016 The G3N Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package logger
import (
"os"
)
// Ansi terminal color codes
const (
csi = "\x1B["
black = "30m"
red = "31m"
green = "32m"
yellow = "33m"
blue = "34m"
magenta = "35m"
cyan = "36m"
white = "37m"
bblack = "30m"
bred = "31;1m"
bgreen = "32;1m"
byellow = "33;1m"
bblue = "34;1m"
bmagenta = "35;1m"
bcyan = "36;1m"
bwhite = "37;1m"
)
// Maps log level to color sequence
var colorMap = map[int]string{
DEBUG: white,
INFO: green,
WARN: byellow,
ERROR: bred,
FATAL: bmagenta,
}
// Console is a console writer used for logging.
type Console struct {
writer *os.File
color bool
}
// NewConsole creates and returns a new logger Console writer
// If color is true, this writer uses Ansi codes to write
// log messages in color accordingly to its level.
func NewConsole(color bool) *Console {
return &Console{os.Stdout, color}
}
// Write writes the provided logger event to the console.
func (w *Console) Write(event *Event) {
if w.color {
w.writer.Write([]byte(csi))
w.writer.Write([]byte(colorMap[event.level]))
}
w.writer.Write([]byte(event.fmsg))
if w.color {
w.writer.Write([]byte(csi))
w.writer.Write([]byte(white))
}
}
func (w *Console) Close() {
}
func (w *Console) Sync() {
}