Skip to content

Commit

Permalink
*: replace old encoder with new encoder (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
xhebox authored Oct 27, 2022
1 parent ac74724 commit fa4b426
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
2 changes: 1 addition & 1 deletion cmd/tiproxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func main() {
}

configFile := rootCmd.PersistentFlags().String("config", "conf/proxy.yaml", "proxy config file path")
logEncoder := rootCmd.PersistentFlags().String("log_encoder", "", "log in format of tidb, console, or json")
logEncoder := rootCmd.PersistentFlags().String("log_encoder", "tidb", "log in format of tidb, console, or json")
logLevel := rootCmd.PersistentFlags().String("log_level", "", "log level")
clusterName := rootCmd.PersistentFlags().String("cluster_name", "tiproxy", "default cluster name, used to generate node name and differential clusters in dns discovery")
nodeName := rootCmd.PersistentFlags().String("node_name", "", "by default, it is generate prefixed by cluster-name")
Expand Down
19 changes: 12 additions & 7 deletions lib/util/cmd/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"strings"
"time"
"unicode"
"unicode/utf8"

"go.uber.org/zap/buffer"
Expand Down Expand Up @@ -171,12 +172,14 @@ outerloop:
f.line.AppendString("\\n")
case '\r':
f.line.AppendString("\\r")
case '\t':
f.line.AppendString("\\t")
default:
if r >= 0x20 {
f.line.AppendByte(s[i])
} else {
if unicode.IsControl(r) {
f.line.AppendString(`\u`)
fmt.Fprintf(f.line, "%4x", r)
fmt.Fprintf(f.line, "%04x", r)
} else {
f.line.AppendByte(s[i])
}
}
} else {
Expand Down Expand Up @@ -334,9 +337,11 @@ func (s *tidbEncoder) AddUintptr(key string, val uintptr) {
func (s *tidbEncoder) AddReflected(key string, obj interface{}) error {
s.beginQuoteFiled()
s.addKey(key)
s.AppendReflected(obj)
s.endQuoteFiled()
return nil
err := s.AppendReflected(obj)
if err == nil {
s.endQuoteFiled()
}
return err
}
func (s *tidbEncoder) OpenNamespace(key string) {
s.addKey(key)
Expand Down
2 changes: 1 addition & 1 deletion lib/util/cmd/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ func TestEncoder(t *testing.T) {

// test append reflected
enc = enc.clone(false)
enc.AddReflected("ff", struct{ A string }{"\""})
require.NoError(t, enc.AddReflected("ff", struct{ A string }{"\""}))
require.Equal(t, "[ff=\"{\\\"A\\\":\\\"\\\\\"\\\"}\"]", enc.line.String())
}
9 changes: 4 additions & 5 deletions lib/util/cmd/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,17 @@ package cmd

import (
"github.com/pingcap/TiProxy/lib/config"
"github.com/pingcap/log"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

func buildEncoder(cfg *config.Log) (zapcore.Encoder, error) {
encfg := zap.NewProductionEncoderConfig()
switch cfg.Encoder {
case "tidb":
return log.NewTextEncoder(&log.Config{})
case "newtidb":
fallthrough
case "json":
return zapcore.NewJSONEncoder(encfg), nil
case "console":
return zapcore.NewConsoleEncoder(encfg), nil
default:
return NewTiDBEncoder(encfg), nil
}
Expand Down

0 comments on commit fa4b426

Please sign in to comment.