logger

package module
v0.0.17 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2024 License: AGPL-3.0 Imports: 11 Imported by: 1

README

Logger

Versatile logging PKG for logging

Usage

Create a logger instance

To create a basic logger pass in a context and a span name.

*Right now the span name doesn't act like a true span

ctx, log := logger.New(ctx, "spanName")
Create a logger instance with extra fields

You can pass in extra fields as options that will get logged as details with every log

ctx, log := logger.New(
    ctx,
    "spanName",
    logger.WithField("fieldName", "valueToLog"),
)
Error Log

logs a message prefixed with Error:

// basic error log
log.Error(ctx, "this is an error message")

// with an extra field
log.WithField("extraFieldName", "extraFieldValue").Error(ctx, "this is an error message with extra field")

// with error
err := someFunction()
if err != nil {
    log.WithError(err).Error(ctx, "this is an error message with an error attached")
}

// with error and field
err := someFunction()
if err != nil {
    log.WithError(err).
        WithField("extraFieldName", "extraFieldValue").
        Error(ctx, "this is an error message with an error attached")
}
Fatal Log

logs a message prefixed with Fatal:

// basic Fatal log
log.Fatal(ctx, "this is a fatal error message")

// with an extra field
log.WithField("extraFieldName", "extraFieldValue").Fatal(ctx, "this is a fatal error message with extra field")

// with error
err := someFunction()
if err != nil {
    log.WithError(err).Fatal(ctx, "this is a fatal error message with an error attached")
}

// with error and field
err := someFunction()
if err != nil {
    log.WithError(err).
        WithField("extraFieldName", "extraFieldValue").
        Fatal(ctx, "this is a fatal error message with an error attached and extra field")
}
Info Log

logs a message prefixed with Info:

// basic Info log
log.Info(ctx, "this is an info message")

// with an extra field
log.WithField("extraFieldName", "extraFieldValue").Info(ctx, "this is an info message with extra field")
Warning Log

logs a message prefixed with Warn:

// basic Warn log
log.Warn(ctx, "this is a warning message")

// with an extra field
log.WithField("extraFieldName", "extraFieldValue").Warn(ctx, "this is a warning message with extra field")

// with error
err := someFunction()
if err != nil {
    log.WithError(err).Warn(ctx, "this is a warning message with an error attached")
}

// with error and field
err := someFunction()
if err != nil {
    log.WithError(err).
        WithField("extraFieldName", "extraFieldValue").
        Warn(ctx, "this is a warning message with an error attached and extra field")
}

Advanced

There are a few advanced options you can use with your logger.

Create a logger instance with Slack as an output

Set any number of channels to send any log level to, then every time a log is called it will get sent to the corresponding channels in slack.

ctx, log := logger.New(
    ctx,
    "spanName",
    logger.WithSlack(
        os.Getenv("SLACK_BOT_TOKEN"),
        logger.Groups{
            logger.ERROR:   []string{"slackErrorChannelID"},
            logger.FATAL:   []string{"slackFatalChannelID"},
            logger.INFO:    []string{"slackInfoChannelID"},
            logger.WARNING: []string{"slackWarningChannelID"},
        },
    ),
)
Create a logger instance that sends requests to webhook endpoints

Set any number of endpoints to send your logs to, then every time a log is called it will make a request to the all the endpoints defined

ctx, log := logger.New(
    ctx,
    "spanName",
    logger.WithWebhook(
        "message", // the key used for the message in the JSON payload ie: {"message": "some log meessge here"}
        logger.Groups{
            logger.ERROR:   []string{"https://test.com/error"},
            logger.FATAL:   []string{"https://test.com/fatal"},
            logger.INFO:    []string{"https://test.com/info"},
            logger.WARNING: []string{"https://test.com/warning"},
        },
    ),
)
Request Payload

Request body that gets sent as a POST

{
  "message": "log message:\n{\"fieldName\": \"valueToLog\"}"
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Fields

type Fields map[LogKey]interface{}

Fields set of key value pairs that are added to the context as 1 field each

type Groups

type Groups map[LogType][]string

used to map log types to external channel sets

type LogKey

type LogKey string

important fields used for the logger

type LogOption

type LogOption func(context.Context) context.Context

LogOption option functions used to add new fields or options to the logger

func WithField

func WithField(key string, value interface{}) LogOption

WithField adds a new field to the logger

func WithFields

func WithFields(fields Fields) LogOption

WithFields adds multiple new fields to the logger

func WithSlack

func WithSlack(key string, groups Groups) LogOption

WithSlack logger option adds the Slack details to the logger

func WithWebhook

func WithWebhook(msgKey string, groups Groups) LogOption

WithWebhook logger option adds the webhook details to the logger

type LogType

type LogType int

Log type constants

const (
	INFO    LogType = 1
	WARNING LogType = 2
	ERROR   LogType = 3
	FATAL   LogType = 4
)

type Logger

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

Logger

func New

func New(ctx context.Context, span string, options ...LogOption) (context.Context, *Logger)

New creates a new instance of a logger

func (*Logger) Error

func (l *Logger) Error(ctx context.Context, msg string)

Error logs an error line along with any fields that have been added to the context or logger

func (*Logger) Fatal

func (l *Logger) Fatal(ctx context.Context, msg string)

Fatal logs a fatal line along with any fields that have been added to the context or logger

func (*Logger) Info

func (l *Logger) Info(ctx context.Context, msg string)

Info logs a basic info line along with any fields that have been added to the context or logger

func (*Logger) Warn

func (l *Logger) Warn(ctx context.Context, msg string)

Warn logs a warning line along with any fields that have been added to the context or logger

func (*Logger) WithError

func (l *Logger) WithError(err error) *Logger

WithError adds an error to be logged to the logger

func (*Logger) WithField

func (l *Logger) WithField(key string, value interface{}) *Logger

WithField adds a single field to the logger

func (*Logger) WithFields

func (l *Logger) WithFields(fields Fields) *Logger

WithFields adds multiple fields to the logger

Jump to

Keyboard shortcuts

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