Skip to content

Commit

Permalink
Merge pull request #23 from qiniu/develop
Browse files Browse the repository at this point in the history
dev to master
  • Loading branch information
wonderflow authored Jun 6, 2017
2 parents 2404c3d + f07c94f commit 11145bc
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 37 deletions.
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,22 @@ logkit本身支持多种数据源,并且可以同时发送多个数据源的

1. 下载&解压logkit工具

**Linux 版本**

```
wget http://op26gaeek.bkt.clouddn.com/logkit.tar.gz && tar xvf logkit.tar.gz && cd _package_linux64/
```

**MacOS 版本**

```
wget http://op26gaeek.bkt.clouddn.com/logkit.tar.gz && tar xvf logkit.tar.gz
wget http://op26gaeek.bkt.clouddn.com/logkit_mac.tar.gz && tar xvf logkit_mac.tar.gz && cd _package_mac/
```

2. 修改runner的配置

```
打开 _package/confs/default.conf
打开 confs/default.conf
```

按照图示进行修改
Expand All @@ -44,7 +52,7 @@ wget http://op26gaeek.bkt.clouddn.com/logkit.tar.gz && tar xvf logkit.tar.gz
3. 启动logkit工具

```
cd _package && ./logkit -f logkit.conf
./logkit -f logkit.conf
```

logkit.conf是logkit工具本身的配置文件,主要用于指定logkit运行时需要的资源和各个runner配置文件的具体路径。
Expand Down
2 changes: 2 additions & 0 deletions logkit.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
"max_procs": 8,
"debug_level": 1,
"clean_self_log":true,
"bind_host":"localhost:3000",
"timeformat_layouts":["2006|01|02|-0700"],
"confs_path": ["confs*"]
}
11 changes: 9 additions & 2 deletions logkit.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

config "github.com/qiniu/logkit/conf"
"github.com/qiniu/logkit/mgr"
"github.com/qiniu/logkit/times"
"github.com/qiniu/logkit/utils"

_ "net/http/pprof"
Expand All @@ -25,6 +26,7 @@ type Config struct {
CleanSelfLog bool `json:"clean_self_log"`
CleanSelfDir string `json:"clean_self_dir"`
CleanSelfPattern string `json:"clean_self_pattern"`
TimeLayouts []string `json:"timeformat_layouts"`
CleanSelfLogCnt int `json:"clean_self_cnt"`
mgr.ManagerConfig
}
Expand Down Expand Up @@ -103,7 +105,9 @@ func main() {
log.Fatal("config.Load failed:", err)
}
log.Printf("Config: %#v", conf)

if conf.TimeLayouts != nil {
times.AddLayout(conf.TimeLayouts)
}
if conf.MaxProcs == 0 {
conf.MaxProcs = runtime.NumCPU()
}
Expand All @@ -122,6 +126,7 @@ func main() {
log.Fatalf("watch path error %v", err)
}
stopClean := make(chan struct{}, 0)
defer close(stopClean)
if conf.CleanSelfLog {
go loopCleanLogkitLog(conf.CleanSelfDir, conf.CleanSelfPattern, conf.CleanSelfLogCnt, stopClean)
}
Expand All @@ -133,7 +138,9 @@ func main() {

utils.WaitForInterrupt(func() {
rs.Stop()
stopClean <- struct{}{}
if conf.CleanSelfLog {
stopClean <- struct{}{}
}
m.Stop()
})
}
70 changes: 38 additions & 32 deletions times/times.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,44 @@ import (
"time"
)

var layouts = []string{
"02/Jan/2006:15:04:05 -0700",
"2006/01/02 15:04:05",
"2006-01-02 15:04:05 -0700 MST",
"2006-01-02 15:04:05 -0700",
"2006-01-02 15:04:05",
"2006/01/02 15:04:05 -0700 MST",
"2006/01/02 15:04:05 -0700",
"2006-01-02 -0700 MST",
"2006-01-02 -0700",
"2006-01-02",
"2006/01/02 -0700 MST",
"2006/01/02 -0700",
"2006/01/02",
"02/01/2006--15:04:05",
"02 Jan 06 15:04",
time.ANSIC,
time.UnixDate,
time.RubyDate,
time.RFC822,
time.RFC822Z,
time.RFC850,
time.RFC1123,
time.RFC1123Z,
time.RFC3339,
time.RFC3339Nano,
time.Kitchen,
time.Stamp,
time.StampMilli,
time.StampMicro,
time.StampNano,
}

// AddLayout 可以增加用户自定义的时间类型
func AddLayout(udfLayouts []string) {
layouts = append(udfLayouts, layouts...)
}

// Format 跟 PHP 中 date 类似的使用方式,如果 ts 没传递,则使用当前时间
func Format(format string, ts ...time.Time) string {
patterns := []string{
Expand Down Expand Up @@ -65,38 +103,6 @@ func StrToTime(value string) (time.Time, error) {
if value == "" {
return time.Now(), errors.New("empty time string")
}
layouts := []string{
"02/Jan/2006:15:04:05 -0700",
"2006/01/02 15:04:05",
"2006-01-02 15:04:05 -0700 MST",
"2006-01-02 15:04:05 -0700",
"2006-01-02 15:04:05",
"2006/01/02 15:04:05 -0700 MST",
"2006/01/02 15:04:05 -0700",
"2006-01-02 -0700 MST",
"2006-01-02 -0700",
"2006-01-02",
"2006/01/02 -0700 MST",
"2006/01/02 -0700",
"2006/01/02",
"02/01/2006--15:04:05",
"02 Jan 06 15:04",
time.ANSIC,
time.UnixDate,
time.RubyDate,
time.RFC822,
time.RFC822Z,
time.RFC850,
time.RFC1123,
time.RFC1123Z,
time.RFC3339,
time.RFC3339Nano,
time.Kitchen,
time.Stamp,
time.StampMilli,
time.StampMicro,
time.StampNano,
}

var t time.Time
var err error
Expand Down
21 changes: 21 additions & 0 deletions times/times_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package times

import (
"errors"
"fmt"
"testing"
"time"
Expand Down Expand Up @@ -92,4 +93,24 @@ func TestStrToTime2(t *testing.T) {
fmt.Println(tm.String())
nt := tm.Format(time.RFC3339)
fmt.Println(nt)
testcase = "2017-06-05T21:00:18+08:00"
tm, err = StrToTime(testcase)
if err != nil {
t.Error(err)
}
fmt.Println(tm.String())
fmt.Println(time.Now().Format(time.RFC3339))
}

func TestAddLayout(t *testing.T) {
tm, err := StrToTime("[02/Jan/2017:15:04:05 -0700]")
if err == nil {
t.Error(errors.New("should have error without layouts"))
}
AddLayout([]string{"[02/Jan/2006:15:04:05 -0700]"})
tm, err = StrToTime("[02/Jan/2017:15:04:05 -0700]")
if err != nil {
t.Error(err)
}
fmt.Println(tm.String())
}

0 comments on commit 11145bc

Please sign in to comment.