lfshook

package module
v0.0.0-...-0224f69 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2020 License: MIT Imports: 6 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.

[文件大小/覆盖等] (https://github.com/natefinch/lumberjack)

Example

import (
	"github.com/rifflock/lfshook"
	"github.com/sirupsen/logrus"
)

var Log *logrus.Logger

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

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

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

lfshook 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 lfshook.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 (
	rotatelogs "github.com/lestrrat-go/file-rotatelogs"
	"github.com/rifflock/lfshook"
	"github.com/sirupsen/logrus"
)

var Log *logrus.Logger

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

	path := "/var/log/go.log"
	writer := rotatelogs.New(
		path+".%Y%m%d%H%M",
		rotatelogs.WithLinkName(path),
		rotatelogs.WithMaxAge(time.Duration(86400)*time.Second),
		rotatelogs.WithRotationTime(time.Duration(604800)*time.Second),
	)

	logrus.Hooks.Add(lfshook.NewHook(
		lfshook.WriterMap{
			logrus.InfoLevel:  writer,
			logrus.ErrorLevel: writer,
		},
		&logrus.JSONFormatter,
	))

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

	return Log
}
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.

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

func NewFileLogger

func NewFileLogger(infoPath, errorPath string, size int) *logrus.Logger

NewFileLogger 初始化 logger

Types

type LfsHook

type LfsHook struct {
	Config RotateFileConfig
	// 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

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

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.Writer)

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 RotateFileConfig

type RotateFileConfig struct {
	Filename   string
	MaxSize    int
	MaxBackups int
	MaxAge     int
	Level      logrus.Level
	Formatter  logrus.Formatter
}

type WriterMap

type WriterMap map[logrus.Level]io.Writer

WriterMap is map for mapping a log level to an io.Writer. Multiple levels may share a writer, but multiple writers may not be used for one level.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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