Skip to content

Commit

Permalink
祝大家平安夜、圣诞节快乐!
Browse files Browse the repository at this point in the history
  • Loading branch information
FishGoddess committed Dec 24, 2020
2 parents b2b8cb9 + 9687302 commit cd7601a
Show file tree
Hide file tree
Showing 50 changed files with 1,749 additions and 3,739 deletions.
3 changes: 3 additions & 0 deletions FUTURE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## ✒ 未来版本的新特性 (Features in future versions)

### v0.3.3
* 使用多个变量替代 map,避免哈希消耗性能

### v0.3.x
经过一段时间的实际使用,发现了一些不太方便的地方需要进行调整。
* Handler 的设计太过于抽象,导致很多日志库本身的功能实现过于剥离、插件化
Expand Down
18 changes: 18 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
## ✒ 历史版本的特性介绍 (Features in old versions)

### v0.3.2
> 此版本发布于 2020-12-24
* 废弃了 files 包
* 完善文档,第一个 v0.3.x 正式版
* 最后,祝大家平安夜、圣诞节快乐!

### v0.3.1-alpha
> 此版本发布于 2020-12-13
* 日志文件自动分割处理,支持 Checker 机制检查是否需要分割
* 内置三种 Checker,分别是 TimeChecker,SizeChecker,CountChecker
* 多种检查器可以叠加效果,只要其中有一个达到分割的条件都会进行分割

### v0.3.0-alpha
> 此版本发布于 2020-11-28
* 大幅度重构版本,废除了 handler 设计
* 暂不支持配置文件,正式版会支持
* v0.3.x 的第一个体验版

### v0.2.10
> 此版本发布于 2020-08-22
* 完善配置文件注释,目前是 // 和 # 都支持,后续将只支持 //
Expand Down
129 changes: 58 additions & 71 deletions README.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,110 +3,97 @@
[![License](_icon/license.svg)](https://www.apache.org/licenses/LICENSE-2.0.html)
[![Go Doc](_icon/godoc.svg)](https://pkg.go.dev/github.com/FishGoddess/logit?tab=doc)

**logit** is an easy-to-use, also level-based and config file first logger for [GoLang](https://golang.org) applications.
**logit** is a level-based and high-performance logger for [GoLang](https://golang.org) applications.

[阅读中文版的 Read me](./README.md)

[Introduction Video on BiliBili](https://www.bilibili.com/video/BV14t4y1y7rF)

### 🥇 Features

* Modularization design, easy to extend your logger with wrapper and handler
* Modularization design, easy to extend your logger with encoders and writers
* Level-based logging, and there are four levels to use
* Config file supports, you can use a config file to change you logger flexibility even it has been a binary
* Log Function supports, it is a better way to output a very long log
* Enable or disable Logger, you can disable or switch to a higher level in your production environment
* Log file supports, and you can customer the name of your log file
* Duration rolling supports, which means it will roll to a new log file by duration automatically, such as one day one log file
* File size rolling supports, which means it will roll to a new log file by file size automatically, such as one 64 MB one log file
* Log handler supports, you can extend logger with your own log handler easily
* Time rolling supports, such as one day one log file
* File size rolling supports, such as one 64 MB one log file
* Count rolling supports, such as 1000 logging operations one log file
* High-performance supports, by avoiding to call runtime.Caller
* Time format supports, you can format time in your way
* Log as Json string supports, by using provided JsonLoggerHandler

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

> v0.1.x and older versions will not be supported anymore, please update to v0.2.x as quickly as possible. You will get some brand-new user experiences and supports for a long time!
> The brand-new version v0.3.x is released with a more elegant design!
### 🚀 Installation

```bash
$ go get -u github.com/FishGoddess/logit
$ go get github.com/FishGoddess/logit
```

> Or edit your project's go.mod file if you are using Go modules.
```bash
module your_project_name

go 1.14

require (
github.com/FishGoddess/logit v0.2.10
)
```

logit has no more external dependencies.
### 📖 Examples

```go
package main

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

"github.com/FishGoddess/logit"
)

func main() {

// 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!")

// Notice that logit has blocked some methods for more refreshing method list.
// If you want to use some higher level methods, you should call logit.Me() to
// get the fully functional logger, then call what you want to call.
// For example, if you want to output log with file info, try this:
logit.Me().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))
})

// Or you can use formatting method like this:
logit.Debugf("This is a debug msg with %d params: %s, %s", 2, "msgFormat", "msgParams")
logit.Infof("This is a debug msg with %d params: %s, %s", 2, "msgFormat", "msgParams")
logit.Warnf("This is a debug msg with %d params: %s, %s", 2, "msgFormat", "msgParams")
logit.Errorf("This is a debug msg with %d params: %s, %s", 2, "msgFormat", "msgParams")

// If a config file "logit.conf" in "./", then logit will load it automatically.
// This is more convenience to use config file and logger.
// There are four levels can be logged
logit.Debug("Hello, I am debug!") // Ignore because default level is info
logit.Info("Hello, I am info!")
logit.Warn("Hello, I am warn!")
logit.Error("Hello, I am error!")

// You can format log with some parameters if you want
logit.DebugF("Hello, I am debugF %d!", 2) // Ignore because default level is info
logit.InfoF("Hello, I am infoF %d!", 2)
logit.WarnF("Hello, I am warnF %d!", 2)
logit.ErrorF("Hello, I am errorF %d!", 2)

// logit.Me() returns a completed logger for use

// Set level to debug
logit.Me().SetLevel(logit.DebugLevel)

// Log won't carry caller information in default
// So, try NeedCaller if you need
logit.Me().NeedCaller(true)

// Set format of time in log
logit.Me().TimeFormat("2006/01/02 15:04:05")

// Set encoder and writer
// Actually, every level has own encoder and writer
// This way will set encoder and writer of all levels to the same one
logit.Me().SetEncoder(logit.JsonEncoder())
logit.Me().SetWriter(os.Stdout)

// We also provide some functions to set encoder and writer of each level
logit.Me().SetDebugEncoder(logit.JsonEncoder())
logit.Me().SetInfoEncoder(logit.JsonEncoder())
logit.Me().SetWarnEncoder(logit.JsonEncoder())
logit.Me().SetErrorEncoder(logit.JsonEncoder())
logit.Me().SetDebugWriter(os.Stdout)
logit.Me().SetInfoWriter(os.Stdout)
logit.Me().SetWarnWriter(os.Stdout)
logit.Me().SetErrorWriter(os.Stdout)
}
```

### 📖 Examples

* [basic](./_examples/basic.go)
* [logger](./_examples/logger.go)
* [level_and_disable](./_examples/level_and_disable.go)
* [config_file](./_examples/config_file.go)
* [handler](./_examples/handler.go)
* [files](./_examples/files.go)
* [log_to_file](./_examples/log_to_file.go)
* [encoder](./_examples/encoder.go)
* [writer](./_examples/writer.go)

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

_Learn more about config file in [_examples/config](./_examples/config)._

### 🔥 Benchmarks

```bash
Expand All @@ -117,12 +104,12 @@ $ go test -v ./_examples/benchmarks_test.go -bench=. -benchtime=10s
| test case | times ran (large is better) | ns/op (small is better) | B/op | allocs/op |
| -----------|--------|-------------|-------------|-------------|
| **logit** | **6429907** | **1855 ns/op** | **384 B/op** | **8 allocs/op** |
| golog | 3361483 | 3589 ns/op | 712 B/op | 24 allocs/op |
| zap | 2971119 | 4066 ns/op | 448 B/op | 16 allocs/op |
| logrus | 1553419 | 7869 ns/op | 1633 B/op | 52 allocs/op |
| **logit** | **7513623** | **1612 ns/op** | **384 B/op** | **8 allocs/op** |
| golog | 4569554 | 2631 ns/op | 712 B/op | 24 allocs/op |
| zap | 3891336 | 3084 ns/op | 448 B/op | 16 allocs/op |
| logrus | 2089682 | 5769 ns/op | 1633 B/op | 52 allocs/op |

> Environment:I7-6700HQ CPU @ 2.6 GHZ, 16 GB RAM
> Environment:R7-4700U CPU @ 2.0 GHZ16 GB RAM
**Notice:**

Expand All @@ -139,13 +126,13 @@ $ go test -v ./_examples/benchmarks_test.go -bench=. -benchtime=10s
**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.**

**4. You should know that some APIs like Debugf can't reach high performance as the same as others because of reflection,**
**4. You should know that some APIs like DebugF can't reach high performance as the same as others because of reflection,**
**however, their performances are not as bad as we think:**

| test case | times ran (large is better) | ns/op (small is better) | B/op | allocs/op |
| -----------|--------|-------------|-------------|-------------|
| logit | 6429907 | 1855 ns/op | 384 B/op | 8 allocs/op |
| **logit-reflection** | **5288931** | **2334 ns/op** | **424 B/op** | **12 allocs/op** |
| logit | 7513623 | 1612 ns/op | 384 B/op | 8 allocs/op |
| **logit-reflection** | **6042254** | **1984 ns/op** | **424 B/op** | **12 allocs/op** |

### 👥 Contributing

Expand Down
Loading

0 comments on commit cd7601a

Please sign in to comment.