Latest Threat Research:SANDWORM_MODE: Shai-Hulud-Style npm Worm Hijacks CI Workflows and Poisons AI Toolchains.Details
Socket
Book a DemoInstallSign in
Socket

web.lueluesay.top/git/chenyi/logrus

Package Overview
Dependencies
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

web.lueluesay.top/git/chenyi/logrus

Go Modules
Version
v0.0.0-20230908035640-e246c312e78a
Version published
Created
Source

Syslog Hooks for Logrus :walrus:

Usage

import (
  "log/syslog"
  "logrus"
  lSyslog "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"
  "logrus"
  lSyslog "github.com/sirupsen/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/sirupsen/logrus"
	logrus_syslog "github.com/sirupsen/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})

	//...
}

FAQs

Package last updated on 08 Sep 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts