mailhook

package module
v0.0.0-...-e8a00c6 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2023 License: MIT Imports: 3 Imported by: 0

README

In some deployments, you'll want to report errors by email. If you add this hook, an email will send for the following levels:

  • Error
  • Fatal
  • Panic

The subject is of the form APPLICATION_NAME - LEVEL and the body contains the timestamp and the message.

Installation

Install the package with go:

go get gopkg.in/gomail.v2
go get github.com/unievolver/mailhook

Usage

For APPLICATION_NAME, substitute a short string that will identify your application or service in the logs.

import (
  "log/syslog"
  "github.com/sirupsen/logrus"
  "github.com/unievolver/mailhook"
  "gopkg.in/gomail.v2"
)

func main() {
  log       := logrus.New()
  // if you do not need authentication for your smtp host
  hook, err := mailhook.NewMailHook(
	"HOST", PORT, "USERNAME", "PASSWORD",
	"To", "APPLICATION_NAME",
  )
  if err == nil {
    log.AddHook(hook)
  }
}

Example with authentication:

  // if you need authentication for your smtp host
  hook, err := mailhook.NewMailHook("HOST", PORT, "USERNAME", "PASSWORD", "TO", "APPLICATION_NAME")

If you want to send mails with 163:

 hook, err := mailhook.NewMailHook(
	"smtp.163.com", 465, "user.name@163.com", "password",
	"user.name@163.com", "testapp",
	)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MailHook

type MailHook struct {
	Host     string
	Port     int
	Username string
	Password string
	From     *mail.Address
	To       []*mail.Address
	AppName  string
	// contains filtered or unexported fields
}

MailHook to sends logs by email with authentication.

func NewMailHook

func NewMailHook(host string, port int, username string, password string, to string, appname string) (*MailHook, error)

NewMailHook creates a hook to be added to an instance of logger.

func (*MailHook) Fire

func (hook *MailHook) Fire(entry *logrus.Entry) error

Fire is called when a log event is fired.

func (*MailHook) Levels

func (hook *MailHook) Levels() []logrus.Level

Levels returns the available logging levels.

func (*MailHook) SetLevels

func (hook *MailHook) SetLevels(levels []logrus.Level)

Levels returns the available logging levels.

Jump to

Keyboard shortcuts

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