lfshook

package module
v0.0.0-...-3d834ab Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2018 License: MIT Imports: 5 Imported by: 1

README

Local Filesystem Hook for Logrus

Sometimes developers like to write directly to a file on the filesystem. This is a hook for logrus designed to allow users to do just that. The log levels are dynamic at instanciation of the hook, so it is capable of logging at some or all levels.

Example

import (
	log "github.com/Sirupsen/logrus"
	"github.com/rifflock/lfshook"
)

var Log *log.Logger

func NewLogger( config map[string]interface{} ) *log.Logger {
	if Log != nil {
		return Log
	}

	Log = log.New()
	Log.Formatter = new(log.JSONFormatter)
	Log.Hooks.Add(lfshook.NewHook(lfshook.PathMap{
		log.InfoLevel : "/var/log/info.log",
		log.ErrorLevel : "/var/log/error.log",
	}))
	return Log
}
Specifying a different formatter

By default lfshook uses TextFormatter with colors disabled. If you wish to specify a different logger you can do so with the SetFormatter() method.

hook := lfshook.NewHook(lfshook.PathMap{
	logger.InfoLevel : filename,
	logger.ErrorLevel : filename,
})
hook.SetFormatter(&logger.JSONFormatter{})
Log.AddHook(hook)
Note:

Whichever user is running the go application must have read/write permissions to the log files selected, or if the files do not yet exists, then to the directory in which the files will be created.

Documentation

Overview

Package LFShook allows users to write to the logfiles using logrus.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewHook

func NewHook(levelMap PathMap) *lfsHook

Given a map with keys equal to log levels. We can generate our levels handled on the fly, and write to a specific file for each level. We can also write to the same file for all levels. They just need to be specified.

Types

type PathMap

type PathMap map[logrus.Level]string

Map for linking a log level to a log file Multiple levels may share a file, but multiple files may not be used for one level

Jump to

Keyboard shortcuts

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