-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformatter_strings.go
More file actions
91 lines (79 loc) · 1.88 KB
/
formatter_strings.go
File metadata and controls
91 lines (79 loc) · 1.88 KB
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
/*
* Copyright (c) 2024-2026 Mikhail Knyazhev <markus621@yandex.com>. All rights reserved.
* Use of this source code is governed by a BSD 3-Clause license that can be found in the LICENSE file.
*/
package logx
import (
"encoding"
"fmt"
"io"
"strings"
"time"
"go.osspkg.com/ioutils/data"
)
type FormatString struct {
delim byte
}
func NewFormatString() *FormatString {
return &FormatString{delim: '\t'}
}
func (v *FormatString) SetDelimiter(d byte) {
v.delim = d
}
func (v *FormatString) write(w *data.Buffer, key, value interface{}) {
w.WriteByte('"') //nolint:errcheck
w.WriteString(typing(key)) //nolint:errcheck
w.WriteString("\"=\"") //nolint:errcheck
w.WriteString(typing(value)) //nolint:errcheck
w.WriteByte('"') //nolint:errcheck
w.WriteByte(v.delim) //nolint:errcheck
}
func (v *FormatString) Encode(out io.Writer, m *Message) error {
w := poolBuffer.Get()
defer func() {
poolBuffer.Put(w)
}()
v.write(w, "time", m.Time.Format(time.RFC3339))
v.write(w, "level", m.Level)
v.write(w, "msg", m.Message)
if count := len(m.Ctx); count > 0 {
if count%2 != 0 {
m.Ctx = append(m.Ctx, nil)
count++
}
for i := 0; i < count; i = i + 2 {
v.write(w, m.Ctx[i], m.Ctx[i+1])
}
}
w.Write(newLine) //nolint:errcheck
if _, err := w.WriteTo(out); err != nil {
return fmt.Errorf("logx string write: %w", err)
}
return nil
}
func typing(v interface{}) string {
if v == nil {
return "null"
}
switch vv := v.(type) {
case error:
v = vv.Error()
case fmt.GoStringer:
v = vv.GoString()
case fmt.Stringer:
v = vv.String()
case encoding.TextMarshaler:
if b, err := vv.MarshalText(); err == nil {
v = string(b)
}
case encoding.BinaryMarshaler:
if b, err := vv.MarshalBinary(); err == nil {
v = string(b)
}
case []byte:
v = string(vv)
default:
}
s := fmt.Sprintf("%#v", v)
return strings.Trim(s, "\"")
}