Skip to content

Commit

Permalink
pangpanglabs#61 log print for eland job monitor
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoxinmiao committed Jan 19, 2019
1 parent cb832a7 commit 3cae522
Show file tree
Hide file tree
Showing 4 changed files with 205 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ Golang utility packages
- [goutils/echomiddleware](/echomiddleware)
- [goutils/echotpl](/echotpl)
- [goutils/jwtutil](/jwtutil)
- [goutils/cronjob](/cronjob)
- [goutils/cronjob](/cronjob)
- [goutils/joblog](/joblog)
21 changes: 21 additions & 0 deletions joblog/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# joblog

log print for eland job monitor

## Getting Started

```golang
jobLog := joblog.New(url,"test", map[string]interface{}{"log": "this is test"})
err := jobLog.Info("good")
test.Ok(t, err)
err = jobLog.Warning(struct{ Name string }{"xiaoxinmiao"})
test.Ok(t, err)
err = jobLog.Error(errors.New("this is bug."))
test.Ok(t, err)
```

## View log
https://wiki.elandsystems.cn/display/DBA/Batch-job+Monitor

## Note:
the **new** method's param parameter can only pass `objects`, `pointer objects` or `maps`.
134 changes: 134 additions & 0 deletions joblog/joblog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package joblog

import (
"encoding/json"
"errors"
"fmt"
"net/http"
"reflect"

"github.com/pangpanglabs/goutils/httpreq"
)

type JobLog struct {
url string
serviceName string
Disable bool
JobId int64
Err error
}

type jobStartDto struct {
Servcie string `json:"service"`
Param interface{} `json:"param"`
}

type messageLevel struct {
Level string `json:"level"`
Message string `json:"message"`
}

func New(url, serviceName string, param interface{}, options ...func(*JobLog)) (jobLog *JobLog) {
jobLog = &JobLog{
url: url,
serviceName: serviceName,
}
for _, option := range options {
if option == nil {
continue
}
option(jobLog)
}
if jobLog.Disable == true {
return
}
if len(jobLog.serviceName) == 0 {
jobLog.Err = errors.New("serviceName is missing.")
return
}
var result struct {
Result int64 `json:"result"`
}
if ok, kind := validParam(param); !ok {
jobLog.Err = fmt.Errorf("param type act:%v,exp:%v.",
kind, "Struct, Map, Ptr")
return
}
_, jobLog.Err = httpreq.New(http.MethodPost, jobLog.url, &jobStartDto{
Servcie: jobLog.serviceName,
Param: param,
}).Call(&result)
if jobLog.Err != nil {
return
}
jobLog.JobId = result.Result
return
}

func (r *JobLog) Info(message interface{}) error {
return r.write(message, "info")
}

func (r *JobLog) Warning(message interface{}) error {
return r.write(message, "warning")
}
func (r *JobLog) Error(message interface{}) error {
return r.write(message, "error")
}

func (r *JobLog) write(message interface{}, level string) (err error) {
if r.Err != nil {
err = r.Err
return
}
if r.Disable == true {
return
}
url := fmt.Sprintf("%v/%v/logs", r.url, r.JobId)
_, err = httpreq.New(http.MethodPost, url, &messageLevel{
Message: toString(message),
Level: level,
}).Call(nil)
return
}

func (r *JobLog) Finish(jobId int64) (err error) {
if r.Err != nil {
err = r.Err
return
}
if r.Disable == true {
return
}
url := fmt.Sprintf("%v/%v/finish", r.url, r.JobId)
httpreq.New(http.MethodPost, url, nil).Call(nil)
return
}

func toString(raw interface{}) string {
switch v := raw.(type) {
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, bool, float32, float64:
return fmt.Sprint(v)
case string:
return string(v)
case error:
return v.Error()
}
val := reflect.ValueOf(raw)
switch val.Kind() {
case reflect.Struct, reflect.Map, reflect.Array, reflect.Slice, reflect.Ptr:
b, _ := json.Marshal(raw)
return string(b)
}
return ""
}
func validParam(param interface{}) (ok bool, kind reflect.Kind) {
val := reflect.ValueOf(param)
kind = val.Kind()
switch kind {
case reflect.Struct, reflect.Map, reflect.Ptr:
ok = true
return
}
return
}
48 changes: 48 additions & 0 deletions joblog/joblog_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package joblog_test

import (
"errors"
"os"
"testing"

"github.com/ElandGroup/joblog"

"github.com/pangpanglabs/goutils/test"
)

var url = os.Getenv("JOB_LOG")

func TestLog(t *testing.T) {
//1.test normal logic
jobLog := joblog.New(url,
"test", map[string]interface{}{"log": "this is test"})
err := jobLog.Info("good")
test.Ok(t, err)

err = jobLog.Warning(struct{ Name string }{"xiaoxinmiao"})
test.Ok(t, err)

err = jobLog.Error(errors.New("this is bug."))
test.Ok(t, err)

//2.test Disable:this content will not be logged
jobLog = joblog.New(url,
"test", map[string]interface{}{"log": "this is test 2"}, func(log *joblog.JobLog) {
log.Disable = true
})
err = jobLog.Info("good 2")
test.Ok(t, err)

err = jobLog.Warning(struct{ Name string }{"xiaoxinmiao 2"})
test.Ok(t, err)

err = jobLog.Error(errors.New("this is bug. 2"))
test.Ok(t, err)

//3.test start param
type Dto struct{ Message string }
test.Ok(t, joblog.New(url, "test", Dto{Message: "how are you 1"}).Err)
test.Ok(t, joblog.New(url, "test", &Dto{Message: "how are you 2"}).Err)
test.Ok(t, joblog.New(url, "test", map[string]interface{}{"message": "how are you 3"}).Err)

}

0 comments on commit 3cae522

Please sign in to comment.