go get github.com/seipan/loghook
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.
Code licensed under the MIT License.