• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

go-playground / log / 5863334224

15 Aug 2023 03:36AM UTC coverage: 82.793% (-11.0%) from 93.807%
5863334224

Pull #52

github

deankarn
optimizations
Pull Request #52: 1st pass slog support

212 of 212 new or added lines in 5 files covered. (100.0%)

587 of 709 relevant lines covered (82.79%)

10.37 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.0
/default_logger.go
1
package log
2

3
import (
4
        "fmt"
5
        "io"
6
        "os"
7
        "strconv"
8
        "sync"
9
)
10

11
const (
12
        space   = byte(' ')
13
        equals  = byte('=')
14
        newLine = byte('\n')
15
        base10  = 10
16
        v       = "%v"
17
)
18

19
// ConsoleBuilder is used to create a new console logger
20
type ConsoleBuilder struct {
21
        writer          io.Writer
22
        timestampFormat string
23
}
24

25
// NewConsoleBuilder creates a new ConsoleBuilder for configuring and creating a new console logger
26
func NewConsoleBuilder() *ConsoleBuilder {
2✔
27
        return &ConsoleBuilder{
2✔
28
                writer:          os.Stderr,
2✔
29
                timestampFormat: DefaultTimeFormat,
2✔
30
        }
2✔
31
}
2✔
32

33
func (b *ConsoleBuilder) WithWriter(writer io.Writer) *ConsoleBuilder {
2✔
34
        b.writer = writer
2✔
35
        return b
2✔
36
}
2✔
37

38
func (b *ConsoleBuilder) WithTimestampFormat(format string) *ConsoleBuilder {
2✔
39
        b.timestampFormat = format
2✔
40
        return b
2✔
41
}
2✔
42

43
func (b *ConsoleBuilder) Build() *Logger {
2✔
44
        return &Logger{
2✔
45
                writer:          b.writer,
2✔
46
                timestampFormat: b.timestampFormat,
2✔
47
        }
2✔
48
}
2✔
49

50
// Logger is an instance of the console logger
51
type Logger struct {
52
        m               sync.Mutex
53
        writer          io.Writer
54
        timestampFormat string
55
}
56

57
// Log handles the log entry
58
func (c *Logger) Log(e Entry) {
32✔
59
        var lvl string
32✔
60
        var i int
32✔
61
        buff := BytePool().Get()
32✔
62
        buff.B = append(buff.B, e.Timestamp.Format(c.timestampFormat)...)
32✔
63
        buff.B = append(buff.B, space)
32✔
64

32✔
65
        lvl = e.Level.String()
32✔
66

32✔
67
        for i = 0; i < 6-len(lvl); i++ {
70✔
68
                buff.B = append(buff.B, space)
38✔
69
        }
38✔
70

71
        buff.B = append(buff.B, lvl...)
32✔
72
        buff.B = append(buff.B, space)
32✔
73
        buff.B = append(buff.B, e.Message...)
32✔
74

32✔
75
        c.addFields("", buff, e.Fields)
32✔
76
        buff.B = append(buff.B, newLine)
32✔
77

32✔
78
        c.m.Lock()
32✔
79
        _, _ = c.writer.Write(buff.B)
32✔
80
        c.m.Unlock()
32✔
81

32✔
82
        BytePool().Put(buff)
32✔
83
}
84

85
func (c *Logger) addFields(prefix string, buff *Buffer, fields []Field) {
32✔
86
        for _, f := range fields {
60✔
87

28✔
88
                switch t := f.Value.(type) {
28✔
89
                case string:
16✔
90
                        printKey(buff, prefix+f.Key)
16✔
91
                        buff.B = append(buff.B, t...)
16✔
92
                case int:
1✔
93
                        printKey(buff, prefix+f.Key)
1✔
94
                        buff.B = strconv.AppendInt(buff.B, int64(t), base10)
1✔
95
                case int8:
1✔
96
                        printKey(buff, prefix+f.Key)
1✔
97
                        buff.B = strconv.AppendInt(buff.B, int64(t), base10)
1✔
98
                case int16:
1✔
99
                        printKey(buff, prefix+f.Key)
1✔
100
                        buff.B = strconv.AppendInt(buff.B, int64(t), base10)
1✔
101
                case int32:
1✔
102
                        printKey(buff, prefix+f.Key)
1✔
103
                        buff.B = strconv.AppendInt(buff.B, int64(t), base10)
1✔
104
                case int64:
1✔
105
                        printKey(buff, prefix+f.Key)
1✔
106
                        buff.B = strconv.AppendInt(buff.B, t, base10)
1✔
107
                case uint:
1✔
108
                        printKey(buff, prefix+f.Key)
1✔
109
                        buff.B = strconv.AppendUint(buff.B, uint64(t), base10)
1✔
110
                case uint8:
1✔
111
                        printKey(buff, prefix+f.Key)
1✔
112
                        buff.B = strconv.AppendUint(buff.B, uint64(t), base10)
1✔
113
                case uint16:
1✔
114
                        printKey(buff, prefix+f.Key)
1✔
115
                        buff.B = strconv.AppendUint(buff.B, uint64(t), base10)
1✔
116
                case uint32:
1✔
117
                        printKey(buff, prefix+f.Key)
1✔
118
                        buff.B = strconv.AppendUint(buff.B, uint64(t), base10)
1✔
119
                case uint64:
1✔
120
                        printKey(buff, prefix+f.Key)
1✔
121
                        buff.B = strconv.AppendUint(buff.B, t, base10)
1✔
122
                case float32:
×
123
                        printKey(buff, prefix+f.Key)
×
124
                        buff.B = strconv.AppendFloat(buff.B, float64(t), 'f', -1, 32)
×
125
                case float64:
×
126
                        printKey(buff, prefix+f.Key)
×
127
                        buff.B = strconv.AppendFloat(buff.B, t, 'f', -1, 64)
×
128
                case bool:
1✔
129
                        printKey(buff, prefix+f.Key)
1✔
130
                        buff.B = strconv.AppendBool(buff.B, t)
1✔
131
                case []Field:
×
132
                        c.addFields(prefix+f.Key+".", buff, t)
×
133
                default:
1✔
134
                        printKey(buff, prefix+f.Key)
1✔
135
                        buff.B = append(buff.B, fmt.Sprintf(v, f.Value)...)
1✔
136
                }
137
        }
138
}
139

140
func printKey(buff *Buffer, key string) {
28✔
141
        buff.B = append(buff.B, space)
28✔
142
        buff.B = append(buff.B, key...)
28✔
143
        buff.B = append(buff.B, equals)
28✔
144
}
28✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc