Skip to content
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

feat(option) : Add Option struct #32

Merged
merged 3 commits into from
Dec 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 23 additions & 23 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
level Level
mutex sync.Mutex

Types string
Types Option

Slack *Slack

Expand All @@ -61,7 +61,7 @@
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) 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 @@

// 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" {

Check warning on line 128 in logger.go

View check run for this annotation

Codecov / codecov/patch

logger.go#L128

Added line #L128 was not covered by tests
l.Slack.SetDebugWebhook(webhook)
} else {
l.Discord.SetDebugWebhook(webhook)
}
}

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

Check warning on line 136 in logger.go

View check run for this annotation

Codecov / codecov/patch

logger.go#L136

Added line #L136 was not covered by tests
l.Slack.SetInfoWebhook(webhook)
} else {
l.Discord.SetInfoWebhook(webhook)
}
}

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

Check warning on line 144 in logger.go

View check run for this annotation

Codecov / codecov/patch

logger.go#L144

Added line #L144 was not covered by tests
l.Slack.SetWarnWebhook(webhook)
} else {
l.Discord.SetWarnWebhook(webhook)
}
}

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

Check warning on line 152 in logger.go

View check run for this annotation

Codecov / codecov/patch

logger.go#L152

Added line #L152 was not covered by tests
l.Slack.SetErrorWebhook(webhook)
} else {
l.Discord.SetErrorWebhook(webhook)
}
}

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

Check warning on line 160 in logger.go

View check run for this annotation

Codecov / codecov/patch

logger.go#L160

Added line #L160 was not covered by tests
l.Slack.SetPanicWebhook(webhook)
} else {
l.Discord.SetPanicWebhook(webhook)
}
}

func (l *Logger) SetFatalWebhook(webhook string) {
if l.Types == "slack" {
if l.Types.Types() == "slack" {

Check warning on line 168 in logger.go

View check run for this annotation

Codecov / codecov/patch

logger.go#L168

Added line #L168 was not covered by tests
l.Slack.SetFatalWebhook(webhook)
} else {
l.Discord.SetFatalWebhook(webhook)
Expand All @@ -177,7 +177,7 @@
}

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) Webhook() string {
if l.Types == "slack" {
if l.Types.Types() == "slack" {

Check warning on line 218 in logger.go

View check run for this annotation

Codecov / codecov/patch

logger.go#L218

Added line #L218 was not covered by tests
return l.Slack.Webhook()
} else {
return l.Discord.Webhook()
Expand All @@ -224,55 +224,55 @@

// nosend webhook method.
func (l *Logger) NoSendWebhook() {
if l.Types == "slack" {
if l.Types.Types() == "slack" {

Check warning on line 227 in logger.go

View check run for this annotation

Codecov / codecov/patch

logger.go#L227

Added line #L227 was not covered by tests
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" {

Check warning on line 259 in logger.go

View check run for this annotation

Codecov / codecov/patch

logger.go#L259

Added line #L259 was not covered by tests
l.Slack.SetErrorWebhook("nosend")
} else {
l.Discord.SetErrorWebhook("nosend")
}
}

func (l *Logger) NoSendPanic() {
if l.Types == "slack" {
if l.Types.Types() == "slack" {

Check warning on line 267 in logger.go

View check run for this annotation

Codecov / codecov/patch

logger.go#L267

Added line #L267 was not covered by tests
l.Slack.SetPanicWebhook("nosend")
} else {
l.Discord.SetPanicWebhook("nosend")
}
}

func (l *Logger) NoSendFatal() {
if l.Types == "slack" {
if l.Types.Types() == "slack" {

Check warning on line 275 in logger.go

View check run for this annotation

Codecov / codecov/patch

logger.go#L275

Added line #L275 was not covered by tests
l.Slack.SetFatalWebhook("nosend")
} else {
l.Discord.SetFatalWebhook("nosend")
Expand All @@ -299,14 +299,14 @@
}

// send log to discord or slack
if l.Types == "discord" {
if l.Types.Types() == "discord" {

Check warning on line 302 in logger.go

View check run for this annotation

Codecov / codecov/patch

logger.go#L302

Added line #L302 was not covered by tests
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" {

Check warning on line 309 in logger.go

View check run for this annotation

Codecov / codecov/patch

logger.go#L309

Added line #L309 was not covered by tests
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 @@
}

// send log to discord or slack
if l.Types == "discord" {
if l.Types.Types() == "discord" {

Check warning on line 340 in logger.go

View check run for this annotation

Codecov / codecov/patch

logger.go#L340

Added line #L340 was not covered by tests
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" {

Check warning on line 347 in logger.go

View check run for this annotation

Codecov / codecov/patch

logger.go#L347

Added line #L347 was not covered by tests
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())
})
})
}
Loading