senlog

package module
v0.0.0-...-815978e Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2022 License: BSD-2-Clause Imports: 11 Imported by: 0

README

senlog

A convinient Golang logger for Sentry

senlog is a wrapper arround sentry-go, the official Sentry logging package for Go. It has an easy "inline" interface for structured logging with context. For example, consider the following log event with sentry-go:

sentry.WithScope(func(scope *sentry.Scope) {
	scope.SetLevel(sentry.LevelInfo)
		
    scope.SetContext("character", map[string]interface{}{
		"name":        "Mighty Fighter",
		"age":         19,
		"attack_type": "melee",
	})		
	
    sentry.CaptureMessage("Character Info")	
})

This could be written with senlog as the following single line of code:

senlog.Cxt("character").Set("name", "Mighty Fighter").Set("age", 19).Set("attack_type", "melee").INF("Character Info")

Got it? ;-)

Along with Sentry server, senlog output can be written to console and local file. Each output type is called destination. In the following section you can find the example code for each destination with usage.

Integration Example:

package main

import (
	"errors"

	"github.com/ejazmughal/senlog"
	"github.com/getsentry/sentry-go"
)

func main() {

	senlog.INF("'console' destination is added by default, that's why you see this message!")

	err := senlog.AddDestination("sentry", sentry.ClientOptions{
		Dsn:       <YOUR_SENTRY_DSN>,
		Transport: senlog.NewSentryTransport(senlog.DEBUG),
		//Environment: "senlog_test",
	})
	if err != nil {
		senlog.FTL(err, "Could not add 'sentry' destinantion")
	}

	senlog.INF("'sentry' destination is added by you, This message is logged on sentry.io")

	senlog.Set("FirstName", "Ejaz").Set("LastName", "Mughal").INF("This is a log event with 'Default Context' and string value")

	senlog.Cxt("User Defined Context").Set("someID", 107).Set("SomeKey", "some text value").INF("This is an example of a user defined context with integer and string value")

	err = errors.New("Some error")
	senlog.Set("someIntegerValue", 7).ERR(err, "Example error log")

	// You could even write log parallel to a local file
	logFile := "sen.log"
	err = senlog.AddDestination("file", sentry.ClientOptions{
		Transport: senlog.NewFileTransport(logFile, logFile, senlog.INFO),
	})

	if err != nil {
		senlog.FTL(err, "Could not add 'file' destinantion")
	}

	senlog.INF("This message will be written to all three destinations: console, sentry and file")

	// You can disbale output to stdout/stderr by removing 'console' destination
	// This could be usefull in production environment
	senlog.RemoveDestination("console")

	senlog.INF("This message will be written to two destinations: sentry and file")

	senlog.RemoveDestination("sentry")

	senlog.INF("This message will be written to local file only")
}

Documentation

Index

Constants

View Source
const (
	DEBUG = 1
	INFO  = 2
	WARN  = 3
	ERROR = 4
	FATAL = 5
)

log levels

View Source
const FlushTimeout = 2 * time.Second

Variables

This section is empty.

Functions

func AddDestination

func AddDestination(key string, options sentry.ClientOptions) error

func DBG

func DBG(v ...interface{})

func ERR

func ERR(e error, v ...interface{})

func FTL

func FTL(e error, v ...interface{})

func INF

func INF(v ...interface{})

Multiple parameter values will be concated without spaces!

func NewFileTransport

func NewFileTransport(outFile string, errFile string, minLogLevel int) *ioTransport

returns ioTransport with time and date

func NewIoTransport

func NewIoTransport(stdout io.Writer, stderr io.Writer, minLogLevel int) *ioTransport

returns ioTransport with time only line prefix

func RemoveDestination

func RemoveDestination(key string)

func SetLogLevel

func SetLogLevel(destinationKey string, minLevel int)

set min log level for a destinition

func WRN

func WRN(v ...interface{})

Types

type Colors

type Colors struct {
	RESET_COLOR   string
	TIME_COLOR    string
	CXT_KEY_COLOR string
	STACK_COLOR   string //stacktrack
}

type Context

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

func Cxt

func Cxt(k string) *Context

func Set

func Set(k string, v interface{}) *Context

func (*Context) Cxt

func (x *Context) Cxt(k string) *Context

func (*Context) DBG

func (x *Context) DBG(v ...interface{})

func (*Context) ERR

func (x *Context) ERR(e error, v ...interface{})

func (*Context) FTL

func (x *Context) FTL(e error, v ...interface{})

func (*Context) INF

func (x *Context) INF(v ...interface{})

func (*Context) Set

func (x *Context) Set(k string, v interface{}) *Context

func (*Context) WRN

func (x *Context) WRN(v ...interface{})

type LeveledLogger

type LeveledLogger interface {
	SetLogLevel(minLevel int)
	MinLogLevel() int
}

type Logger

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

func (*Logger) Call

func (tr *Logger) Call(SendEventFunc func(*sentry.Event), ev *sentry.Event)

func (*Logger) MinLogLevel

func (l *Logger) MinLogLevel() int

func (*Logger) SetLogLevel

func (l *Logger) SetLogLevel(level int)

type SentryTransport

type SentryTransport struct {
	Logger
	// contains filtered or unexported fields
}

func NewSentryTransport

func NewSentryTransport(minLogLevel int) *SentryTransport

func (*SentryTransport) Configure

func (tr *SentryTransport) Configure(options sentry.ClientOptions)

func (*SentryTransport) Flush

func (tr *SentryTransport) Flush(t time.Duration) bool

func (*SentryTransport) SendEvent

func (tr *SentryTransport) SendEvent(ev *sentry.Event)

Jump to

Keyboard shortcuts

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