Skip to content

Commit

Permalink
v0.1.1 版本预发布
Browse files Browse the repository at this point in the history
  • Loading branch information
FishGoddess committed Mar 29, 2020
2 parents a8147dd + 2bff116 commit 39931b3
Show file tree
Hide file tree
Showing 19 changed files with 373 additions and 442 deletions.
13 changes: 10 additions & 3 deletions FUTURE.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
## ✒ 未来版本的新特性 (Features in future version)

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

### v0.1.1
### v0.1.2
* 加入配置文件的支持

### v0.1.0
### v0.1.1-alpha
* 再次对 Handler 进行重构,尽量优化 Logger 的设计
* 去除 Encoder,减少多余的设计,轻量化 Logger
* 取消时间缓存机制,减少并发竞争性
* 优化 releaseLog 的 extra 内存分配
* 加入 FileConfig,为后续支持配置文件做准备

### v0.1.0-alpha
* 重新设计 Logger,主要是轻量化处理和重构 handler 的设计
* 增加 Encoder 接口,方便用户扩展 Logger,并内置 Json 编码器
* Json 编码器允许时间不做格式化,使用 Unix 形式处理时间,方便解析处理
Expand Down
11 changes: 10 additions & 1 deletion HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
## ✒ 历史版本的特性介绍 (Features in old version)

### v0.1.0
### v0.1.1-alpha
> 此版本发布于 2020-03-29
* 再次对 Handler 进行重构,尽量优化 Logger 的设计
* 去除 Encoder,减少多余的设计,轻量化 Logger
* 取消时间缓存机制,减少并发竞争性
* 优化 releaseLog 的 extra 内存分配
* 加入 FileConfig,为后续支持配置文件做准备

### v0.1.0-alpha
> 此版本发布于 2020-03-27
* 重新设计 Logger,主要是轻量化处理和重构 handler 的设计
* 增加 Encoder 接口,方便用户扩展 Logger,并内置 Json 编码器
* Json 编码器允许时间不做格式化,使用 Unix 形式处理时间,方便解析处理
Expand Down
8 changes: 4 additions & 4 deletions 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.0
github.com/FishGoddess/logit v0.1.1-alpha
)
```

Expand Down Expand Up @@ -95,10 +95,9 @@ func main() {
* [basic](./_examples/basic.go)
* [logger](./_examples/logger.go)
* [level_and_disable](./_examples/level_and_disable.go)
* [log_to_file](./_examples/log_to_file.go)
* [handler](./_examples/handler.go)
* [wrapper](./_examples/wrapper.go)
* [encoder](./_examples/encoder.go)
* [log_to_file](./_examples/log_to_file.go)

_Check more examples in [_examples](./_examples)._

Expand All @@ -112,7 +111,7 @@ $ go test -v ./_examples/benchmarks_test.go -bench=. -benchtime=1s
| test case | times ran (large is better) | ns/op (small is better) | features | extension |
| -----------|--------|-------------|-------------|-------------|
| **logit** | **1242982** |   **960 ns/op** | powerful | high |
| **logit** |   **667340** | **1844 ns/op** | powerful | high |
| zap |   401043 | 2793 ns/op | normal | normal |
| logrus |   158262 | 7751 ns/op | normal | normal |
| golog |   751064 | 1614 ns/op | normal | normal |
Expand All @@ -133,6 +132,7 @@ $ go test -v ./_examples/benchmarks_test.go -bench=. -benchtime=1s
**3. After checking the benchmarks of v0.0.8 version, we found that time format takes a lots of time**
**because of time.Time.AppendFormat. In v0.0.11 and higher versions, we use time cache mechanism to**
**reduce the times of time format. However, is it worth to replace time format operation with concurrent competition?**
**The answer is no, so we cancel this mechanism in v0.1.1-alpha and higher versions.**

### 👥 Contributing

Expand Down
13 changes: 5 additions & 8 deletions README.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.0
github.com/FishGoddess/logit v0.1.1-alpha
)
```

Expand All @@ -67,14 +67,11 @@ import (

func main() {

// Log as you want.
// Log messages with four levels.
logit.Debug("I am a debug message!")
logit.Info("I am an info message!")
logit.Warn("I am a warn message!")
logit.Error("I am an error message!")

// Change logger level.
logit.ChangeLevelTo(logit.DebugLevel)

// If you want to output log with file info, try this:
logit.EnableFileInfo()
Expand All @@ -95,10 +92,9 @@ func main() {
* [basic](./_examples/basic.go)
* [logger](./_examples/logger.go)
* [level_and_disable](./_examples/level_and_disable.go)
* [log_to_file](./_examples/log_to_file.go)
* [handler](./_examples/handler.go)
* [wrapper](./_examples/wrapper.go)
* [encoder](./_examples/encoder.go)
* [log_to_file](./_examples/log_to_file.go)

_更多使用案例请查看 [_examples](./_examples) 目录。_

Expand All @@ -112,7 +108,7 @@ $ go test -v ./_examples/benchmarks_test.go -bench=. -benchtime=1s
| 测试 | 单位时间内运行次数 (越大越好) | 每个操作消耗时间 (越小越好) | 功能性 | 扩展性 |
| -----------|--------|-------------|-------------|-------------|
| **logit** | **1242982** |   **960 ns/op** | 强大 ||
| **logit** |   **667340** | **1844 ns/op** | 强大 ||
| zap |   401043 | 2793 ns/op | 正常 | 正常 |
| logrus |   158262 | 7751 ns/op | 正常 | 正常 |
| golog |   751064 | 1614 ns/op | 正常 | 正常 |
Expand All @@ -133,6 +129,7 @@ $ go test -v ./_examples/benchmarks_test.go -bench=. -benchtime=1s
**3. 经过对 v0.0.8 版本的性能检测,发现时间格式化操作消耗了接近一半的处理时间,**
**主要体现在 time.Time.AppendFormat 的调用上。在 v0.0.11 版本中使用了时间缓存机制进行优化,**
**目前存在一个疑惑就是使用并发竞争去换取时间格式化的性能消耗究竟值不值得?**
**答案是不值得,我们在 v0.1.1-alpha 及更高版本中取消了这个时间缓存机制。**

### 👥 贡献者

Expand Down
16 changes: 13 additions & 3 deletions _examples/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,30 @@
package main

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

"github.com/FishGoddess/logit"
)

func main() {

// Log messages with four levels.
// Notice that the default level is info, so first line of debug message
// will not be logged! If you want to change level, see logit.ChangeLevelTo
logit.Debug("I am a debug message! But I will not be logged in default level!")
logit.Debug("I am a debug message!")
logit.Info("I am an info message!")
logit.Warn("I am a warn message!")
logit.Error("I am an error message!")

// If you want to output log with file info, try this:
logit.EnableFileInfo()
logit.Info("Show file info!")

// If you have a long log and it is made of many variables, try this:
// The msg is the return value of msgGenerator.
logit.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))
})
}
2 changes: 1 addition & 1 deletion _examples/benchmarks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (nw *nopWriter) Write(p []byte) (n int, err error) {
func BenchmarkLogitLogger(b *testing.B) {

// 测试用的日志记录器
logger := logit.NewLogger(logit.DebugLevel, logit.NewDefaultHandler(&nopWriter{}, logit.NewDefaultEncoder(logit.DefaultTimeFormat)))
logger := logit.NewLogger(logit.DebugLevel, logit.NewDefaultHandler(&nopWriter{}, logit.DefaultTimeFormat))

// 测试用的日志任务
logTask := func() {
Expand Down
59 changes: 0 additions & 59 deletions _examples/encoder.go

This file was deleted.

18 changes: 15 additions & 3 deletions _examples/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,20 @@ type myHandler struct{}

// Customize your own handler.
func (mh *myHandler) Handle(log *logit.Log) bool {
os.Stdout.WriteString("handler1: " + log.Msg() + "\n")
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() logit.Handler {
return &myHandler{}
})
}

func main() {

// Create a logger holder.
Expand All @@ -43,13 +53,15 @@ func main() {
// 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{})
logger.AddHandlers(&myHandler{}, logit.HandlerOf("json"))
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.
logger.SetHandlers(&myHandler{})
// If you register your handler to logit by logit.RegisterHandler, then you can
// use your handler everywhere like this:
logger.SetHandlers(logit.HandlerOf("myHandler"))
fmt.Println("fmt =========================================")
logger.Info("after setting handlers...")
}
10 changes: 5 additions & 5 deletions _examples/level_and_disable.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ func main() {

logit.Debug("Default logger level is debug.")

// Change logger level to warn level.
// So debug and info log will be ignored.
logit.ChangeLevelTo(logit.WarnLevel)
// Change logger level to info level.
// So debug log will be ignored.
logit.ChangeLevelTo(logit.InfoLevel)
logit.Debug("You never see me!")

// In particular, you can change level to OffLevel to disable the logger.
// So the info message next line will not be logged!
logit.ChangeLevelTo(logit.OffLevel)
level := logit.ChangeLevelTo(logit.OffLevel)
logit.Info("I will not be logged!")

// Enable the Logger.
// The info message next line will be logged again!
logit.ChangeLevelTo(logit.InfoLevel)
logit.ChangeLevelTo(level)
logit.Info("I am running again!")
}
13 changes: 8 additions & 5 deletions _examples/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,24 @@ func main() {
logger.Warn("this is a warn message!")
logger.Error("this is an error message!")

// NewLogger creates a new Logger holder with given level and handlers.
// 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, logit.NewDefaultEncoder("2006/01/02 15:04:05")))
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"))
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,
Writer: os.Stdout,
Encoder: logit.NewJsonEncoder("2006/01/02 15:04:05", false),
Level: logit.DebugLevel,
Handlers: []logit.Handler{logit.NewJsonHandler(os.Stdout, "")},
})
logger.Info("I am a json log!")

Expand Down
19 changes: 9 additions & 10 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,15 @@ type Config struct {
// If the level of log is smaller than this Level, this log will be ignored.
Level Level

Handlers []Handler
}

// FileConfig is the config mapping a file.
type FileConfig struct {

// Config means that a file config is also a config.
Config

// Writer is where to write a log.
Writer io.Writer

// Encoder is how to encode a log to be writable.
// There are two encoders for you:
// 1. DefaultEncoder
// 2. JsonEncoder
//
// DefaultEncoder encodes a log to a plain string like "[Info] [2020-03-06 16:10:44] msg" in bytes.
// JsonEncoder encodes a log to a Json string like `{"level":"debug", "time":"2020-03-22 22:35:00", "msg":"log content..."}` in bytes.
// Of cause, you can implement Encoder interface to do you encoding job in you own way.
Encoder Encoder
}
Loading

0 comments on commit 39931b3

Please sign in to comment.