writer

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2023 License: MIT Imports: 2 Imported by: 0

README

Writer Hooks for Logrus

Send logs of given levels to any object with io.Writer interface.

Usage

If you want for example send high level logs to Stderr and logs of normal execution to Stdout, you could do it like this:

package main

import (
	"io/ioutil"
	"os"

	log "github.com/autosegment/logrus"
	"github.com/autosegment/logrus/hooks/writer"
)

func main() {
	log.SetOutput(ioutil.Discard) // Send all logs to nowhere by default

	log.AddHook(&writer.Hook{ // Send logs with level higher than warning to stderr
		Writer: os.Stderr,
		LogLevels: []log.Level{
			log.PanicLevel,
			log.FatalLevel,
			log.ErrorLevel,
			log.WarnLevel,
		},
	})
	log.AddHook(&writer.Hook{ // Send info and debug logs to stdout
		Writer: os.Stdout,
		LogLevels: []log.Level{
			log.InfoLevel,
			log.DebugLevel,
		},
	})
	log.Info("This will go to stdout")
	log.Warn("This will go to stderr")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Hook

type Hook struct {
	Writer    io.Writer
	LogLevels []log.Level
}

Hook is a hook that writes logs of specified LogLevels to specified Writer

func (*Hook) Fire

func (hook *Hook) Fire(entry *log.Entry) error

Fire will be called when some logging function is called with current hook It will format log entry to string and write it to appropriate writer

func (*Hook) Levels

func (hook *Hook) Levels() []log.Level

Levels define on which log levels this hook would trigger

Jump to

Keyboard shortcuts

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