slogsyslog

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2025 License: BSD-3-Clause Imports: 14 Imported by: 0

README

Structured log syslog

Go Reference

Go structured log syslog handler.

Requirements

  • Go >= 1.21

Usage

package main

import (
	"log"
	"log/slog"

	slogsyslog "github.com/mocheryl/slog-syslog"
)

func main() {
	h, err := slogsyslog.New(nil)
	if err != nil {
		log.Fatalf("Could not initialize syslog slog handler: %s\n", err)
		return
	}

	l := slog.New(h)
	slog.SetDefault(l)

	slog.Info("Hello, World!")
	// Output: Jun 29 08:44:16 localhost helloworld[1234]: Hello, World!

	h.Close()
}

Credits

Most of the code and ideas taken from the following projects:

Contributing

Any contributions are welcome - see the CONTRIBUTING.md file for details.

License

This project is licensed under the 3-clause BSD license - see the LICENSE file for details.

Documentation

Overview

Package slogsyslog implements log/slog.Handler for syslog server.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Facility

type Facility int

Facility is the log facility.

const (
	Kern Facility = iota << 3

	User

	Mail

	Daemon

	Auth

	Syslog

	LPR

	News

	UUCP

	Cron

	AuthPriv

	FTP

	Local0

	Local1

	Local2

	Local3

	Local4

	Local5

	Local6

	Local7
)

Log facilities.

func (Facility) String

func (f Facility) String() string

type Options

type Options struct {
	// AddSource, causes the handler to compute the source code position of the
	// log statement and add it as a prefix to the message.
	AddSource bool

	// Level is the level at which we log at.
	Level slog.Leveler

	// Network protocol to use when connecting to a syslog server.
	Network string

	// Address of the syslog server.
	Address string

	// DialTimeout is duration after which connecting to a syslog server
	// timeouts.
	DialTimeout time.Duration

	// WriteTimeout is duration after which writing to a syslog server timeouts.
	WriteTimeout time.Duration

	// Facility with which we are logging.
	Facility Facility

	// Tag with which we are logging.
	Tag string
}

Options sets the syslog logging options.

type SyslogHandler

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

SyslogHandler is a structured log log/slog.Handler implementation that writes messages to a syslog server.

func New

func New(opts *Options) (*SyslogHandler, error)

New creates a new syslog slog log/slog.Handler. By default it will log at log/slog.LevelInfo level to a UNIX datagram socket located at /dev/log.

func (*SyslogHandler) Close

func (s *SyslogHandler) Close() error

func (*SyslogHandler) Enabled

func (s *SyslogHandler) Enabled(ctx context.Context, level slog.Level) bool

func (*SyslogHandler) Handle

func (s *SyslogHandler) Handle(ctx context.Context, r slog.Record) error

func (*SyslogHandler) WithAttrs

func (s *SyslogHandler) WithAttrs(attrs []slog.Attr) slog.Handler

func (*SyslogHandler) WithGroup

func (s *SyslogHandler) WithGroup(name string) slog.Handler

Jump to

Keyboard shortcuts

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