1

I have a script to print the same log like:

type Country stuct{
  cityName string
}

var c Country

and I

var logger = logrus.New()
logger.Debug(c)

but in the log file, it looks like:

country_name:\"\\347\\276\\216\\345\\233\\275\" 

I want to know what \\347\\276\\216\\345\\233\\275\ means and how to read the true meanwhile my script read the log file.

2
  • What is logger in the posted code? The string "\347\276\216\345\233\275\" contains the octal escapes for "美国". Commented Dec 11, 2019 at 8:10
  • var logger = logrus.New() Commented Dec 11, 2019 at 8:18

2 Answers 2

3

The default logrus formatter writes JSON with ASCII encoding. You need to implement your own formatter and use UTF-8. See Logger.SetFormatter.

As I looked into the source code I didn't see any setting that can be set to change the encoding.

Sign up to request clarification or add additional context in comments.

Comments

0
func convertOctonaryUtf8(in string) string {
    s := []byte(in)
    reg := regexp.MustCompile(`\\[0-7]{3}`)

    out := reg.ReplaceAllFunc(s,
        func(b []byte) []byte {
            i, _ := strconv.ParseInt(string(b[1:]), 8, 0)
            return []byte{byte(i)}
        })
    return string(out)
}

the func get the "\347\276\216\345\233\275\" mean

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.