Skip to content

seipan/loghook

Repository files navigation

Last commit Repository Stars Issues Open Issues go codecov

eyecatch

loghook

⚡ logger to notify logs to slack,discord using webhook ⚡



Installation

go get github.com/seipan/loghook

Usage

When using it, you need to obtain the default webhook for discord and the incoming webhook for slack in advance.

package discord

import "github.com/seipan/loghook"

var (
	// DiscordWebhookURL is a webhook url for discord.
	DiscordWebhookURL = "https://discord.com/api/webhooks/xxxxxxxx/xxxxxxxx"
)

func main() {
	logger := loghook.NewLogger("", "test", "discord", DiscordWebhookURL)
	logger.SetLevel(loghook.DebugLevel)
	logger.SetWebhook(DiscordWebhookURL)

	logger.Debug("test")
	logger.Infof("test %s", "info")
}

If you do not want to be notified of a particular log level, you can set

package discord

import "github.com/seipan/loghook"

var (
	// DiscordWebhookURL is a webhook url for discord.
	DiscordWebhookURL = "https://discord.com/api/webhooks/xxxxxxxx/xxxxxxxx"
)

func main(){
	logger := loghook.NewLogger("", "test", "discord", DiscordWebhookURL)

	logger.NoSendDebug()
	logger.Debug("test")
	logger.NoSendInfo()
	logger.Infof("test %s", "info")
}

You can also change the webhook to be notified for each log level

package discord

import "github.com/seipan/loghook"

var (
	// DiscordWebhookURL is a webhook url for discord.
	DiscordWebhookURL = "https://discord.com/api/webhooks/xxxxxxxx/xxxxxxxx"
)

func main(){
	logger := loghook.NewLogger("", "test", "discord", DiscordWebhookURL)

	logger.SetErrorWebhook(DiscordErrorWebhookURL)
	logger.Error("test")
}

There is also a method that takes 'context' as an argument

package discord

import "github.com/seipan/loghook"

var (
	// DiscordWebhookURL is a webhook url for discord.
	DiscordWebhookURL = "https://discord.com/api/webhooks/xxxxxxxx/xxxxxxxx"
)

func main(){
	logger := loghook.NewLogger("", "test", "discord", DiscordWebhookURL)

	logger.ErrorContext("test")
}

If you want a more detailed example, please see the examples.

License

Code licensed under the MIT License.

Author

seipan.

Star History

Star History Chart