From 965a126a9a97a8ab90508fa1d34a6384dc197217 Mon Sep 17 00:00:00 2001 From: TopiSenpai Date: Wed, 26 May 2021 00:40:15 +0200 Subject: [PATCH] updated to latest disgohook, small cleanup & added builder example --- .github/dependabot.yml | 26 ++++--- README.md | 5 +- dislog.go | 46 ++++++------ dislog_builder.go | 71 ++++++++++++------- example/go.mod | 10 --- examples/builder_example/builder_example.go | 54 ++++++++++++++ examples/builder_example/go.mod | 10 +++ {example => examples/builder_example}/go.sum | 2 + .../factory_example/factory_example.go | 7 +- examples/factory_example/go.mod | 10 +++ examples/factory_example/go.sum | 16 +++++ go.mod | 2 +- go.sum | 4 +- 13 files changed, 191 insertions(+), 72 deletions(-) delete mode 100644 example/go.mod create mode 100644 examples/builder_example/builder_example.go create mode 100644 examples/builder_example/go.mod rename {example => examples/builder_example}/go.sum (87%) rename example/example.go => examples/factory_example/factory_example.go (80%) create mode 100644 examples/factory_example/go.mod create mode 100644 examples/factory_example/go.sum diff --git a/.github/dependabot.yml b/.github/dependabot.yml index c2c321c..054232c 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -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 + diff --git a/README.md b/README.md index 0a0fdb1..8aa2359 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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 diff --git a/dislog.go b/dislog.go index 72a20bc..56aac3e 100644 --- a/dislog.go +++ b/dislog.go @@ -9,6 +9,7 @@ import ( "github.com/DisgoOrg/disgohook" "github.com/DisgoOrg/disgohook/api" + "github.com/DisgoOrg/log" "github.com/sirupsen/logrus" ) @@ -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 { @@ -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) } diff --git a/dislog_builder.go b/dislog_builder.go index a11abfa..b698a11 100644 --- a/dislog_builder.go +++ b/dislog_builder.go @@ -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 { @@ -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 } diff --git a/example/go.mod b/example/go.mod deleted file mode 100644 index bbe2f45..0000000 --- a/example/go.mod +++ /dev/null @@ -1,10 +0,0 @@ -module github.com/DisgoOrg/dislog/example - -go 1.16 - -replace github.com/DisgoOrg/dislog => ../ - -require ( - github.com/DisgoOrg/dislog v1.0.4 - github.com/sirupsen/logrus v1.8.1 -) diff --git a/examples/builder_example/builder_example.go b/examples/builder_example/builder_example.go new file mode 100644 index 0000000..72c4119 --- /dev/null +++ b/examples/builder_example/builder_example.go @@ -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") +} diff --git a/examples/builder_example/go.mod b/examples/builder_example/go.mod new file mode 100644 index 0000000..ee73f35 --- /dev/null +++ b/examples/builder_example/go.mod @@ -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 +) diff --git a/example/go.sum b/examples/builder_example/go.sum similarity index 87% rename from example/go.sum rename to examples/builder_example/go.sum index 9899b47..8defa69 100644 --- a/example/go.sum +++ b/examples/builder_example/go.sum @@ -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= diff --git a/example/example.go b/examples/factory_example/factory_example.go similarity index 80% rename from example/example.go rename to examples/factory_example/factory_example.go index 27f5a2a..cf47f72 100644 --- a/example/example.go +++ b/examples/factory_example/factory_example.go @@ -8,6 +8,8 @@ import ( "github.com/sirupsen/logrus" ) +var webhookToken = os.Getenv("webhook_token") + var logger = logrus.New() func main() { @@ -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 diff --git a/examples/factory_example/go.mod b/examples/factory_example/go.mod new file mode 100644 index 0000000..413ade8 --- /dev/null +++ b/examples/factory_example/go.mod @@ -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 +) diff --git a/examples/factory_example/go.sum b/examples/factory_example/go.sum new file mode 100644 index 0000000..8defa69 --- /dev/null +++ b/examples/factory_example/go.sum @@ -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= diff --git a/go.mod b/go.mod index 74cf9c3..5ba1526 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/DisgoOrg/dislog go 1.16 require ( - github.com/DisgoOrg/disgohook v1.0.4 + github.com/DisgoOrg/disgohook v1.1.1 github.com/DisgoOrg/log v1.0.3 github.com/sirupsen/logrus v1.8.1 ) diff --git a/go.sum b/go.sum index 9899b47..842a5c5 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -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=