lfslog

package
v2.0.1+incompatible Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2018 License: MIT, MIT Imports: 9 Imported by: 0

README

Local Filesystem Hook for Logrus

GoDoc

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

Example

import (
	"github.com/jefurry/logrus/hooks/lfslog"
	"github.com/jefurry/logrus"
)

var Log *logrus.Logger

func NewLogger() *logrus.Logger {
	if Log != nil {
		return Log
	}

	pathMap := lfslog.PathMap{
		logrus.InfoLevel:  "/var/log/info.log",
		logrus.ErrorLevel: "/var/log/error.log",
	}

	Log = logrus.New()
	Log.Hooks.Add(lfslog.NewHook(
		pathMap,
		&logrus.JSONFormatter{},
	))
	return Log
}
Formatters

lfslog will strip colors from any TextFormatter type formatters when writing to local file, because the color codes don't look great in file.

If no formatter is provided via lfslog.NewHook, a default text formatter will be used.

Log rotation

In order to enable automatic log rotation it's possible to provide an io.Writer instead of the path string of a log file. In combination with packages like file-rotatelogs log rotation can easily be achieved.

package main

import (
	"github.com/jefurry/logrus/hooks/lfslog"
	"github.com/jefurry/logrus"
	rotatelog "github.com/jefurry/logrus/hooks/rotatelog"
)

var Log *logrus.Logger

func NewLogger() (*logrus.Logger, error) {
	if Log != nil {
		return Log
	}

	path := "/var/log/go.log"
	writer, err := rotatelog.New(
		path+".%Y%m%d%H%M",
		rotatelog.WithLinkName(path),
		rotatelog.WithMaxAge(time.Duration(86400)*time.Second),
		rotatelog.WithRotationTime(time.Duration(604800)*time.Second),
	)
	if err != nil {
		return nil, err
	}

	Log1 = logrus.New()
	Log1.Hooks.Add(lfslog.NewHook(
		lfslog.WriterMap{
			logrus.InfoLevel:  writer,
			logrus.ErrorLevel: writer,
		},
		&logrus.JSONFormatter{},
	))

	Log = logrus.New()
	Log.Hooks.Add(lfslog.NewHook(
		pathMap,
		&logrus.JSONFormatter{},
	))

	return Log, nil
}
Note:

User who run the go application must have read/write permissions to the selected log files. If the files do not exists yet, then user must have permission to the target directory.

Other

Thanks rifflock.

Documentation

Overview

Package lfshook is hook for sirupsen/logrus that used for writing the logs to local files.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LfsHook

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

LfsHook is a hook to handle writing to local log files.

func NewHook

func NewHook(output interface{}, formatter logrus.Formatter) (*LfsHook, error)

NewHook returns new LFS hook. Output can be a string, io.WriteCloser, WriterMap or PathMap. If using io.WriteCloser or WriterMap, user is responsible for closing the used io.WriteCloser.

func (*LfsHook) Close

func (hook *LfsHook) Close() error

func (*LfsHook) Fire

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

Fire writes the log file to defined path or using the defined writer. User who run this function needs write permissions to the file or directory if the file does not yet exist.

func (*LfsHook) Levels

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

Levels returns configured log levels.

func (*LfsHook) SetDefaultPath

func (hook *LfsHook) SetDefaultPath(defaultPath string)

SetDefaultPath sets default path for levels that don't have any defined output path.

func (*LfsHook) SetDefaultWriter

func (hook *LfsHook) SetDefaultWriter(defaultWriter io.WriteCloser)

SetDefaultWriter sets default writer for levels that don't have any defined writer.

func (*LfsHook) SetFormatter

func (hook *LfsHook) SetFormatter(formatter logrus.Formatter)

SetFormatter sets the format that will be used by hook. If using text formatter, this method will disable color output to make the log file more readable.

type PathMap

type PathMap map[logrus.Level]string

PathMap is map for mapping a log level to a file's path. Multiple levels may share a file, but multiple files may not be used for one level.

type WriterMap

type WriterMap map[logrus.Level]io.WriteCloser

WriterMap is map for mapping a log level to an io.WriteCloser. Multiple levels may share a writer, but multiple writers 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