Skip to content

Commit

Permalink
增加按概率写入log
Browse files Browse the repository at this point in the history
  • Loading branch information
alex cai committed Nov 8, 2016
1 parent 7f894dc commit 8d10415
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ go get -u github.com/ibbd-dev/go-async-log
- 错误等级实现
- 可以写入json数据
- 时间格式采用`RFC3339`,格式如`2006-01-02T15:04:05Z07:00`
- 支持按概率写log

## 配置项

Expand All @@ -35,6 +36,8 @@ lf := asyncLog.NewLogFile("/tmp/test.log")
// 设置按天切割文件,如果默认则是按小时
lf.SetRotate(asyncLog.RotateDate)

lf.SetProbability(0.5) // 设置写log的概率,默认全部都写入

_ = lf.Write("lf: hello world")

// 注意:因为是每秒写入一次,所以这里需要暂停一下
Expand All @@ -46,6 +49,7 @@ time.Sleep(time.Second * 2)

```go
infoFile := asyncLog.NewLevelLog("/tmp/test-info.log", asyncLog.LevelInfo) // 只有Info级别或者以上级别的日志才会被记录
infoFile.SetProbability(0.5) // 设置写log的概率,默认全部都写入
infoFile.Debug("hello world") // 该日志不会写入文件
infoFile.Info("hello world")
infoFile.Error("hello world")
Expand Down
20 changes: 18 additions & 2 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package asyncLog
import (
"encoding/json"
"log"
"math/rand"
"os"
"strings"
"sync"
Expand All @@ -22,7 +23,7 @@ type LogFile struct {

// 同步设置
sync struct {
cycle time.Duration // 同步数据到文件的周期,默认为1秒
duration time.Duration // 同步数据到文件的周期,默认为1秒
beginTime time.Time // 开始同步的时间,判断同步的耗时
status syncStatus // 同步状态
}
Expand All @@ -46,6 +47,9 @@ type LogFile struct {
suffix string // 切割后的文件名后缀
mutex sync.Mutex // 文件名锁
}

// 日志写入概率
probability float32
}

// log同步的状态
Expand Down Expand Up @@ -135,8 +139,11 @@ func NewLogFile(filename string) *LogFile {
// 默认开启缓存
lf.cache.use = true

// 日志写入概率,默认为1.1, 就是全部写入
lf.probability = 1.1

// TODO 同步的时间周期,缓存开启才有效
lf.sync.cycle = time.Second
lf.sync.duration = time.Second
return lf
}

Expand All @@ -152,8 +159,17 @@ func (lf *LogFile) SetUseCache(useCache bool) {
lf.cache.use = useCache
}

func (lf *LogFile) SetProbability(probability float32) {
lf.probability = probability
}

// Write 写缓存
func (lf *LogFile) Write(msg string) error {
if lf.probability < 1.0 && rand.Float32() > lf.probability {
// 按照概率写入
return nil
}

if lf.flag == StdFlag {
msg = time.Now().Format(logTimeFormat) + " " + msg + newlineChar
} else {
Expand Down
10 changes: 10 additions & 0 deletions log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ func TestNewLogFile(t *testing.T) {
time.Sleep(time.Second * 2)
}

func TestProbability(t *testing.T) {
lf := NewLogFile("/tmp/test-probability.log")
lf.SetProbability(0.5)
for i := 0; i < 20; i++ {
lf.Write("probability")
}

time.Sleep(time.Second)
}

func BenchmarkWrite(b *testing.B) {
lf := NewLogFile("/tmp/bench-test.log")
b.RunParallel(func(pb *testing.PB) {
Expand Down

0 comments on commit 8d10415

Please sign in to comment.