From 8f2958146439665e5b4549b4e12b084a4b800442 Mon Sep 17 00:00:00 2001 From: FishGoddess <1149062639@qq.com> Date: Fri, 10 Apr 2020 23:41:35 +0800 Subject: [PATCH] =?UTF-8?q?=E7=B4=A7=E6=80=A5=E4=BF=AE=E5=A4=8D=20Json=20?= =?UTF-8?q?=E5=A4=84=E7=90=86=E5=99=A8=E6=B2=A1=E6=9C=89=E5=81=9A=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E8=BD=AC=E4=B9=89=E7=9A=84=20bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FUTURE.md | 7 +++++-- HISTORY.md | 4 ++++ README.en.md | 2 +- README.md | 2 +- _examples/config/logit-config-template.cfg | 2 +- _examples/config/logit-config-template.en.cfg | 2 +- doc.go | 2 +- handler.go | 11 +++++++++-- logger_extension_test.go | 7 ++++++- 9 files changed, 29 insertions(+), 10 deletions(-) diff --git a/FUTURE.md b/FUTURE.md index 0d5ea71..23b57c0 100644 --- a/FUTURE.md +++ b/FUTURE.md @@ -1,15 +1,18 @@ ## ✒ 未来版本的新特性 (Features in future version) -### v0.1.5 +### v0.1.6 * 完善 DurationRollingFile 结构,加入文件个数限制 * 完善 SizeRollingFile 结构,加入文件个数限制 -### v0.1.4 +### v0.1.5 * 更改内置日志处理器的换行符(\n)为系统的换行符 * 继续完善配置文件,主要针对内置的日志处理器做适配 * DefaultHandler 和 wrapper 进行配置文件的适配 * JsonHandler 和 wrapper 进行配置文件的适配 +### v0.1.4 +* 紧急修复 Json 处理器没有做字符转义的 bug,详情查询 [issue/1](https://github.com/FishGoddess/logit/issues/1) + ### v0.1.3 * 增加配置文件中是否开启文件信息记录的选项 diff --git a/HISTORY.md b/HISTORY.md index 6f0420f..816a770 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,5 +1,9 @@ ## ✒ 历史版本的特性介绍 (Features in old version) +### v0.1.4 +> 此版本发布于 2020-04-10 +* 紧急修复 Json 处理器没有做字符转义的 bug,详情查询 [issue/1](https://github.com/FishGoddess/logit/issues/1) + ### v0.1.3 > 此版本发布于 2020-04-05 * 增加配置文件中是否开启文件信息记录的选项 diff --git a/README.en.md b/README.en.md index a8a86ad..9bbb38c 100644 --- a/README.en.md +++ b/README.en.md @@ -42,7 +42,7 @@ module your_project_name go 1.14 require ( - github.com/FishGoddess/logit v0.1.3 + github.com/FishGoddess/logit v0.1.4 ) ``` diff --git a/README.md b/README.md index a829247..2890c6c 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ module your_project_name go 1.14 require ( - github.com/FishGoddess/logit v0.1.3 + github.com/FishGoddess/logit v0.1.4 ) ``` diff --git a/_examples/config/logit-config-template.cfg b/_examples/config/logit-config-template.cfg index 5fbb212..f8b1f1e 100644 --- a/_examples/config/logit-config-template.cfg +++ b/_examples/config/logit-config-template.cfg @@ -1,4 +1,4 @@ -# logit 配置文件的模板 v0.1.3 +# logit 配置文件的模板 v0.1.4 # 日志级别,可取值有 debug,info,warn,error,off "level": "info", diff --git a/_examples/config/logit-config-template.en.cfg b/_examples/config/logit-config-template.en.cfg index 92c2458..12e0ec5 100644 --- a/_examples/config/logit-config-template.en.cfg +++ b/_examples/config/logit-config-template.en.cfg @@ -1,4 +1,4 @@ -# logit config template v0.1.3 +# logit config template v0.1.4 # Logger level, all valid value is debug,info,warn,error,off "level": "info", diff --git a/doc.go b/doc.go index 99cda7d..ccc3927 100644 --- a/doc.go +++ b/doc.go @@ -212,4 +212,4 @@ Package logit provides an easy way to use foundation for your logging operations package logit // import "github.com/FishGoddess/logit" // Version is the version string representation of logit. -const Version = "v0.1.3" +const Version = "v0.1.4" diff --git a/handler.go b/handler.go index fd1b5f3..f660ef7 100644 --- a/handler.go +++ b/handler.go @@ -24,6 +24,7 @@ import ( "io" "os" "strconv" + "strings" "sync" ) @@ -145,16 +146,22 @@ func EncodeToJson(log *Log, timeFormat string) []byte { // 如果有文件信息,就把文件信息也加进去 if log.file != "" && log.Line() != 0 { - buffer.WriteString(`, "file":"` + log.File()) + buffer.WriteString(`, "file":"` + escapeString(log.File())) buffer.WriteString(`", "line":` + strconv.Itoa(log.Line())) } buffer.WriteString(`, "msg":"`) - buffer.WriteString(log.Msg()) + buffer.WriteString(escapeString(log.Msg())) buffer.WriteString("\"}\n") return buffer.Bytes() } +// escapeString is for escaping string from special character, such as double quotes. +// See issue: https://github.com/FishGoddess/logit/issues/1 +func escapeString(s string) string { + return strings.ReplaceAll(s, `"`, `\"`) +} + // DefaultHandler is a default handler for use. // Generally speaking, encoding a log to bytes then written by writer is the most of // handlers do. So we provide a default handler, which only need a writer and an encoder. diff --git a/logger_extension_test.go b/logger_extension_test.go index 7f63e01..d5af0d4 100644 --- a/logger_extension_test.go +++ b/logger_extension_test.go @@ -20,6 +20,7 @@ package logit import ( "math/rand" + "os" "strconv" "testing" "time" @@ -91,7 +92,8 @@ func TestNewDefaultSizeRollingLogger(t *testing.T) { // 测试输出日志是从函数中生成的几个方法 func TestLoggerLogFunction(t *testing.T) { - logger := NewDevelopLogger() + logger := NewProductionLogger(os.Stdout) + logger.ChangeLevelTo(DebugLevel) logger.DebugFunc(func() string { return "debug rand int: " + strconv.Itoa(rand.Intn(100)) }) @@ -104,6 +106,9 @@ func TestLoggerLogFunction(t *testing.T) { logger.ErrorFunc(func() string { return "error rand int: " + strconv.Itoa(rand.Intn(100)) }) + + // test double quotes + logger.Info(`test "double quotes" !!!!`) } // 测试从配置文件中创建一个 logger