Skip to content

Commit

Permalink
紧急修复 Json 处理器没有做字符转义的 bug
Browse files Browse the repository at this point in the history
  • Loading branch information
FishGoddess committed Apr 10, 2020
2 parents 4e11548 + 8f29581 commit 632d2a5
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 10 deletions.
7 changes: 5 additions & 2 deletions FUTURE.md
Original file line number Diff line number Diff line change
@@ -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
* 增加配置文件中是否开启文件信息记录的选项

Expand Down
4 changes: 4 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -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
* 增加配置文件中是否开启文件信息记录的选项
Expand Down
2 changes: 1 addition & 1 deletion README.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
```

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
```

Expand Down
2 changes: 1 addition & 1 deletion _examples/config/logit-config-template.cfg
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# logit 配置文件的模板 v0.1.3
# logit 配置文件的模板 v0.1.4

# 日志级别,可取值有 debug,info,warn,error,off
"level": "info",
Expand Down
2 changes: 1 addition & 1 deletion _examples/config/logit-config-template.en.cfg
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
11 changes: 9 additions & 2 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"io"
"os"
"strconv"
"strings"
"sync"
)

Expand Down Expand Up @@ -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.
Expand Down
7 changes: 6 additions & 1 deletion logger_extension_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package logit

import (
"math/rand"
"os"
"strconv"
"testing"
"time"
Expand Down Expand Up @@ -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))
})
Expand All @@ -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
Expand Down

0 comments on commit 632d2a5

Please sign in to comment.