Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
FishGoddess committed Apr 19, 2020
2 parents 632d2a5 + f8f4161 commit 2b84170
Show file tree
Hide file tree
Showing 19 changed files with 525 additions and 304 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# logs
*.log

# ide
.idea/
*.iml
Expand Down
14 changes: 7 additions & 7 deletions FUTURE.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
## ✒ 未来版本的新特性 (Features in future version)

### v0.1.6
* 完善 DurationRollingFile 结构,加入文件个数限制
* 完善 SizeRollingFile 结构,加入文件个数限制

### v0.1.5
* 更改内置日志处理器的换行符(\n)为系统的换行符
### v0.2.0
* 继续完善配置文件,主要针对内置的日志处理器做适配
* 合并目前的 DefaultHandler 和 JsonHandler
* DefaultHandler 和 wrapper 进行配置文件的适配
* JsonHandler 和 wrapper 进行配置文件的适配
* 加入 FileHandler,专门负责文件相关的日志处理器
* 增加分级日志处理器包装器,不同的级别可以使用不同的日志处理器

### v0.1.5
* 完善 Json 处理器没有做字符转义的修复方案,详情查询 [issue/1](https://github.com/FishGoddess/logit/issues/1)

### v0.1.4
* 紧急修复 Json 处理器没有做字符转义的 bug,详情查询 [issue/1](https://github.com/FishGoddess/logit/issues/1)
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.5
> 此版本发布于 2020-04-19
* 完善 Json 处理器没有做字符转义的修复方案,详情查询 [issue/1](https://github.com/FishGoddess/logit/issues/1)

### v0.1.4
> 此版本发布于 2020-04-10
* 紧急修复 Json 处理器没有做字符转义的 bug,详情查询 [issue/1](https://github.com/FishGoddess/logit/issues/1)
Expand Down
4 changes: 3 additions & 1 deletion README.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

_Check [HISTORY.md](./HISTORY.md) and [FUTURE.md](./FUTURE.md) to know about more information._

> v0.1.x is an interim version and will not be updated but fixed from now on. Next version v0.2.x is a big update which will bring new features and user experience, also it will be updated and maintained for a long time!
### 🚀 Installation

The only requirement is the [Golang Programming Language](https://golang.org).
Expand All @@ -42,7 +44,7 @@ module your_project_name
go 1.14

require (
github.com/FishGoddess/logit v0.1.4
github.com/FishGoddess/logit v0.1.5
)
```

Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

_历史版本的特性请查看 [HISTORY.md](./HISTORY.md)。未来版本的新特性和计划请查看 [FUTURE.md](./FUTURE.md)_

> v0.1.x 版本只是一个特性过渡版本,目前已经停止更新,只进行维护和修复。下一个大更新版本 v0.2.x 将带来全新的特性和使用体验(其中一点就是做减法),并保持长期更新和维护!
### 🚀 安装方式

唯一需要的依赖就是 [Golang 运行环境](https://golang.org).
Expand All @@ -43,7 +45,7 @@ module your_project_name
go 1.14

require (
github.com/FishGoddess/logit v0.1.4
github.com/FishGoddess/logit v0.1.5
)
```

Expand Down
14 changes: 14 additions & 0 deletions _examples/benchmarks_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// Copyright 2020 Ye Zi Jie. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: fish
// Email: [email protected]
// Created at 2020/03/02 20:51:29
Expand Down
41 changes: 36 additions & 5 deletions _examples/config/logit-config-template.cfg
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# logit 配置文件的模板 v0.1.4
# logit 配置文件的模板 v0.1.5

# 日志级别,可取值有 debug,info,warn,error,off
"level": "info",
Expand All @@ -7,15 +7,46 @@
# 注意记录文件信息会有运行时操作,比较消耗性能,确保您是必须要记录才开启这个选项
"caller": false,

# 日志处理器,您可以添加多个日志处理器
# 日志处理器,您可以添加多个日志处理器,可选值有 default,json
# 只有当你使用 RegisterHandler 将你自定义的日志处理器注册进 logit 才可以在这里使用
"handlers":{
"handlers": {
# 这里使用 Json 形式的日志处理器
# 注意:以下是每个日志处理器所需要的参数,并且会以键值对的形式注入到 RegisterHandler 的 newHandler 中
# 配置文件只负责把参数传递进去,要怎么使用这些参数是这个日志处理器自己的事情
# 所以不同的日志处理器的参数也不一定相同,需要参考具体日志处理器的文档描述
"json":{
"json": {
# 时间格式化样板,如果是 "" 就说明不格式化时间,使用 unix 形式
"timeFormat":"2006年01月02日"
"timeFormat": "2006年01月02日",

# 日志写出器
# 这个选项如果不设置就使用 os.Stdout 来输出日志
"writer": {
# 当使用 duration 的时候,可以设置时间间隔和日志输出的目标文件夹
# 当使用 size 的时候,可以设置文件最大大小和日志输出的目标文件夹
# 当使用 off 的时候,可以设置日志文件的路径

# 使用 duration 的范例
# rolling 是否需要使用滚动的日志机制,可选值有 duration,size,off
# 注意这个 duration 的 1 的单位是秒,也就是每隔一秒滚动一个日志文件
# directory 表示日志存储的文件夹,因为会有很多日志文件需要存储,所以这里配置的是文件夹
"rolling": "duration",
"duration": 1,
"directory": "D:/"

# 使用 size 的范例
# rolling 是否需要使用滚动的日志机制,可选值有 duration,size,off
# 注意这个 size 的 1 的单位是 MB,也就是日志文件达到 1 MB 之后就会滚动一个日志文件
# directory 表示日志存储的文件夹,因为会有很多日志文件需要存储,所以这里配置的是文件夹
#"rolling": "size",
#"size": 1,
#"directory": "D:/"

# 使用 size 的范例
# rolling 是否需要使用滚动的日志机制,可选值有 duration,size,off
# 注意这个 size 的 1 的单位是 MB,也就是日志文件达到 1 MB 之后就会滚动一个日志文件
# directory 表示日志存储的文件夹,因为会有很多日志文件需要存储,所以这里配置的是文件夹
#"rolling": "off",
#"file": "D:/me.log"
}
}
}
37 changes: 34 additions & 3 deletions _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.4
# logit config template v0.1.5

# Logger level, all valid value is debug,info,warn,error,off
"level": "info",
Expand All @@ -13,11 +13,42 @@
"handlers":{
# Use provided Json handler here
# Notice that what params can be set here is dependent to what handler you use
# Also, every params you set here will inject into newHandler in RegisterHandler in kv pair form
# Also, every params you set here will be injected into newHandler in RegisterHandler in kv pair form
# Config file only be responsible for injection, so handler has responsibility to decide how to use these params
# Different handler may have different params, check specific handler document to know about more information
"json":{
# how to format time, if this value is not set or is "", then unix format will be used
# How to format time, if this value is not set or is "", then unix format will be used
"timeFormat":"2006-01-02"

# Log writer
# Use os.Stdout if you do not set it up
"writer": {
# 当使用 duration 的时候,可以设置时间间隔和日志输出的目标文件夹
# 当使用 size 的时候,可以设置文件最大大小和日志输出的目标文件夹
# 当使用 off 的时候,可以设置日志文件的路径

# 使用 duration 的范例
# rolling 是否需要使用滚动的日志机制,可选值有 duration,size,off
# 注意这个 duration 的 1 的单位是秒,也就是每隔一秒滚动一个日志文件
# directory 表示日志存储的文件夹,因为会有很多日志文件需要存储,所以这里配置的是文件夹
"rolling": "duration",
"duration": 1,
"directory": "D:/"

# 使用 size 的范例
# rolling 是否需要使用滚动的日志机制,可选值有 duration,size,off
# 注意这个 size 的 1 的单位是 MB,也就是日志文件达到 1 MB 之后就会滚动一个日志文件
# directory 表示日志存储的文件夹,因为会有很多日志文件需要存储,所以这里配置的是文件夹
#"rolling": "size",
#"size": 1,
#"directory": "D:/"

# 使用 size 的范例
# rolling 是否需要使用滚动的日志机制,可选值有 duration,size,off
# 注意这个 size 的 1 的单位是 MB,也就是日志文件达到 1 MB 之后就会滚动一个日志文件
# directory 表示日志存储的文件夹,因为会有很多日志文件需要存储,所以这里配置的是文件夹
#"rolling": "off",
#"file": "D:/me.log"
}
}
}
52 changes: 26 additions & 26 deletions _examples/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,49 +19,49 @@
package main

import (
"fmt"
"os"
"fmt"
"os"

"github.com/FishGoddess/logit"
"github.com/FishGoddess/logit"
)

type myHandler struct{}

// Customize your own handler.
func (mh *myHandler) Handle(log *logit.Log) bool {
os.Stdout.Write([]byte("myHandler: "))
os.Stdout.Write(logit.EncodeToJson(log, "")) // Try `os.Stdout.WriteString(log.Msg())` ?
return true
os.Stdout.Write([]byte("myHandler: "))
os.Stdout.Write(logit.EncodeToJson(log, "")) // Try `os.Stdout.WriteString(log.Msg())` ?
return true
}

// We recommend you to register your handler to logit, so that
// you can use your handler by logit.HandlerOf.
// See logit.HandlerOf.
func init() {
logit.RegisterHandler("myHandler", func(params map[string]string) logit.Handler {
return &myHandler{}
})
logit.RegisterHandler("myHandler", func(params map[string]interface{}) logit.Handler {
return &myHandler{}
})
}

func main() {

// Create a logger holder.
// Default handler is logit.DefaultHandler.
logger := logit.NewDevelopLogger()
logger.Info("before adding handlers...")
// Create a logger holder.
// Default handler is logit.DefaultHandler.
logger := logit.NewDevelopLogger()
logger.Info("before adding handlers...")

// Add handlers to logger.
// There are two handlers in logger because logger has a default handler inside after creating.
// See logit.DefaultHandler.
logger.AddHandlers(&myHandler{}, logit.HandlerOf("json", map[string]string{}))
fmt.Println("fmt =========================================")
logger.Info("after adding handlers...")
// Add handlers to logger.
// There are two handlers in logger because logger has a default handler inside after creating.
// See logit.DefaultHandler.
logger.AddHandlers(&myHandler{}, logit.HandlerOf("json", map[string]interface{}{}))
fmt.Println("fmt =========================================")
logger.Info("after adding handlers...")

// Set handlers to logger.
// There are two handlers in logger because the default handler inside was removed.
// If you register your handler to logit by logit.RegisterHandler, then you can
// use your handler everywhere like this:
logger.SetHandlers(logit.HandlerOf("myHandler", map[string]string{}))
fmt.Println("fmt =========================================")
logger.Info("after setting handlers...")
// Set handlers to logger.
// There are two handlers in logger because the default handler inside was removed.
// If you register your handler to logit by logit.RegisterHandler, then you can
// use your handler everywhere like this:
logger.SetHandlers(logit.HandlerOf("myHandler", map[string]interface{}{}))
fmt.Println("fmt =========================================")
logger.Info("after setting handlers...")
}
11 changes: 4 additions & 7 deletions _examples/logger.cfg
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
"level":"debug",
"level": "debug",

"handlers":{
"default":{
"timeFormat" : "2006-01-02 15:04"
},
"json":{
# "timeFormat" : "2006/01/02"
"handlers": {
"default": {
"timeFormat": "2006-01-02 15:04"
}
}
86 changes: 43 additions & 43 deletions _examples/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,58 +19,58 @@
package main

import (
"math/rand"
"os"
"strconv"
"time"
"math/rand"
"os"
"strconv"
"time"

"github.com/FishGoddess/logit"
"github.com/FishGoddess/logit"
)

func main() {

// NewDevelopLogger creates a new Logger holder for developing, generally log to terminal or console.
// You can switch to logit.NewProductionLogger for production environment.
//logger := logit.NewProductionLogger(os.Stdout)
logger := logit.NewDevelopLogger()
// NewDevelopLogger creates a new Logger holder for developing, generally log to terminal or console.
// You can switch to logit.NewProductionLogger for production environment.
//logger := logit.NewProductionLogger(os.Stdout)
logger := logit.NewDevelopLogger()

// Then you will be easy to log!
logger.Debug("this is a debug message!")
logger.Info("this is an info message!")
logger.Warn("this is a warn message!")
logger.Error("this is an error message!")
// Then you will be easy to log!
logger.Debug("this is a debug message!")
logger.Info("this is an info message!")
logger.Warn("this is a warn message!")
logger.Error("this is an error message!")

// NewLoggerWithoutEncoder creates a new Logger holder with given level and handlers.
// As you know, file also can be written, just replace os.Stdout with your file!
// A logger is made of level and handlers, so we provide some handlers for use, see logit.Handler.
// This method is the most original way to create a logger for use.
logger = logit.NewLogger(logit.DebugLevel, logit.NewDefaultHandler(os.Stdout, "2006/01/02 15:04:05"))
logger.Info("What time is it now?")
// NewLoggerWithoutEncoder creates a new Logger holder with given level and handlers.
// As you know, file also can be written, just replace os.Stdout with your file!
// A logger is made of level and handlers, so we provide some handlers for use, see logit.Handler.
// This method is the most original way to create a logger for use.
logger = logit.NewLogger(logit.DebugLevel, logit.NewDefaultHandler(os.Stdout, "2006/01/02 15:04:05"))
logger.Info("What time is it now?")

// For convenience, we provide a register mechanism and you can use handlers like this:
logger = logit.NewLogger(logit.DebugLevel, logit.HandlerOf("default", map[string]string{}))
logger.Info("What handler is it now?")
// For convenience, we provide a register mechanism and you can use handlers like this:
logger = logit.NewLogger(logit.DebugLevel, logit.HandlerOf("default", map[string]interface{}{}))
logger.Info("What handler is it now?")

// NewLoggerFrom creates a new Logger holder with given config.
// The config has all the things to create a logger, such as level.
// We provide some encoders: default encoder and json encoder.
// See logit.Encoder to check more information.
logger = logit.NewLoggerFrom(logit.Config{
Level: logit.DebugLevel,
Handlers: []logit.Handler{logit.NewJsonHandler(os.Stdout, "")},
})
logger.Info("I am a json log!")
// NewLoggerFrom creates a new Logger holder with given config.
// The config has all the things to create a logger, such as level.
// We provide some encoders: default encoder and json encoder.
// See logit.Encoder to check more information.
logger = logit.NewLoggerFrom(logit.Config{
Level: logit.DebugLevel,
Handlers: []logit.Handler{logit.NewJsonHandler(os.Stdout, "")},
})
logger.Info("I am a json log!")

// If you want to output log with file info, try this:
logger.EnableFileInfo()
logger.Info("What file is it? Which line?")
logger.DisableFileInfo()
// If you want to output log with file info, try this:
logger.EnableFileInfo()
logger.Info("What file is it? Which line?")
logger.DisableFileInfo()

// If you have a long log and it is made of many variables, try this:
// The msg is the return value of msgGenerator.
logger.DebugFunc(func() string {
// Use time as the source of random number generator.
r := rand.New(rand.NewSource(time.Now().Unix()))
return "debug rand int: " + strconv.Itoa(r.Intn(100))
})
// If you have a long log and it is made of many variables, try this:
// The msg is the return value of msgGenerator.
logger.DebugFunc(func() string {
// Use time as the source of random number generator.
r := rand.New(rand.NewSource(time.Now().Unix()))
return "debug rand int: " + strconv.Itoa(r.Intn(100))
})
}
Loading

0 comments on commit 2b84170

Please sign in to comment.