loghook

package module
v0.3.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 17, 2023 License: MIT Imports: 10 Imported by: 0

README

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Discord

type Discord struct {
	// contains filtered or unexported fields
}

This structure Discord holds the webhook url for logging to discord.

func NewDiscord

func NewDiscord(webhook string) *Discord

func (*Discord) DebugWebhook

func (d *Discord) DebugWebhook() string

func (*Discord) ErrorWebhook

func (d *Discord) ErrorWebhook() string

func (*Discord) FatalWebhook

func (d *Discord) FatalWebhook() string

func (*Discord) InfoWebhook

func (d *Discord) InfoWebhook() string

func (*Discord) PanicWebhook

func (d *Discord) PanicWebhook() string

func (*Discord) SetDebugWebhook

func (d *Discord) SetDebugWebhook(webhook string)

Set the webhookurl for the Debug level.

func (*Discord) SetErrorWebhook

func (d *Discord) SetErrorWebhook(webhook string)

Set the webhookurl for the Error level.

func (*Discord) SetFatalWebhook

func (d *Discord) SetFatalWebhook(webhook string)

Set the webhookurl for the Fatal level.

func (*Discord) SetInfoWebhook

func (d *Discord) SetInfoWebhook(webhook string)

Set the webhookurl for the Info level.

func (*Discord) SetPanicWebhook

func (d *Discord) SetPanicWebhook(webhook string)

Set the webhookurl for the Panic level.

func (*Discord) SetWarnWebhook

func (d *Discord) SetWarnWebhook(webhook string)

Set the webhookurl for the Warn level.

func (*Discord) SetWebhook

func (d *Discord) SetWebhook(webhook string)

Sets the webhook url. This url will be used if the webhook url for any level is not set

func (*Discord) WarnWebhook

func (d *Discord) WarnWebhook() string

func (*Discord) Webhook

func (d *Discord) Webhook() string

type Level

type Level uint32

Level is a logging priority. Higher levels are more important.

const (
	// Debug level logs are used for debugging
	DebugLevel Level = iota
	// Info level logs are logs at a lower level and are used to preserve information.
	InfoLevel
	// Warn level is a higher level of logging than Info level
	// and is usually used to output more important logs than the info log.
	WarnLevel
	// Error level is a higher level of logging and is usually used to output a log of errors
	ErrorLevel
	// PanicLevel logs a message, then panics.
	PanicLevel
	// FatalLevel logs a message, then calls os.Exit(1).
	FatalLevel
)

func ParseLevel

func ParseLevel(lvl string) (Level, error)

func (*Level) Get

func (l *Level) Get() interface{}

Get the level

func (Level) MarshalText

func (l Level) MarshalText() string

func (*Level) Set

func (l *Level) Set(s string) error

Converts from string to level and sets

func (Level) String

func (l Level) String() string

Converts log level to string, returns unknown if log level is not expected.

func (*Level) UnmarshalText

func (l *Level) UnmarshalText(text string) error

Converts the string representing level to level. Returns an error if the level is not expected

func (Level) UppercaseString

func (l Level) UppercaseString() string

type Logger

type Logger struct {
	Types Option

	Slack *Slack

	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

	// This is the name of the bot that will send notifications to the discord
	// ex) hogehoge
	Name string
	// contains filtered or unexported fields
}

This structure defines what is needed to output logs to any channel on discord or slack.

func NewLogger

func NewLogger(img string, name string, types string, webhook string) *Logger

func (*Logger) Debug

func (l *Logger) Debug(i ...interface{})

func (*Logger) DebugContext added in v0.1.9

func (l *Logger) DebugContext(ctx context.Context, i ...interface{})

func (*Logger) Debugf

func (l *Logger) Debugf(format string, i ...interface{})

func (*Logger) Error

func (l *Logger) Error(i ...interface{})

func (*Logger) ErrorContext added in v0.1.9

func (l *Logger) ErrorContext(ctx context.Context, i ...interface{})

func (*Logger) Errorf

func (l *Logger) Errorf(format string, i ...interface{})

func (*Logger) Fatal

func (l *Logger) Fatal(i ...interface{})

func (*Logger) FatalContext added in v0.1.9

func (l *Logger) FatalContext(ctx context.Context, i ...interface{})

func (*Logger) Fatalf

func (l *Logger) Fatalf(format string, i ...interface{})

func (*Logger) Info

func (l *Logger) Info(i ...interface{})

func (*Logger) InfoContext added in v0.1.9

func (l *Logger) InfoContext(ctx context.Context, format string, i ...interface{})

func (*Logger) Infof

func (l *Logger) Infof(format string, i ...interface{})

func (*Logger) Level

func (l *Logger) Level() Level

func (*Logger) Log

func (l *Logger) Log(ctx context.Context, level Level, args ...interface{})

func (*Logger) Logf

func (l *Logger) Logf(ctx context.Context, level Level, format string, args ...interface{})

func (*Logger) NoSendDebug

func (l *Logger) NoSendDebug()

func (*Logger) NoSendError

func (l *Logger) NoSendError()

func (*Logger) NoSendFatal

func (l *Logger) NoSendFatal()

func (*Logger) NoSendInfo

func (l *Logger) NoSendInfo()

func (*Logger) NoSendPanic

func (l *Logger) NoSendPanic()

func (*Logger) NoSendWarn

func (l *Logger) NoSendWarn()

func (*Logger) NoSendWebhook

func (l *Logger) NoSendWebhook()

nosend webhook method.

func (*Logger) Panic

func (l *Logger) Panic(i ...interface{})

func (*Logger) PanicContext added in v0.1.9

func (l *Logger) PanicContext(ctx context.Context, i ...interface{})

func (*Logger) Panicf

func (l *Logger) Panicf(format string, i ...interface{})

func (*Logger) SetDebugWebhook

func (l *Logger) SetDebugWebhook(webhook string)

func (*Logger) SetErrorWebhook

func (l *Logger) SetErrorWebhook(webhook string)

func (*Logger) SetFatalWebhook

func (l *Logger) SetFatalWebhook(webhook string)

func (*Logger) SetInfoWebhook

func (l *Logger) SetInfoWebhook(webhook string)

func (*Logger) SetLevel

func (l *Logger) SetLevel(level Level)

func (*Logger) SetPanicWebhook

func (l *Logger) SetPanicWebhook(webhook string)

func (*Logger) SetSendLevel added in v0.2.0

func (l *Logger) SetSendLevel(level Level)

func (*Logger) SetWarnWebhook

func (l *Logger) SetWarnWebhook(webhook string)

func (*Logger) SetWebhook

func (l *Logger) SetWebhook(webhook string)

Sets the specified url in the webhook for each level

func (*Logger) Warn

func (l *Logger) Warn(i ...interface{})

func (*Logger) WarnContext added in v0.1.9

func (l *Logger) WarnContext(ctx context.Context, i ...interface{})

func (*Logger) Warnf

func (l *Logger) Warnf(format string, i ...interface{})

func (*Logger) Webhook

func (l *Logger) Webhook() string

type Option added in v0.3.0

type Option struct {
	// contains filtered or unexported fields
}

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.

func NewOption added in v0.3.0

func NewOption(types string) *Option

func (*Option) Types added in v0.3.0

func (o *Option) Types() string

type Slack

type Slack struct {
	// contains filtered or unexported fields
}

This structure slack holds the webhook url for logging to slack. This webhook assumes that the Incoming Webhook is used.

func NewSlack

func NewSlack(webhook string) *Slack

func (*Slack) DebugWebhook

func (s *Slack) DebugWebhook() string

func (*Slack) ErrorWebhook

func (s *Slack) ErrorWebhook() string

func (*Slack) FatalWebhook

func (s *Slack) FatalWebhook() string

func (*Slack) InfoWebhook

func (s *Slack) InfoWebhook() string

func (*Slack) PanicWebhook

func (s *Slack) PanicWebhook() string

func (*Slack) SetDebugWebhook

func (s *Slack) SetDebugWebhook(webhook string)

Set the webhookurl for the Debug level.

func (*Slack) SetErrorWebhook

func (s *Slack) SetErrorWebhook(webhook string)

Set the webhookurl for the Error level.

func (*Slack) SetFatalWebhook

func (s *Slack) SetFatalWebhook(webhook string)

Set the webhookurl for the Fatal level.

func (*Slack) SetInfoWebhook

func (s *Slack) SetInfoWebhook(webhook string)

Set the webhookurl for the Info level.

func (*Slack) SetPanicWebhook

func (s *Slack) SetPanicWebhook(webhook string)

Set the webhookurl for the Panic level.

func (*Slack) SetWarnWebhook

func (s *Slack) SetWarnWebhook(webhook string)

Set the webhookurl for the Warn level.

func (*Slack) SetWebhook

func (s *Slack) SetWebhook(webhook string)

Sets the webhook url. This url will be used if the webhook url for any level is not set

func (*Slack) WarnWebhook

func (s *Slack) WarnWebhook() string

func (*Slack) Webhook

func (s *Slack) Webhook() string

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL