From 95265ec93ab6b30ff31245d2f1827d04c0fac591 Mon Sep 17 00:00:00 2001 From: seipan Date: Sat, 16 Dec 2023 19:50:30 +0900 Subject: [PATCH 1/3] feat(option) : Add Option struct --- logger.go | 46 +++++++++++++++++++++++----------------------- option.go | 15 +++++++++++++++ option_test.go | 1 + 3 files changed, 39 insertions(+), 23 deletions(-) create mode 100644 option.go create mode 100644 option_test.go diff --git a/logger.go b/logger.go index d87ec09..7a5ce69 100644 --- a/logger.go +++ b/logger.go @@ -39,7 +39,7 @@ type Logger struct { level Level mutex sync.Mutex - Types string + Types Option Slack *Slack @@ -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), } @@ -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) { @@ -117,7 +117,7 @@ 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) @@ -125,7 +125,7 @@ func (l *Logger) SetWebhook(webhook string) { } func (l *Logger) SetDebugWebhook(webhook string) { - if l.Types == "slack" { + if l.Types.Types() == "slack" { l.Slack.SetDebugWebhook(webhook) } else { l.Discord.SetDebugWebhook(webhook) @@ -133,7 +133,7 @@ func (l *Logger) SetDebugWebhook(webhook string) { } func (l *Logger) SetInfoWebhook(webhook string) { - if l.Types == "slack" { + if l.Types.Types() == "slack" { l.Slack.SetInfoWebhook(webhook) } else { l.Discord.SetInfoWebhook(webhook) @@ -141,7 +141,7 @@ func (l *Logger) SetInfoWebhook(webhook string) { } func (l *Logger) SetWarnWebhook(webhook string) { - if l.Types == "slack" { + if l.Types.Types() == "slack" { l.Slack.SetWarnWebhook(webhook) } else { l.Discord.SetWarnWebhook(webhook) @@ -149,7 +149,7 @@ func (l *Logger) SetWarnWebhook(webhook string) { } func (l *Logger) SetErrorWebhook(webhook string) { - if l.Types == "slack" { + if l.Types.Types() == "slack" { l.Slack.SetErrorWebhook(webhook) } else { l.Discord.SetErrorWebhook(webhook) @@ -157,7 +157,7 @@ func (l *Logger) SetErrorWebhook(webhook string) { } func (l *Logger) SetPanicWebhook(webhook string) { - if l.Types == "slack" { + if l.Types.Types() == "slack" { l.Slack.SetPanicWebhook(webhook) } else { l.Discord.SetPanicWebhook(webhook) @@ -165,7 +165,7 @@ func (l *Logger) SetPanicWebhook(webhook string) { } func (l *Logger) SetFatalWebhook(webhook string) { - if l.Types == "slack" { + if l.Types.Types() == "slack" { l.Slack.SetFatalWebhook(webhook) } else { l.Discord.SetFatalWebhook(webhook) @@ -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() @@ -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() @@ -224,7 +224,7 @@ 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") @@ -232,7 +232,7 @@ func (l *Logger) NoSendWebhook() { } func (l *Logger) NoSendInfo() { - if l.Types == "slack" { + if l.Types.Types() == "slack" { l.Slack.SetInfoWebhook("nosend") } else { l.Discord.SetInfoWebhook("nosend") @@ -240,7 +240,7 @@ func (l *Logger) NoSendInfo() { } func (l *Logger) NoSendDebug() { - if l.Types == "slack" { + if l.Types.Types() == "slack" { l.Slack.SetDebugWebhook("nosend") } else { l.Discord.SetDebugWebhook("nosend") @@ -248,7 +248,7 @@ func (l *Logger) NoSendDebug() { } func (l *Logger) NoSendWarn() { - if l.Types == "slack" { + if l.Types.Types() == "slack" { l.Slack.SetWarnWebhook("nosend") } else { l.Discord.SetWarnWebhook("nosend") @@ -256,7 +256,7 @@ func (l *Logger) NoSendWarn() { } func (l *Logger) NoSendError() { - if l.Types == "slack" { + if l.Types.Types() == "slack" { l.Slack.SetErrorWebhook("nosend") } else { l.Discord.SetErrorWebhook("nosend") @@ -264,7 +264,7 @@ func (l *Logger) NoSendError() { } func (l *Logger) NoSendPanic() { - if l.Types == "slack" { + if l.Types.Types() == "slack" { l.Slack.SetPanicWebhook("nosend") } else { l.Discord.SetPanicWebhook("nosend") @@ -272,7 +272,7 @@ func (l *Logger) NoSendPanic() { } func (l *Logger) NoSendFatal() { - if l.Types == "slack" { + if l.Types.Types() == "slack" { l.Slack.SetFatalWebhook("nosend") } else { l.Discord.SetFatalWebhook("nosend") @@ -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 { @@ -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 { diff --git a/option.go b/option.go new file mode 100644 index 0000000..2128267 --- /dev/null +++ b/option.go @@ -0,0 +1,15 @@ +package loghook + +type Option struct { + types string +} + +func (o *Option) Types() string { + return o.types +} + +func NewOption(types string) *Option { + return &Option{ + types: types, + } +} diff --git a/option_test.go b/option_test.go new file mode 100644 index 0000000..5c484ea --- /dev/null +++ b/option_test.go @@ -0,0 +1 @@ +package loghook From 894e5b7c5b8b1d1f091881ccffa5391252d60ce4 Mon Sep 17 00:00:00 2001 From: seipan Date: Sat, 16 Dec 2023 19:51:51 +0900 Subject: [PATCH 2/3] test(option) : Add OptionTest --- option_test.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/option_test.go b/option_test.go index 5c484ea..7df485c 100644 --- a/option_test.go +++ b/option_test.go @@ -1 +1,16 @@ 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()) + }) + }) +} From 2225e8698928ba51d9432a3e833753f0f70d44f0 Mon Sep 17 00:00:00 2001 From: seipan Date: Sat, 16 Dec 2023 19:54:51 +0900 Subject: [PATCH 3/3] doc(option): Add licsence and doc for option --- option.go | 25 +++++++++++++++++++++++++ option_test.go | 22 ++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/option.go b/option.go index 2128267..c728212 100644 --- a/option.go +++ b/option.go @@ -1,6 +1,31 @@ +// 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 } diff --git a/option_test.go b/option_test.go index 7df485c..a69aa7f 100644 --- a/option_test.go +++ b/option_test.go @@ -1,3 +1,25 @@ +// 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 (