Skip to content

Commit

Permalink
Added custom logger structure. Fix bamzi#26
Browse files Browse the repository at this point in the history
  • Loading branch information
akdilsiz committed Dec 11, 2019
1 parent d0b7b07 commit 7b00cdf
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 2 deletions.
61 changes: 61 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,67 @@ func JobHtml(c *gin.Context) {
}

```
## Custom Logger
If you use your own or customized logger package, you can integrate your logger package by following the steps below.

```go
package main

import (
"github.com/robfig/cron/v3"
"github.com/rs/zerolog"
"log"
)

type CustomLogger struct {
cron.Logger
Log *zerolog.Logger
}

func (l CustomLogger) Info(format string, keysAndValues ...interface{}) {
l.Log.Info().Msg(fmt.Sprintf(format, keysAndValues...))
}

func (l CustomLogger) Error(err error, format string, keysAndValues ...interface{}) {
l.Log.Err(err).Msg(fmt.Sprintf(format, keysAndValues...))
}

func main() {
zeroLogger := zerolog.New(os.Stderr).With().Timestamp().Logger()
log.SetOutput(zeroLogger)

logger := CustomLogger{Log: &zeroLogger}

StartWithLogger(logger)

Schedule("@every 1s", CustomJob{})

ch := make(chan bool)

time.AfterFunc(5 * time.Second, func() {
ch<-true
})

<-ch
}
```
Log Output
```
{"level":"info","time":"2019-12-11T19:19:11+03:00","message":"JobRunner Started"}
{"level":"info","time":"2019-12-11T19:19:11+03:00","message":"start"}
{"level":"info","time":"2019-12-11T19:19:11+03:00","message":"added%!(EXTRA string=now, time.Time=2019-12-11 19:19:11.539557059 +0300 +03, string=entry, cron.EntryID=1, string=next, time.Time=2019-12-11 19:19:12 +0300 +03)"}
{"level":"info","time":"2019-12-11T19:19:12+03:00","message":"wake%!(EXTRA string=now, time.Time=2019-12-11 19:19:12.000175121 +0300 +03)"}
{"level":"info","time":"2019-12-11T19:19:12+03:00","message":"run%!(EXTRA string=now, time.Time=2019-12-11 19:19:12.000175121 +0300 +03, string=entry, cron.EntryID=1, string=next, time.Time=2019-12-11 19:19:13 +0300 +03)"}
{"level":"info","time":"2019-12-11T19:19:13+03:00","message":"wake%!(EXTRA string=now, time.Time=2019-12-11 19:19:13.000324711 +0300 +03)"}
{"level":"info","time":"2019-12-11T19:19:13+03:00","message":"run%!(EXTRA string=now, time.Time=2019-12-11 19:19:13.000324711 +0300 +03, string=entry, cron.EntryID=1, string=next, time.Time=2019-12-11 19:19:14 +0300 +03)"}
{"level":"info","time":"2019-12-11T19:19:14+03:00","message":"wake%!(EXTRA string=now, time.Time=2019-12-11 19:19:14.000597951 +0300 +03)"}
{"level":"info","time":"2019-12-11T19:19:14+03:00","message":"run%!(EXTRA string=now, time.Time=2019-12-11 19:19:14.000597951 +0300 +03, string=entry, cron.EntryID=1, string=next, time.Time=2019-12-11 19:19:15 +0300 +03)"}
{"level":"info","time":"2019-12-11T19:19:15+03:00","message":"wake%!(EXTRA string=now, time.Time=2019-12-11 19:19:15.0004445 +0300 +03)"}
{"level":"info","time":"2019-12-11T19:19:15+03:00","message":"run%!(EXTRA string=now, time.Time=2019-12-11 19:19:15.0004445 +0300 +03, string=entry, cron.EntryID=1, string=next, time.Time=2019-12-11 19:19:16 +0300 +03)"}
{"level":"info","time":"2019-12-11T19:19:16+03:00","message":"wake%!(EXTRA string=now, time.Time=2019-12-11 19:19:16.000527496 +0300 +03)"}
{"level":"info","time":"2019-12-11T19:19:16+03:00","message":"run%!(EXTRA string=now, time.Time=2019-12-11 19:19:16.000527496 +0300 +03, string=entry, cron.EntryID=1, string=next, time.Time=2019-12-11 19:19:17 +0300 +03)"}
```

## WHY?
To reduce our http response latency by 200+%

Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/bamzi/jobrunner

go 1.13

require github.com/robfig/cron/v3 v3.0.0
require (
github.com/robfig/cron/v3 v3.0.0
github.com/rs/zerolog v1.17.2
)
13 changes: 13 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,15 @@
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/robfig/cron/v3 v3.0.0 h1:kQ6Cb7aHOHTSzNVNEhmp8EcWKLb4CbiMW9h9VyIhO4E=
github.com/robfig/cron/v3 v3.0.0/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ=
github.com/rs/zerolog v1.17.2 h1:RMRHFw2+wF7LO0QqtELQwo8hqSmqISyCJeFeAAuWcRo=
github.com/rs/zerolog v1.17.2/go.mod h1:9nvC1axdVrAHcu/s9taAVfBuIdTZLVQmKQyvrUjF5+I=
github.com/zenazn/goji v0.9.0/go.mod h1:7S9M489iMyHBNxwZnk9/EHS098H4/F6TATF2mIxtB1Q=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190828213141-aed303cbaa74/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
14 changes: 13 additions & 1 deletion init.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ func isSelfConcurrent(cocnurrencyFlag int) {
}
}

func StartWithLogger(logger cron.Logger, v ...int) {

MainCron = cron.New(cron.WithLogger(logger))

for i,option := range v {
functions[i].(func(int))(option)
}

MainCron.Start()

logger.Info("JobRunner Started")
}

func Start(v ...int) {
MainCron = cron.New()

Expand All @@ -56,5 +69,4 @@ func Start(v ...int) {

fmt.Printf("%s[JobRunner] %v Started... %s \n",
magenta, time.Now().Format("2006/01/02 - 15:04:05"), reset)

}
46 changes: 46 additions & 0 deletions init_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package jobrunner

import (
"fmt"
"github.com/robfig/cron/v3"
"github.com/rs/zerolog"
"os"
"testing"
"time"
)

type CustomJob struct {}

func (j CustomJob) Run() {
fmt.Println("Custom job run")
}

type CustomLogger struct {
cron.Logger
Log *zerolog.Logger
}

func (l CustomLogger) Info(format string, keysAndValues ...interface{}) {
l.Log.Info().Msgf(format, keysAndValues...)
}

func (l CustomLogger) Error(err error, format string, keysAndValues ...interface{}) {
l.Log.Err(err).Msgf(format, keysAndValues...)
}

func Test_Init(t *testing.T) {
zeroLogger := zerolog.New(os.Stderr).With().Timestamp().Logger()

logger := CustomLogger{Log: &zeroLogger}

StartWithLogger(logger)
Schedule("@every 1s", CustomJob{})

ch := make(chan bool)

time.AfterFunc(5 * time.Second, func() {
ch<-true
})

<-ch
}

0 comments on commit 7b00cdf

Please sign in to comment.