This commit is contained in:
Aliaksandr Valialkin 2024-05-20 12:36:20 +02:00
parent 353b0661a7
commit 00294e5d0d
No known key found for this signature in database
GPG key ID: 52C003EE2BCDB9EB

View file

@ -72,7 +72,8 @@ var parserPool sync.Pool
// //
// The p.Fields remains valid until the next call to PutJSONParser(). // The p.Fields remains valid until the next call to PutJSONParser().
func (p *JSONParser) ParseLogMessageNoResetBuf(msg, prefix string) error { func (p *JSONParser) ParseLogMessageNoResetBuf(msg, prefix string) error {
return p.parseLogMessage(msg, prefix, false) p.resetNobuf()
return p.parseLogMessage(msg, prefix)
} }
// ParseLogMessage parses the given JSON log message msg into p.Fields. // ParseLogMessage parses the given JSON log message msg into p.Fields.
@ -82,10 +83,11 @@ func (p *JSONParser) ParseLogMessageNoResetBuf(msg, prefix string) error {
// The p.Fields remains valid until the next call to ParseLogMessage() or PutJSONParser(). // The p.Fields remains valid until the next call to ParseLogMessage() or PutJSONParser().
func (p *JSONParser) ParseLogMessage(msg []byte, prefix string) error { func (p *JSONParser) ParseLogMessage(msg []byte, prefix string) error {
msgStr := bytesutil.ToUnsafeString(msg) msgStr := bytesutil.ToUnsafeString(msg)
return p.parseLogMessage(msgStr, prefix, true) p.reset()
return p.parseLogMessage(msgStr, prefix)
} }
func (p *JSONParser) parseLogMessage(msg, prefix string, resetBuf bool) error { func (p *JSONParser) parseLogMessage(msg, prefix string) error {
v, err := p.p.Parse(msg) v, err := p.p.Parse(msg)
if err != nil { if err != nil {
return fmt.Errorf("cannot parse json: %w", err) return fmt.Errorf("cannot parse json: %w", err)
@ -93,11 +95,6 @@ func (p *JSONParser) parseLogMessage(msg, prefix string, resetBuf bool) error {
if t := v.Type(); t != fastjson.TypeObject { if t := v.Type(); t != fastjson.TypeObject {
return fmt.Errorf("expecting json dictionary; got %s", t) return fmt.Errorf("expecting json dictionary; got %s", t)
} }
if resetBuf {
p.reset()
} else {
p.resetNobuf()
}
p.prefixBuf = append(p.prefixBuf[:0], prefix...) p.prefixBuf = append(p.prefixBuf[:0], prefix...)
p.Fields, p.buf, p.prefixBuf = appendLogFields(p.Fields, p.buf, p.prefixBuf, v) p.Fields, p.buf, p.prefixBuf = appendLogFields(p.Fields, p.buf, p.prefixBuf, v)
return nil return nil