syslog

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2023 License: MIT Imports: 4 Imported by: 0

README

Syslog Hooks for Logrus

Usage

import (
  "log/syslog"
  "github.com/zaikir/logrus"
  lSyslog "github.com/zaikir/logrus/hooks/syslog"
)

func main() {
  log       := logrus.New()
  hook, err := lSyslog.NewSyslogHook("udp", "localhost:514", syslog.LOG_INFO, "")

  if err == nil {
    log.Hooks.Add(hook)
  }
}

If you want to connect to local syslog (Ex. "/dev/log" or "/var/run/syslog" or "/var/run/log"). Just assign empty string to the first two parameters of NewSyslogHook. It should look like the following.

import (
  "log/syslog"
  "github.com/zaikir/logrus"
  lSyslog "github.com/zaikir/logrus/hooks/syslog"
)

func main() {
  log       := logrus.New()
  hook, err := lSyslog.NewSyslogHook("", "", syslog.LOG_INFO, "")

  if err == nil {
    log.Hooks.Add(hook)
  }
}
Different log levels for local and remote logging

By default NewSyslogHook() sends logs through the hook for all log levels. If you want to have different log levels between local logging and syslog logging (i.e. respect the priority argument passed to NewSyslogHook()), you need to implement the logrus_syslog.SyslogHook interface overriding Levels() to return only the log levels you're interested on.

The following example shows how to log at DEBUG level for local logging and WARN level for syslog logging:

package main

import (
	"log/syslog"

	log "github.com/zaikir/logrus"
	logrus_syslog "github.com/zaikir/logrus/hooks/syslog"
)

type customHook struct {
	*logrus_syslog.SyslogHook
}

func (h *customHook) Levels() []log.Level {
	return []log.Level{log.WarnLevel}
}

func main() {
	log.SetLevel(log.DebugLevel)

	hook, err := logrus_syslog.NewSyslogHook("tcp", "localhost:5140", syslog.LOG_WARNING, "myTag")
	if err != nil {
		panic(err)
	}

	log.AddHook(&customHook{hook})

	//...
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type SyslogHook

type SyslogHook struct {
	Writer        *syslog.Writer
	SyslogNetwork string
	SyslogRaddr   string
}

SyslogHook to send logs via syslog.

func NewSyslogHook

func NewSyslogHook(network, raddr string, priority syslog.Priority, tag string) (*SyslogHook, error)

Creates a hook to be added to an instance of logger. This is called with `hook, err := NewSyslogHook("udp", "localhost:514", syslog.LOG_DEBUG, "")` `if err == nil { log.Hooks.Add(hook) }`

func (*SyslogHook) Fire

func (hook *SyslogHook) Fire(entry *logrus.Entry) error

func (*SyslogHook) Levels

func (hook *SyslogHook) Levels() []logrus.Level

Jump to

Keyboard shortcuts

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