-
Notifications
You must be signed in to change notification settings - Fork 4
/
console_printer.go
73 lines (60 loc) · 1.38 KB
/
console_printer.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
package prettylog
import (
"fmt"
"os"
"strings"
"github.com/insolar/assured-ledger/ledger-core/log/logoutput"
)
const TimestampFormat = "2006-01-02T15:04:05.000000000Z07:00"
var Defaults = Config{
NoColor: true,
TimeFormat: TimestampFormat,
FormatCaller: formatCaller,
PartsOrder: []string{
logoutput.TimestampFieldName,
logoutput.LevelFieldName,
logoutput.MessageFieldName,
logoutput.CallerFieldName,
},
}
type Formatter = func(interface{}) string
type Config struct {
Enable bool
// NoColor disables the colorized output.
NoColor bool
// TimeFormat specifies the format for timestamp in output.
TimeFormat string
// PartsOrder defines the order of parts in output.
PartsOrder []string
FormatTimestamp Formatter
FormatLevel Formatter
FormatCaller Formatter
FormatMessage Formatter
FormatFieldName Formatter
FormatFieldValue Formatter
FormatErrFieldName Formatter
FormatErrFieldValue Formatter
}
func formatCaller(i interface{}) string {
var c string
if cc, ok := i.(string); ok {
c = cc
}
if len(c) > 0 {
if len(cwd) > 0 {
c = strings.TrimPrefix(c, cwd)
c = strings.TrimPrefix(c, "/")
}
c = logoutput.CallerFieldName + "=" + c
}
return c
}
var cwd string
func init() {
var err error
cwd, err = os.Getwd()
if err != nil {
cwd = ""
fmt.Println("couldn't get current working directory: ", err.Error())
}
}