Skip to content

Commit

Permalink
updated to latest disgohook, small cleanup & added builder example
Browse files Browse the repository at this point in the history
  • Loading branch information
topi314 committed May 25, 2021
1 parent a66ed35 commit 965a126
Show file tree
Hide file tree
Showing 13 changed files with 191 additions and 72 deletions.
26 changes: 16 additions & 10 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
version: 2
updates:
- package-ecosystem: gomod
directory: "/"
schedule:
interval: daily
open-pull-requests-limit: 10
- package-ecosystem: gomod
directory: "/example"
schedule:
interval: daily
open-pull-requests-limit: 10
- package-ecosystem: gomod
directory: "/"
schedule:
interval: daily
open-pull-requests-limit: 10
- package-ecosystem: gomod
directory: "/examples/factory_example"
schedule:
interval: daily
open-pull-requests-limit: 10
- package-ecosystem: gomod
directory: "/examples/builder_example"
schedule:
interval: daily
open-pull-requests-limit: 10

5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

dislog is a [logrus](https://github.com/sirupsen/logrus) [logging hook](https://github.com/sirupsen/logrus#hooks) sending logs over [Discord Webhooks](https://discord.com/developers/docs/resources/webhook) using the [disgohook](https://github.com/DisgoOrg/dislog) library


## Getting Started

### Installing
Expand Down Expand Up @@ -39,6 +38,8 @@ defer dlog.Close()
logger.AddHook(dlog)
```

Builder example can bve found [here](https://github.com/DisgoOrg/dislog/tree/master/examples/builder_example/builder_example.go)

## Documentation

Documentation is unfinished and can be found under
Expand All @@ -48,7 +49,7 @@ Documentation is unfinished and can be found under

## Examples

You can find and example under [example](https://github.com/DisgoOrg/dislog/tree/master/example)
You can find examples [here](https://github.com/DisgoOrg/dislog/tree/master/examples)

## Troubleshooting

Expand Down
46 changes: 26 additions & 20 deletions dislog.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/DisgoOrg/disgohook"
"github.com/DisgoOrg/disgohook/api"
"github.com/DisgoOrg/log"
"github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -47,41 +48,46 @@ var (
_ logrus.Hook = (*DisLog)(nil)
)

func NewDisLogByToken(client *http.Client, webhookLogLevel logrus.Level, webhookToken string, levels ...logrus.Level) (*DisLog, error) {
func newDisLog(webhookLogLevel logrus.Level, webhookClientCreate func(logger log.Logger) (api.WebhookClient, error), levels ...logrus.Level) (*DisLog, error) {
logger := logrus.New()
logger.SetLevel(webhookLogLevel)
webhook, err := disgohook.NewWebhookByToken(client, logger, webhookToken)
webhook, err := webhookClientCreate(logger)
if err != nil {
return nil, err
}
return NewDisLog(webhook, levels...)
}

func NewDisLogByIDToken(client *http.Client, webhookLogLevel logrus.Level, webhookID string, webhookToken string, levels ...logrus.Level) (*DisLog, error) {
logger := logrus.New()
logger.SetLevel(webhookLogLevel)
webhook, err := disgohook.NewWebhookByIDToken(client, logger, webhookID, webhookToken)
if err != nil {
return nil, err
}
return NewDisLog(webhook, levels...)
func NewDisLogByToken(httpClient *http.Client, webhookLogLevel logrus.Level, webhookToken string, levels ...logrus.Level) (*DisLog, error) {
return newDisLog(webhookLogLevel, func(logger log.Logger) (api.WebhookClient, error) {
return disgohook.NewWebhookClientByToken(httpClient, logger, webhookToken)
}, levels...)
}

func NewDisLogByIDSnowflakeToken(httpClient *http.Client, webhookLogLevel logrus.Level, webhookID api.Snowflake, webhookToken string, levels ...logrus.Level) (*DisLog, error) {
return newDisLog(webhookLogLevel, func(logger log.Logger) (api.WebhookClient, error) {
return disgohook.NewWebhookClientByIDToken(httpClient, logger, webhookID, webhookToken)
}, levels...)
}

func NewDisLog(webhook api.Webhook, levels ...logrus.Level) (*DisLog, error) {
func NewDisLogByIDToken(httpClient *http.Client, webhookLogLevel logrus.Level, webhookID string, webhookToken string, levels ...logrus.Level) (*DisLog, error) {
return NewDisLogByIDSnowflakeToken(httpClient, webhookLogLevel, api.Snowflake(webhookID), webhookToken, levels...)
}

func NewDisLog(webhook api.WebhookClient, levels ...logrus.Level) (*DisLog, error) {
return &DisLog{
webhook: webhook,
queued: false,
levels: levels,
webhookClient: webhook,
queued: false,
levels: levels,
}, nil
}

type DisLog struct {
webhook api.Webhook
lock sync.Mutex
queued bool
logQueue []*api.Embed
levels []logrus.Level
webhookClient api.WebhookClient
lock sync.Mutex
queued bool
logQueue []*api.Embed
levels []logrus.Level
}

func (l *DisLog) Levels() []logrus.Level {
Expand Down Expand Up @@ -124,7 +130,7 @@ func (l *DisLog) sendEmbeds() {
return
}

_, err := l.webhook.SendMessage(message.Build())
_, err := l.webhookClient.SendMessage(message.Build())
if err != nil {
fmt.Printf("error while sending logs: %s\n", err)
}
Expand Down
71 changes: 47 additions & 24 deletions dislog_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,58 +11,70 @@ import (
"github.com/sirupsen/logrus"
)

var ErrNoWebhookSet = errors.New("no webhook id token set")
var ErrNoWebhookSet = errors.New("no webhookClient id token set")

func NewDisLogBuilder() *DisLogBuilder {
return &DisLogBuilder{}
func NewDisLogBuilder() *Builder {
return &Builder{}
}

type DisLogBuilder struct {
httpClient *http.Client
logger log.Logger
webhook api.Webhook
webhookID *string
webhookToken *string
webhookIDToken *string
levels []logrus.Level
type Builder struct {
httpClient *http.Client
logger log.Logger
webhookClient api.WebhookClient
webhookLogLevel *logrus.Level
webhookID *api.Snowflake
webhookToken *string
webhookIDToken *string
levels []logrus.Level
}

func (b *DisLogBuilder) SetHttpClient(httpClient *http.Client) *DisLogBuilder {
func (b *Builder) SetHttpClient(httpClient *http.Client) *Builder {
b.httpClient = httpClient
return b
}

func (b *DisLogBuilder) SetLogger(logger log.Logger) *DisLogBuilder {
func (b *Builder) SetLogger(logger log.Logger) *Builder {
b.logger = logger
return b
}

func (b *DisLogBuilder) SetWebhook(webhook api.Webhook) *DisLogBuilder {
b.webhook = webhook
func (b *Builder) SetWebhookClient(webhookClient api.WebhookClient) *Builder {
b.webhookClient = webhookClient
return b
}

func (b *DisLogBuilder) SetWebhookIDToken(webhookIDToken string) *DisLogBuilder {
func (b *Builder) SetWebhookLoglevel(webhookLogLevel logrus.Level) *Builder {
b.webhookLogLevel = &webhookLogLevel
return b
}

func (b *Builder) SetWebhookIDToken(webhookIDToken string) *Builder {
b.webhookIDToken = &webhookIDToken
return b
}

func (b *DisLogBuilder) SetSetWebhookID(webhookID string) *DisLogBuilder {
func (b *Builder) SetSetWebhookID(webhookID string) *Builder {
snowflake := api.Snowflake(webhookID)
b.webhookID = &snowflake
return b
}

func (b *Builder) SetSetWebhookIDSnowflake(webhookID api.Snowflake) *Builder {
b.webhookID = &webhookID
return b
}

func (b *DisLogBuilder) SetSetWebhookToken(webhookToken string) *DisLogBuilder {
func (b *Builder) SetSetWebhookToken(webhookToken string) *Builder {
b.webhookToken = &webhookToken
return b
}

func (b *DisLogBuilder) SetLevels(levels ...logrus.Level) *DisLogBuilder {
func (b *Builder) SetLevels(levels ...logrus.Level) *Builder {
b.levels = levels
return b
}

func (b *DisLogBuilder) Build() (*DisLog, error) {
func (b *Builder) Build() (*DisLog, error) {
dlog := &DisLog{}

if b.levels == nil {
Expand All @@ -79,20 +91,31 @@ func (b *DisLogBuilder) Build() (*DisLog, error) {
if len(webhookTokenSplit) != 2 {
return nil, api.ErrMalformedWebhookToken
}
b.webhookID = &webhookTokenSplit[0]
snowflake := api.Snowflake(webhookTokenSplit[0])
b.webhookID = &snowflake
b.webhookToken = &webhookTokenSplit[1]
}

if b.webhook == nil {
if b.logger == nil {
b.logger = logrus.New()
}

if b.webhookLogLevel == nil {
level := logrus.ErrorLevel
b.webhookLogLevel = &level
}

if b.webhookClient == nil {
if b.webhookID == nil || b.webhookToken == nil {
return nil, ErrNoWebhookSet
}
webhook, err := disgohook.NewWebhookByIDToken(b.httpClient, b.logger, *b.webhookID, *b.webhookToken)
var err error
b.webhookClient, err = disgohook.NewWebhookClientByIDToken(b.httpClient, b.logger, *b.webhookID, *b.webhookToken)
if err != nil {
return nil, err
}
b.webhook = webhook
}
dlog.webhookClient = b.webhookClient

return dlog, nil
}
10 changes: 0 additions & 10 deletions example/go.mod

This file was deleted.

54 changes: 54 additions & 0 deletions examples/builder_example/builder_example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package main

import (
"net/http"
"os"
"time"

"github.com/DisgoOrg/dislog"
"github.com/sirupsen/logrus"
)

var webhookToken = os.Getenv("webhook_token")

var logger = logrus.New()
var httpClient = http.DefaultClient

func main() {
// override default trace color
dislog.TraceLevelColor = 0xd400ff

// override default max delay
dislog.LogWait = 1 * time.Second

// override default time format
dislog.TimeFormatter = "2006-01-02 15:04:05 Z07"

logger.SetLevel(logrus.TraceLevel)
logger.Info("starting examples...")
dlog, err := dislog.NewDisLogBuilder().
// Sets which logrus.LogLevel the webhook client should log at if none is passed
SetWebhookLoglevel(logrus.ErrorLevel).
SetWebhookIDToken(webhookToken).
// Sets a custom http client or nil for inbuilt
SetHttpClient(httpClient).
// Sets which logging levels to send to the webhook
SetLevels(dislog.TraceLevelAndAbove...).
Build()
if err != nil {
logger.Errorf("error initializing dislog %s", err)
return
}
defer dlog.Close()
logger.AddHook(dlog)

logger.Trace("trace log")
logger.Debug("debug log")
logger.Info("info log")
logger.Warn("warn log")
logger.Error("error log")
// Calls os.Exit(1) after logging
logger.Fatal("fatal log")
// Calls panic() after logging
logger.Panic("panic log")
}
10 changes: 10 additions & 0 deletions examples/builder_example/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/DisgoOrg/dislog/examples/builder_example

go 1.16

replace github.com/DisgoOrg/dislog => ../../

require (
github.com/DisgoOrg/dislog v1.0.0
github.com/sirupsen/logrus v1.8.1
)
2 changes: 2 additions & 0 deletions example/go.sum → examples/builder_example/go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/DisgoOrg/disgohook v1.0.4 h1:qNUzkmXQBQ/ErwdYhHEEzvw9/rUcO3NBXBymm12ESvU=
github.com/DisgoOrg/disgohook v1.0.4/go.mod h1:Psu7Ny9KlE81U9C5mcM2+vJkwlGCPfhPY+KorXbkKRo=
github.com/DisgoOrg/disgohook v1.1.1 h1:tZi7FSmIvH/GjnkwLpgmvuX5fYmkOdqNAlfao6387mg=
github.com/DisgoOrg/disgohook v1.1.1/go.mod h1:FmGSn1CtzlqIzKOwX7xgOGntjToxCQIcF/K6kEuZaVY=
github.com/DisgoOrg/log v1.0.3 h1:IjmZQQu/kuBIui22EdXmxzQGYwcPCJEkXa0Fe6W9fJk=
github.com/DisgoOrg/log v1.0.3/go.mod h1:KFGKhBQr37d6rxZ7p2bmc8BEmDH8DZbtgdlJDSCsE7I=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/sirupsen/logrus"
)

var webhookToken = os.Getenv("webhook_token")

var logger = logrus.New()

func main() {
Expand All @@ -21,9 +23,8 @@ func main() {
dislog.TimeFormatter = "2006-01-02 15:04:05 Z07"

logger.SetLevel(logrus.TraceLevel)
logger.Info("starting example...")
println(os.Getenv("webhook_token"))
dlog, err := dislog.NewDisLogByToken(nil, logrus.InfoLevel, os.Getenv("webhook_token"), dislog.TraceLevelAndAbove...)
logger.Info("starting examples...")
dlog, err := dislog.NewDisLogByToken(nil, logrus.InfoLevel, webhookToken, dislog.TraceLevelAndAbove...)
if err != nil {
logger.Errorf("error initializing dislog %s", err)
return
Expand Down
10 changes: 10 additions & 0 deletions examples/factory_example/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/DisgoOrg/dislog/examples/factory_example

go 1.16

replace github.com/DisgoOrg/dislog => ../../

require (
github.com/DisgoOrg/dislog v1.0.0
github.com/sirupsen/logrus v1.8.1
)
16 changes: 16 additions & 0 deletions examples/factory_example/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
github.com/DisgoOrg/disgohook v1.0.4 h1:qNUzkmXQBQ/ErwdYhHEEzvw9/rUcO3NBXBymm12ESvU=
github.com/DisgoOrg/disgohook v1.0.4/go.mod h1:Psu7Ny9KlE81U9C5mcM2+vJkwlGCPfhPY+KorXbkKRo=
github.com/DisgoOrg/disgohook v1.1.1 h1:tZi7FSmIvH/GjnkwLpgmvuX5fYmkOdqNAlfao6387mg=
github.com/DisgoOrg/disgohook v1.1.1/go.mod h1:FmGSn1CtzlqIzKOwX7xgOGntjToxCQIcF/K6kEuZaVY=
github.com/DisgoOrg/log v1.0.3 h1:IjmZQQu/kuBIui22EdXmxzQGYwcPCJEkXa0Fe6W9fJk=
github.com/DisgoOrg/log v1.0.3/go.mod h1:KFGKhBQr37d6rxZ7p2bmc8BEmDH8DZbtgdlJDSCsE7I=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Loading

0 comments on commit 965a126

Please sign in to comment.