snitch

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2022 License: MIT Imports: 6 Imported by: 0

README

snitch

Meant for small projects. Don't try this at scale and don't trust all notifications to come through. It will not block logging, but will drop alerts on network errors, high loads or telegram rate limits.

Package snitch implements a thin wrapper around zap logger that snitches log messages according to log level to specified Telegram chat through your bot.

Example Usage

package main

import (
	"time"

	"github.com/barklan/snitch"
	"go.uber.org/zap"
)

func main() {
	logger, err := zap.NewDevelopment()
	if err != nil {
		panic(err)
	}

	sn, err := snitch.OnZap(logger, &snitch.Config{
		TGToken:   "50804fdfsf89383f3fxWu08889j9sdghopfuh8988FFFdI",
		// Telegram chat ID.
		TGChatID:  -438388543,
		// Level above which to send logs.
		Level:     snitch.InfoLevel,
		// Don't send the same message for 30 seconds.
		Cooldown:  30 * time.Second,
		// Adaptive Replacement Cache size for log events.
		CacheSize: 10,
	})
	if err != nil {
		panic(err)
	}

	for time := range time.Tick(1 * time.Second) {
		sn.Info("some info message", zap.Time("time", time))
	}
}

Documentation

Overview

Package snitch implements a thin wrapper around `zap` logger that snitches log messages according to log level to specified Telegram chat through your bot.

Index

Constants

View Source
const (
	DebugPrefix = "DEBUG: "
	InfoPrefix  = "INFO: "
	WarnPrefix  = "WARN: "
	ErrorPrefix = "ERROR: "
	CritPrefix  = "CRIT: "
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	TGToken   string
	TGChatID  int64
	Level     Level
	Cooldown  time.Duration
	CacheSize int
}

type Level

type Level uint8
const (
	DebugLevel Level = iota
	InfoLevel
	WarnLevel
	ErrorLevel
	CritLevel
	NoLevel
)

type Zap added in v0.2.0

type Zap struct {
	Conf *Config

	L *zap.Logger
	// contains filtered or unexported fields
}

func OnZap

func OnZap(logger *zap.Logger, conf *Config) (*Zap, error)

Function OnZap constructs wrapper around zap.Logger.

func (*Zap) Debug added in v0.2.0

func (s *Zap) Debug(msg string, fields ...zapcore.Field)

Debug directly calls logger.Debug().

func (*Zap) Error added in v0.2.0

func (s *Zap) Error(msg string, fields ...zapcore.Field)

func (*Zap) Fatal added in v0.2.0

func (s *Zap) Fatal(msg string, fields ...zapcore.Field)

Fatal snitches if level <= CritLevel and calls logger.Fatal().

func (*Zap) Info added in v0.2.0

func (s *Zap) Info(msg string, fields ...zapcore.Field)

func (*Zap) Panic added in v0.2.0

func (s *Zap) Panic(msg string, fields ...zapcore.Field)

Panic snitches if level <= CritLevel and calls logger.Panic().

func (*Zap) Warn added in v0.2.0

func (s *Zap) Warn(msg string, fields ...zapcore.Field)

Directories

Path Synopsis
Package mock_snitch is a generated GoMock package.
Package mock_snitch is a generated GoMock package.

Jump to

Keyboard shortcuts

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