alert

package
v0.0.0-...-fb9b9fb Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2017 License: MIT Imports: 17 Imported by: 0

README

alert

A package to send alert messages

Usage

With no configuration:

package main

import (
  "github.com/enova/tokyo/src/alert"
)

func main() {
  alert.Cerr("Write to stderr")
  alert.Info("Information message")
  alert.Warn("Warning message")
  alert.Exit("Major issue, exiting")
}

To enable confugration, the alert package must be used in conjunction with the config pacakge cfg. In your config file, include an Alert section:

test.cfg
--------

Alert.Sentry.Use   true
Alert.LogFile.Use  true
Alert.LogFile.Dir  log

The Use lines are optional. If they don't exist then alert will assume the value is false and that functionality will not be activated. If any of the Use lines are true, alert will complain if the config file is missing any of the required fields for that subsection. So for example, if Alert.LogFile.Use is set to true, then the config file must also have a line for Alert.LogFile.Dir (see Activating Log-Files below).

In your application you must call alert.Set() to configure alerts:

package main

import (
  "github.com/enova/tokyo/src/alert"
  "github.com/enova/tokyo/src/cfg"
)

func main() {

  // Create Configuration
  cfg := cfg.New("test.cfg")

  // Configure Alerts
  alert.Set(cfg)
  
  alert.Cerr("Write to stderr")
  alert.Info("Information message")
  alert.Warn("Warning message")
  alert.Exit("Major issue, exiting")
}

Each of the alert methods accepts an arbitrary list of arguments (i.e. ...interface{}):

s := "go"
i := 5
f := 12.3
m := []int{1, 2, 3}
t := time.Now()

alert.Warn(s, i, f, m, t)

Activating Sentry

To activate Sentry, add the following lines to your config file:

Alert.Sentry.Use  True
Alert.Sentry.DSN  https://abc123...

Any calls to alert.Info(), alert.Warn() and alert.Exit() will then send your message to the specified Sentry DSN. The following tags will be sent in the packet:

user - user executing the application
app  - application name
cmd  - the full command line being executed
pid  - the process PID

You can add additional tags within the config file:

Alert.Sentry.Tag  color blue
Alert.Sentry.Tag  blood-type 0
Alert.Sentry.Tag  cities Tokyo Osaka Kyoto

Each tag value must contain at least two tokens (key value). Additional values on the same line will be joined into a single value (e.g. above "cities" => "Tokyo Osaka Kyoto"). These tags will be added to every Sentry message that the application emits.

If you wish to inhibit emitting a Sentry packet for a particular alert then add the special flag alert.Whisper to the argument list:

for i := 0; i < 100000; i++ {

  // Message
  msg := fmt.Sprinf("The value of I is %d", i)

  // Send Alert But Skip Sentry
  alert.Info(msg, alert.Whisper)
}

The flag alert.Whisper is meant to replace the synonomous (but depracated) flag alert.SkipMail.

Activating Log-File

To activate logging to a file, add the following lines to your config file:

Alert.LogFile.Use True
Alert.LogFile.Dir /var/log/

If the directory does not exist, it will be created. Any calls to alert.Info(), alert.Warn() and alert.Exit() will then write messages to a file in that directory. The filename will be generated using the application name, the current date and time, and the process PID. For example:

/var/log/myapp_20151022_151843_000_46747.log

Adding Your Own Alert-Handler

If you would like to react to alerts using your own logging mechanism you can implement a custom handler to wire in your software. Simple implement the alert.Handler interface:

// Handler is the interface for alert-handlers
type Handler interface {
    Handle(msg Message)
}

and add your instance using alert.AddHandler(). Here is a complete example:

package main

import (
  "github.com/enova/tokyo/src/alert"
)

type MyLogger struct {}
func (m *MyLogger) Handle(msg Message) {
  // ... do something with the message
  fmt.Println(msg.Text, "received at", msg.Now)
}

func main() {
  logger := &MyLogger{}
  alert.AddHandler(logger)
  alert.Info("I should be handling this now")
}

Documentation

Index

Constants

View Source
const MaxMulticastPerHour int = 100

MaxMulticastPerHour limits the number of messages sent to Multicast

View Source
const MaxSentryPerHour int = 100

MaxSentryPerHour limits the number of messages sent to Sentry

View Source
const MaxTextLength int = 128

MaxTextLength limits the length of text sent in the Text field of the emitted packet

Variables

This section is empty.

Functions

func AddHandler

func AddHandler(h Handler)

AddHandler adds the supplied handler to the list of handlers

func Cerr

func Cerr(msg string)

Cerr ...

func Exit

func Exit(msgs ...interface{})

Exit ...

func ExitIf

func ExitIf(failure bool, msgs ...interface{})

ExitIf ...

func ExitOn

func ExitOn(err error, msgs ...interface{})

ExitOn ...

func Info

func Info(msgs ...interface{})

Info ...

func PanicOnExit

func PanicOnExit()

PanicOnExit will cause the alert package to call panic() instead of os.exit()

func Set

func Set(cfg *cfg.Config)

Set configures the alert settings

func SetErr

func SetErr(w io.Writer)

SetErr sets the writer for console output. The default writer is os.Stderr.

func SetLogDir

func SetLogDir(dir string)

SetLogDir ...

func Warn

func Warn(msgs ...interface{})

Warn ...

func WarnIf

func WarnIf(failure bool, msgs ...interface{})

WarnIf invokes a warning if there was a failure

func WarnOn

func WarnOn(err error, msgs ...interface{})

WarnOn invokes a warning containing both the error message and the supplied message if there was an error

Types

type Flag

type Flag int

Flag ...

const (

	// Whisper inhibits a message from being sent externally (e.g. Sentry, Multicast)
	Whisper Flag = 0

	// SkipMail is the same as Whisper (Deprecated)
	SkipMail Flag = 0
)

Flags

func (Flag) String

func (f Flag) String() string

type Handler

type Handler interface {
	Handle(msg Message)
}

Handler is the interface for alert-handlers

type Level

type Level int

Level ...

const (
	LevelInfo Level = 0 // INFO
	LevelWarn Level = 1 // WARN
	LevelExit Level = 2 // EXIT
)

Levels are the levels at which messages are alerted.

func (Level) String

func (l Level) String() string

type Message

type Message struct {
	Meta
	Now   time.Time
	Level Level
	Text  string
	Flags []Flag
}

Message ...

func (*Message) Pretty

func (m *Message) Pretty() string

Pretty returns a colorful string for Console/Log-File It also appends a stack-trace for warnings and above

func (*Message) Whisper

func (m *Message) Whisper() bool

Whisper searches for the presence of the Whisper flag

type Meta

type Meta struct {
	User string
	App  string
	PID  string
}

Meta ...

type Multicaster

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

Multicaster provides functionality for emitting multicast messages

func NewMulticaster

func NewMulticaster(ip string, port, ttl int) *Multicaster

NewMulticaster returns a new Multicaster object

func (*Multicaster) Emit

func (m *Multicaster) Emit(message []byte)

Emit sends a message over multicast

func (*Multicaster) SetTTL

func (m *Multicaster) SetTTL(amount int)

SetTTL sets the time to live for future packets

type Throttle

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

Throttle ...

func NewThrottle

func NewThrottle(limit int) *Throttle

NewThrottle ...

func (*Throttle) Update

func (t *Throttle) Update(now time.Time) bool

Update ...

Jump to

Keyboard shortcuts

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