Skip to content

Commit

Permalink
Merge pull request #24 from seipan/feat/send-level-method
Browse files Browse the repository at this point in the history
feat(logger) : Add setNosendWebhook method
  • Loading branch information
seipan committed Dec 12, 2023
2 parents ebf1510 + 5827d92 commit 3b8ffa4
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
41 changes: 40 additions & 1 deletion logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ type Logger struct {

Discord *Discord

SendLevel Level

// This is the url of the icon image of the bot that sends notifications to the discord
// ex) https://cdn-ak.f.st-hatena.com/images/fotolife/h/hikiniku0115/20190806/20190806000644.png
Img string
Expand Down Expand Up @@ -77,8 +79,43 @@ func (l *Logger) SetLevel(level Level) {
atomic.StoreUint32((*uint32)(&l.level), uint32(level))
}

// Sets the specified url in the webhook for each level
func (l *Logger) SetSendLevel(level Level) {
atomic.StoreUint32((*uint32)(&l.SendLevel), uint32(level))
}

func (l *Logger) setNosend() {
switch l.SendLevel {
case DebugLevel:
l.NoSendDebug()
case InfoLevel:
l.NoSendDebug()
l.NoSendInfo()
case WarnLevel:
l.NoSendDebug()
l.NoSendInfo()
l.NoSendWarn()
case ErrorLevel:
l.NoSendDebug()
l.NoSendInfo()
l.NoSendWarn()
l.NoSendError()
case PanicLevel:
l.NoSendDebug()
l.NoSendInfo()
l.NoSendWarn()
l.NoSendError()
l.NoSendPanic()
case FatalLevel:
l.NoSendDebug()
l.NoSendInfo()
l.NoSendWarn()
l.NoSendError()
l.NoSendPanic()
l.NoSendFatal()
}
}

// Sets the specified url in the webhook for each level
func (l *Logger) SetWebhook(webhook string) {
if l.Types == "slack" {
l.Slack.SetWebhook(webhook)
Expand Down Expand Up @@ -244,6 +281,7 @@ func (l *Logger) NoSendFatal() {

func (l *Logger) Log(ctx context.Context, level Level, args ...interface{}) {
if l.check(ctx, level) {
l.setNosend()
message := ""
message = fmt.Sprint(args...)

Expand Down Expand Up @@ -281,6 +319,7 @@ func (l *Logger) Log(ctx context.Context, level Level, args ...interface{}) {

func (l *Logger) Logf(ctx context.Context, level Level, format string, args ...interface{}) {
if l.check(ctx, level) {
l.setNosend()
message := ""
message = fmt.Sprintf(format, args...)

Expand Down
10 changes: 10 additions & 0 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ package loghook
import (
"context"
"testing"

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

var (
Expand Down Expand Up @@ -81,3 +83,11 @@ func TestSlackExampleWithContext(t *testing.T) {
logger.NoSendWarn()
logger.WarnContext(context.Background(), "test")
}

func TestSetNosend(t *testing.T) {
logger := NewLogger("", "test", "discord", DiscordWebhookURL)
logger.SetSendLevel(DebugLevel)
logger.setNosend()

assert.Equal(t, "nosend", logger.resWebhookURLbyLevel(DebugLevel))
}

0 comments on commit 3b8ffa4

Please sign in to comment.