Skip to content

Commit

Permalink
Merge pull request #32 from seipan/feat/option-method
Browse files Browse the repository at this point in the history
feat(option) : Add Option struct
  • Loading branch information
seipan committed Dec 16, 2023
2 parents 82045cc + 2225e86 commit 8440a71
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 23 deletions.
46 changes: 23 additions & 23 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type Logger struct {
level Level
mutex sync.Mutex

Types string
Types Option

Slack *Slack

Expand All @@ -61,7 +61,7 @@ func NewLogger(img string, name string, types string, webhook string) *Logger {
level: InfoLevel,
Img: img,
Name: name,
Types: types,
Types: *NewOption(types),
Slack: NewSlack(webhook),
Discord: NewDiscord(webhook),
}
Expand All @@ -72,7 +72,7 @@ func (l *Logger) check(ctx context.Context, level Level) bool {
}

func (l *Logger) checkTypes() bool {
return (l.Types == "slack" || l.Types == "discord")
return (l.Types.Types() == "slack" || l.Types.Types() == "discord")
}

func (l *Logger) SetLevel(level Level) {
Expand Down Expand Up @@ -117,55 +117,55 @@ func (l *Logger) setNosend() {

// Sets the specified url in the webhook for each level
func (l *Logger) SetWebhook(webhook string) {
if l.Types == "slack" {
if l.Types.Types() == "slack" {
l.Slack.SetWebhook(webhook)
} else {
l.Discord.SetWebhook(webhook)
}
}

func (l *Logger) SetDebugWebhook(webhook string) {
if l.Types == "slack" {
if l.Types.Types() == "slack" {
l.Slack.SetDebugWebhook(webhook)
} else {
l.Discord.SetDebugWebhook(webhook)
}
}

func (l *Logger) SetInfoWebhook(webhook string) {
if l.Types == "slack" {
if l.Types.Types() == "slack" {
l.Slack.SetInfoWebhook(webhook)
} else {
l.Discord.SetInfoWebhook(webhook)
}
}

func (l *Logger) SetWarnWebhook(webhook string) {
if l.Types == "slack" {
if l.Types.Types() == "slack" {
l.Slack.SetWarnWebhook(webhook)
} else {
l.Discord.SetWarnWebhook(webhook)
}
}

func (l *Logger) SetErrorWebhook(webhook string) {
if l.Types == "slack" {
if l.Types.Types() == "slack" {
l.Slack.SetErrorWebhook(webhook)
} else {
l.Discord.SetErrorWebhook(webhook)
}
}

func (l *Logger) SetPanicWebhook(webhook string) {
if l.Types == "slack" {
if l.Types.Types() == "slack" {
l.Slack.SetPanicWebhook(webhook)
} else {
l.Discord.SetPanicWebhook(webhook)
}
}

func (l *Logger) SetFatalWebhook(webhook string) {
if l.Types == "slack" {
if l.Types.Types() == "slack" {
l.Slack.SetFatalWebhook(webhook)
} else {
l.Discord.SetFatalWebhook(webhook)
Expand All @@ -177,7 +177,7 @@ func (l *Logger) Level() Level {
}

func (l *Logger) resWebhookURLbyLevel(level Level) string {
if l.Types == "slack" {
if l.Types.Types() == "slack" {
switch level {
case DebugLevel:
return l.Slack.DebugWebhook()
Expand Down Expand Up @@ -215,7 +215,7 @@ func (l *Logger) resWebhookURLbyLevel(level Level) string {
}

func (l *Logger) Webhook() string {
if l.Types == "slack" {
if l.Types.Types() == "slack" {
return l.Slack.Webhook()
} else {
return l.Discord.Webhook()
Expand All @@ -224,55 +224,55 @@ func (l *Logger) Webhook() string {

// nosend webhook method.
func (l *Logger) NoSendWebhook() {
if l.Types == "slack" {
if l.Types.Types() == "slack" {
l.Slack.SetWebhook("nosend")
} else {
l.Discord.SetWebhook("nosend")
}
}

func (l *Logger) NoSendInfo() {
if l.Types == "slack" {
if l.Types.Types() == "slack" {
l.Slack.SetInfoWebhook("nosend")
} else {
l.Discord.SetInfoWebhook("nosend")
}
}

func (l *Logger) NoSendDebug() {
if l.Types == "slack" {
if l.Types.Types() == "slack" {
l.Slack.SetDebugWebhook("nosend")
} else {
l.Discord.SetDebugWebhook("nosend")
}
}

func (l *Logger) NoSendWarn() {
if l.Types == "slack" {
if l.Types.Types() == "slack" {
l.Slack.SetWarnWebhook("nosend")
} else {
l.Discord.SetWarnWebhook("nosend")
}
}

func (l *Logger) NoSendError() {
if l.Types == "slack" {
if l.Types.Types() == "slack" {
l.Slack.SetErrorWebhook("nosend")
} else {
l.Discord.SetErrorWebhook("nosend")
}
}

func (l *Logger) NoSendPanic() {
if l.Types == "slack" {
if l.Types.Types() == "slack" {
l.Slack.SetPanicWebhook("nosend")
} else {
l.Discord.SetPanicWebhook("nosend")
}
}

func (l *Logger) NoSendFatal() {
if l.Types == "slack" {
if l.Types.Types() == "slack" {
l.Slack.SetFatalWebhook("nosend")
} else {
l.Discord.SetFatalWebhook("nosend")
Expand All @@ -299,14 +299,14 @@ func (l *Logger) Log(ctx context.Context, level Level, args ...interface{}) {
}

// send log to discord or slack
if l.Types == "discord" {
if l.Types.Types() == "discord" {
dis := discord.SetWebhookStruct(l.Name, l.Img)
dis = discord.SetWebfookMessage(dis, text, level.String())
err := discord.SendLogToDiscord(webhook, dis)
if err != nil {
fmt.Printf("failed to send log to discord: %v\n", err)
}
} else if l.Types == "slack" {
} else if l.Types.Types() == "slack" {
sl := slack.SetWebfookMessage(text, level.String(), l.Name, l.Img)
err := slack.SendLogToSlack(webhook, sl)
if err != nil {
Expand Down Expand Up @@ -337,14 +337,14 @@ func (l *Logger) Logf(ctx context.Context, level Level, format string, args ...i
}

// send log to discord or slack
if l.Types == "discord" {
if l.Types.Types() == "discord" {
dis := discord.SetWebhookStruct(l.Name, l.Img)
dis = discord.SetWebfookMessage(dis, text, level.String())
err := discord.SendLogToDiscord(webhook, dis)
if err != nil {
fmt.Printf("failed to send log to discord: %v\n", err)
}
} else if l.Types == "slack" {
} else if l.Types.Types() == "slack" {
sl := slack.SetWebfookMessage(text, level.String(), l.Name, l.Img)
err := slack.SendLogToSlack(webhook, sl)
if err != nil {
Expand Down
40 changes: 40 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// MIT License

// Copyright (c) 2023 Yamasaki Shotaro

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

package loghook

// Option is a type of structure that holds types such as slack, discord, etc.
// You can freely customize this when you want to notify a different service in slack, discord, etc.
type Option struct {
// slack discord, etc.
types string
}

func (o *Option) Types() string {
return o.types
}

func NewOption(types string) *Option {
return &Option{
types: types,
}
}
38 changes: 38 additions & 0 deletions option_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// MIT License

// Copyright (c) 2023 Yamasaki Shotaro

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

package loghook

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestOption(t *testing.T) {
t.Run("NewOption", func(t *testing.T) {
t.Run("success", func(t *testing.T) {
option := NewOption("discord")
assert.Equal(t, "discord", option.Types())
})
})
}

0 comments on commit 8440a71

Please sign in to comment.