slogpushover

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: May 4, 2024 License: MIT Imports: 5 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

slog: Pushover handler

tag Go Version GoDoc Go report License

A Pushover Handler for slog Go library. Inspired from samber' s slog repositories

🚀 Install

go get github.com/SkYNewZ/slog-pushover

Compatibility: go >= 1.21

💡 Usage

GoDoc: https://pkg.go.dev/github.com/skynewz/slog-pushover

Handler options
type Options struct {
	Level slog.Leveler // minimum level of messages to log (default: slog.LevelDebug)

	Token     string // Pushover application token
	Recipient string // Pushover user/group key

	Message   *pushover.Message // optional: customize Pushover message. 'Message' will be replaced by the log message
	Converter Converter         // optional: customize Pushover message builder

	// optional: see slog.HandlerOptions
	AddSource   bool
	ReplaceAttr func(groups []string, a slog.Attr) slog.Attr
}
Example
package main

func main() {
	handler := slogpushover.NewHandler(&slogpushover.Options{
		Level:       slog.LevelDebug,
		Token:       os.Getenv("PUSHOVER_TOKEN"),
		Recipient:   os.Getenv("PUSHOVER_RECIPIENT"),
		Message:     nil, // You can customize the message details
		Converter:   nil,
		AddSource:   true,
		ReplaceAttr: nil,
	})

	logger := slog.New(handler)
	logger = logger.With("release", "v1.0.0")

	logger.
		With(
			slog.Group("user",
				slog.String("id", "user-123"),
				slog.Time("created_at", time.Now().AddDate(0, 0, -1)),
			),
		).
		With("environment", "dev").
		With("error", fmt.Errorf("an error")).
		Error("A message")
}

👤 Contributors

Contributors

📝 License

Copyright © 2023 Quentin Lemaire.

This project is MIT licensed.

Documentation

Overview

Example
package main

import (
	"fmt"
	"log/slog"
	"os"
	"time"

	slogpushover "github.com/SkYNewZ/slog-pushover"
)

func main() {
	handler := slogpushover.NewHandler(&slogpushover.Options{
		Level:       slog.LevelDebug,
		Token:       os.Getenv("PUSHOVER_TOKEN"),
		Recipient:   os.Getenv("PUSHOVER_RECIPIENT"),
		Message:     nil,
		Converter:   nil,
		AddSource:   true,
		ReplaceAttr: nil,
	})

	logger := slog.New(handler)
	logger = logger.With("release", "v1.0.0")

	logger.
		With(
			slog.Group("user",
				slog.String("id", "user-123"),
				slog.Time("created_at", time.Now().AddDate(0, 0, -1)),
			),
		).
		With("environment", "dev").
		With("error", fmt.Errorf("an error")).
		Error("A message")

}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultConverter

func DefaultConverter(addSource bool, replaceAttr func(groups []string, a slog.Attr) slog.Attr, loggerAttr []slog.Attr, groups []string, record *slog.Record) string

DefaultConverter is the default Converter used by Handler.

func NewHandler

func NewHandler(opts *Options) slog.Handler

NewHandler creates a new slog.Handler that sends messages to Pushover.

Types

type Converter

type Converter func(addSource bool, replaceAttr func(groups []string, a slog.Attr) slog.Attr, loggerAttr []slog.Attr, groups []string, record *slog.Record) string

Converter is a function that converts a slog.Record to a string.

type Options

type Options struct {
	Level slog.Leveler // minimum level of messages to log (default: slog.LevelDebug)

	Token     string // Pushover application token
	Recipient string // Pushover user/group key

	Message   *pushover.Message // optional: customize Pushover message. 'Message' will be replaced by the log message
	Converter Converter         // optional: customize Pushover message builder

	// optional: see slog.HandlerOptions
	AddSource   bool
	ReplaceAttr func(groups []string, a slog.Attr) slog.Attr
}

Jump to

Keyboard shortcuts

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