-
Notifications
You must be signed in to change notification settings - Fork 0
feat: refactor to use slack web api #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nicoandrewss
wants to merge
2
commits into
main
Choose a base branch
from
v3
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,47 +1,49 @@ | ||
| # slacklogger | ||
|
|
||
| This client is used to send messages using a slack webhook url. | ||
| This lib can be used to easily send messages to slack channels via the Slack Web API. | ||
|
|
||
| ## Installation | ||
|
|
||
| ```shell | ||
| go get github.com/Clarilab/slacklogger/v2 | ||
| go get github.com/Clarilab/slacklogger/v3 | ||
| ``` | ||
|
|
||
| ## Importing | ||
|
|
||
| ```go | ||
| import "github.com/Clarilab/slacklogger/v2" | ||
| import "github.com/Clarilab/slacklogger/v3" | ||
| ``` | ||
|
|
||
| ## Examples | ||
|
|
||
| ### Logging with an instanced logger | ||
| ## Requirements | ||
| A [**Slack-App**](https://api.slack.com/docs/apps) installed to the desired **Slack-Workspace** with correct permissions/scopes set. | ||
|
|
||
| ```go | ||
| webhookURL := "https://hooks.slack.com/..." | ||
| environment := "development" | ||
| isDebug := false | ||
| Minimum required permission/scope is: **chat:write** | ||
|
|
||
| slacker := slacklogger.NewSlackLogger(webhookURL, environment, isDebug) | ||
| For more information checkout the [Slack Apps Quickstart Guide](https://api.slack.com/quickstart). | ||
|
|
||
| slacker.Log("Something weird") | ||
| ## Authorization | ||
| When using a proxy like [**Slack-Proxy**](https://github.com/fortio/slack-proxy), the authorization needs to be setup in the proxy and is not needed here. | ||
|
|
||
| // this will result in: env=development Something weird | ||
| ``` | ||
| Otherwise the [**Slack-Apps**](https://api.slack.com/docs/apps) **OAuth-Token** needs to be provided via the WithAuthorization() option. | ||
|
|
||
| ### Logging without an instanced logger | ||
| ## Examples | ||
|
|
||
| ```go | ||
| webhookURL := "https://hooks.slack.com/..." | ||
| environment := "" | ||
| isDebug := false | ||
| message := "Hello World!" | ||
| url := "https://<workspace-name>.slack.com/api/chat.postMessage" | ||
| token := "slack-token" | ||
| channel := "log-channel" | ||
| environment := "development" | ||
|
|
||
|
|
||
|
|
||
| slacklogger.LogWithURL(message, webhookURL, environment, isDebug) | ||
| slacker := slacklogger.NewSlackLogger(url, channel, slacklogger.WithEnvironment(environment), slacklogger.WithAuthorization(token)) | ||
|
|
||
| slacker.Log("Something weird") | ||
|
|
||
| // this will result in: Hello World! | ||
| // this will result in: | ||
| // environment: development | ||
| // | ||
| // Something weird | ||
| ``` | ||
|
|
||
| If isDebug is set to true, it will print to stdout instead. | ||
| If the UseDebug option is used, it will log to stdout instead using the log/slog logger. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package slacklogger | ||
|
|
||
| func newSlackError(status string) error { | ||
| return &SlackError{status: status} | ||
| } | ||
|
|
||
| // SlackError occurs when slack responded with an error status code. | ||
| type SlackError struct { | ||
| status string | ||
| } | ||
|
|
||
| // Error implements the error interface. | ||
| func (e *SlackError) Error() string { | ||
| return "error sending to slack. status: " + e.status | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| module github.com/Clarilab/slacklogger/v2 | ||
| module github.com/Clarilab/slacklogger/v3 | ||
|
|
||
| go 1.20 | ||
| go 1.21 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package slacklogger | ||
|
|
||
| // Option is an option func for creating a new SlackLogger. | ||
| type Option func(*SlackLogger) | ||
|
|
||
| // WithAuthorization sets the authorization token. | ||
| func WithAuthorization(token string) Option { | ||
| return func(sl *SlackLogger) { | ||
| sl.token = token | ||
| } | ||
| } | ||
|
|
||
| // UseDebug is an option for creating a new SlackLogger. | ||
| // When used the logger is NOT logging to slack, instead its logging to stdout using log/slog. | ||
| // Can be useful for tests. | ||
| func UseDebug() Option { | ||
| return func(sl *SlackLogger) { | ||
| sl.isDebug = true | ||
| } | ||
| } | ||
|
|
||
| // WithEnvironment is an option for creating a new SlackLogger. | ||
| // When used the given environment is added before the message. | ||
| func WithEnvironment(env string) Option { | ||
| return func(sl *SlackLogger) { | ||
| sl.environment = env | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,47 +1,87 @@ | ||
| package slacklogger | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "net/http" | ||
|
|
||
| "log/slog" | ||
| ) | ||
|
|
||
| // SlackLogger provides functionality to send messages to slack. | ||
| type SlackLogger struct { | ||
| webhookURL string | ||
| client *http.Client | ||
| url string | ||
| token string | ||
| channel string | ||
| environment string | ||
| isDebug bool | ||
| } | ||
|
|
||
| // NewSlackLogger returns a new instance of SlackLogger | ||
| func NewSlackLogger(webhookURL, environment string, isDebug bool) *SlackLogger { | ||
| return &SlackLogger{ | ||
| webhookURL: webhookURL, | ||
| environment: environment, | ||
| isDebug: isDebug, | ||
| // NewSlackLogger creates a new instance of SlackLogger. | ||
| func NewSlackLogger(url, channel string, options ...Option) *SlackLogger { | ||
| sl := SlackLogger{ | ||
| client: new(http.Client), | ||
| url: url, | ||
| channel: channel, | ||
| } | ||
|
|
||
| for i := range options { | ||
| options[i](&sl) | ||
| } | ||
|
|
||
| return &sl | ||
| } | ||
|
|
||
| func (logger *SlackLogger) Log(message string) { | ||
| LogWithURL(message, logger.webhookURL, logger.environment, logger.isDebug) | ||
| // Log sends a simple message to slack. | ||
| func (l *SlackLogger) Log(message string) { | ||
| l.log(context.Background(), message) | ||
| } | ||
|
|
||
| func (logger *SlackLogger) Write(message []byte) (int, error) { | ||
| logger.Log(string(message)) | ||
| // LogContext sends a simple message to slack with the given context.Context. | ||
| func (l *SlackLogger) LogContext(ctx context.Context, message string) { | ||
| l.log(ctx, message) | ||
| } | ||
|
|
||
| // Write implements the io.Writer interface. | ||
| func (l *SlackLogger) Write(message []byte) (int, error) { | ||
| l.Log(string(message)) | ||
|
|
||
| return len(message), nil | ||
| } | ||
|
|
||
| func LogWithURL(message, url, env string, isDebug bool) { | ||
| if env != "" { | ||
| message = fmt.Sprintf("env=%s, %s", env, message) | ||
| // Send sends the given payload to slack. | ||
| func (l *SlackLogger) Send(ctx context.Context, payload *Payload) { | ||
| const errMessage = "error while logging to slack" | ||
|
|
||
| if err := l.send(ctx, payload); err != nil { | ||
| slog.Error(errMessage, ErrorAttr(err)) | ||
| } | ||
| } | ||
|
|
||
| func (l *SlackLogger) log(ctx context.Context, message string) { | ||
| const ( | ||
| errMessage = "error while logging to slack" | ||
| envPrefixFormat = "environment: %s\n\n, %s" | ||
| debugMessage = "pretending to log to slack" | ||
| ) | ||
|
|
||
| if l.environment != "" { | ||
| message = fmt.Sprintf(envPrefixFormat, l.environment, message) | ||
| } | ||
|
|
||
| if isDebug { | ||
| fmt.Println("Debug: Logging to Slack: " + message) | ||
| if l.isDebug { | ||
| slog.Debug(debugMessage, MessageAttr(message)) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| err := Send(url, Payload{Text: message}) | ||
| if err != nil { | ||
| fmt.Printf("Error while logging to Slack: %s\nOriginal message was: %s\n", err, message) | ||
| payload := Payload{ | ||
| Channel: l.channel, | ||
| Text: message, | ||
| } | ||
|
|
||
| if err := l.send(ctx, &payload); err != nil { | ||
| slog.Error(errMessage, ErrorAttr(err)) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will man das hier vielleicht einfach zu einem "WithPrefix" oder so machen? 😄
Dann kann man auch evtl multiple prefixes machen