-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole_receiver.go
119 lines (99 loc) · 2.76 KB
/
console_receiver.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
// Copyright (c) Jeevanandam M (https://github.com/jeevatkm)
// go-aah/log source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
package log
import (
"encoding/json"
"fmt"
"io"
"os"
"runtime"
"sync"
"aahframework.org/config.v0"
"aahframework.org/essentials.v0"
)
var (
// ANSI color codes
resetColor = []byte("\033[0m")
levelToColor = [][]byte{
LevelFatal: []byte("\033[0;31m"), // red
LevelPanic: []byte("\033[0;31m"), // red
LevelError: []byte("\033[0;31m"), // red
LevelWarn: []byte("\033[0;33m"), // yellow
LevelInfo: []byte("\033[0;37m"), // white
LevelDebug: []byte("\033[0;36m"), // cyan
LevelTrace: []byte("\033[0;35m"), // magenta (purple)
}
_ Receiver = (*ConsoleReceiver)(nil)
)
// ConsoleReceiver writes the log entry into os.Stderr.
// For non-windows it writes with color.
type ConsoleReceiver struct {
out io.Writer
formatter string
flags []ess.FmtFlagPart
isCallerInfo bool
isColor bool
mu sync.Mutex
}
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// ConsoleReceiver methods
//___________________________________
// Init method initializes the console logger.
func (c *ConsoleReceiver) Init(cfg *config.Config) error {
c.out = os.Stderr
c.isColor = runtime.GOOS != "windows"
if v, found := cfg.Bool("log.color"); found {
c.isColor = v
}
c.formatter = cfg.StringDefault("log.format", "text")
if !(c.formatter == textFmt || c.formatter == jsonFmt) {
return fmt.Errorf("log: unsupported format '%s'", c.formatter)
}
c.mu = sync.Mutex{}
return nil
}
// SetPattern method initializes the logger format pattern.
func (c *ConsoleReceiver) SetPattern(pattern string) error {
flags, err := ess.ParseFmtFlag(pattern, FmtFlags)
if err != nil {
return err
}
c.flags = flags
if c.formatter == textFmt {
c.isCallerInfo = isCallerInfo(c.flags)
}
return nil
}
// SetWriter method sets the given writer into console receiver.
func (c *ConsoleReceiver) SetWriter(w io.Writer) {
c.out = w
}
// IsCallerInfo method returns true if log receiver is configured with caller info
// otherwise false.
func (c *ConsoleReceiver) IsCallerInfo() bool {
return c.isCallerInfo
}
// Log method writes the log entry into os.Stderr.
func (c *ConsoleReceiver) Log(entry *Entry) {
c.mu.Lock()
defer c.mu.Unlock()
if c.isColor {
_, _ = c.out.Write(levelToColor[entry.Level])
}
var msg []byte
if c.formatter == textFmt {
msg = textFormatter(c.flags, entry)
} else {
msg, _ = json.Marshal(entry)
msg = append(msg, '\n')
}
_, _ = c.out.Write(msg)
if c.isColor {
_, _ = c.out.Write(resetColor)
}
}
// Writer method returns the current log writer.
func (c *ConsoleReceiver) Writer() io.Writer {
return c.out
}