logrus_formatter

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2019 License: MIT Imports: 6 Imported by: 1

README

logrus-formatter

text formatter for logrus

Logrus

Installation

To install formatter, use go get:

$ go get github.com/0x00b/logrus-formatter

Usage

Here is how it should be used:

package main

import (
	logf "github.com/0x00b/logrus-formatter"
	"github.com/sirupsen/logrus"
	"strings"
)

func TestFunctionNameLoooong(log *logrus.Entry) {
	log.Infoln("just test long function name")
}
func main() {
	log := logrus.NewEntry(logrus.New())

	formatter := &logf.TextFormatter{}
	log.Logger.SetFormatter(formatter)

	log.Printf("format test")    //[2019-03-01T19:39:22+08:00] INFO "format test"
	TestFunctionNameLoooong(log) //[2019-03-01T19:39:22+08:00] INFO "just test long function name"

	//set logrus Data
	log.Data["name"] = "ice"
	log.Data["age"] = 18

	//set format as : [time] level file:func:line msg
	formatter.SetFormat(logf.TagBL, logf.FieldKeyTime, logf.TagBR, logf.FieldKeyLevel, logf.FieldKeyFile, logf.TagColon, logf.FieldKeyFunc, logf.TagColon, logf.FieldKeyLine, logf.FieldKeyMsg)
	// logf.FieldKeyFile/logf.FieldKeyFunc/logf.FieldKeyLine , must call log.Logger.SetReportCaller(true)
	log.Logger.SetReportCaller(true)
	//set timestamp format
	formatter.TimestampFormat = "2006-01-02 15:04:05"

	log.Printf("format test")    //[2019-03-01 19:40:48] INFO .ter/example/main.go:                main.main:37   "format test" (name:"ice" age:18)
	TestFunctionNameLoooong(log) //[2019-03-01 19:40:48] INFO .ter/example/main.go:..TestFunctionNameLoooong:18   "just test long function name" (name:"ice" age:18)

	//自定义函数名的格式。也可以自定义文件名
	formatter.FormatFuncName = func(name string) string {
		funcLen := 10
		l := len(name)
		if l > funcLen {
			return name[l-funcLen : l]
		}
		return strings.Repeat(" ", funcLen-l) + name
	}

	log.Printf("format test") //[2019-03-01 19:43:11] INFO .ter/example/main.go: main.main:41   "format test" (name:"ice" age:18)

	formatter.SetFormat(logf.FieldKeyTime, logf.FieldKeyLevel, logf.FieldKeyFile, logf.TagColon, logf.FieldKeyLine, logf.FieldKeyFunc, logf.FieldKeyMsg)
	log.Printf("format test") //2019-03-18 12:02:38 INFO .ter/example/main.go:48    main.main "format test" (name="ice" age=18)

	formatter.SetFormatAndTagSource(logf.TagBL, logf.FieldKeyTime, logf.TagBR, logf.FieldKeyLevel, logf.FieldKeyMsg)
	log.Printf("format test") //[2019-03-18 15:57:40] INFO "format test" (source=.ter/example/main.go: main.main:51 name="ice" age=18)

	formatter.TagSource = false
	formatter.NoQuoteFields = true
	formatter.RegisterFields("RequestID")
	formatter.SetFormat(logf.FieldKeyTime, logf.TagVBar, logf.FieldKeyLevel, logf.TagVBar, logf.FieldKeyMsg, logf.TagVBar, "RequestID")
	log.Data["RequestID"] = "requestID1234"
	log.Printf("format test") //2019-04-28 20:13:09|INFO|format test|requestID1234 (name="ice" age=18 )

}

API

logf.TextFormatter exposes the following fields and methods.

Fields
  • TimestampFormat string — timestamp format to use for display when a full timestamp is printed.
  • QuoteEmptyFields bool — wrap empty fields in quotes if true.
  • FormatFuncName HandlerFormatFile — custom function name.
  • FormatFileName HandlerFormatFunc — custom file name.
Methods
SetFormat(args ...string) (format string)

Sets an alternative formatting string for output. use the following definition:

FieldKeyMsg            = logrus.FieldKeyMsg		//"msg"
FieldKeyLevel          = logrus.FieldKeyLevel		//"level"
FieldKeyTime           = logrus.FieldKeyTime		//"time"
FieldKeyFunc           = logrus.FieldKeyFunc		//"func"
FieldKeyFile           = logrus.FieldKeyFile		//"file"
FieldKeyLine           = "line"	
TagBL                  = "["
TagBR                  = "]"
TaGColon               = ":"

You can use the above fields to combine the format you want.
eg: 
[2019-03-01 19:51:10] INFO "format test"
SetFormat(TagBL, FieldKeyTime, TagBR, FieldKeyLevel, FieldKeyMsg)

2019-03-05 11:48:08 INFO .ter/example/main.go  main.main 48   "format test"
SetFormat(FieldKeyTime, FieldKeyLevel, FieldKeyFile, FieldKeyFunc, FieldKeyLine, FieldKeyMsg)

2019-03-05 11:49:47 INFO .ter/example/main.go:48    main.main "format test"
SetFormat(FieldKeyTime, FieldKeyLevel, FieldKeyFile, TaGColon, FieldKeyLine, FieldKeyFunc, FieldKeyMsg)

It's necessary to call log.Logger.SetReportCaller(true) if you use:
* FieldKeyFunc
* FieldKeyFile
* FieldKeyLine

License

MIT

Documentation

Index

Constants

View Source
const (
	FieldKeyMsg         = logrus.FieldKeyMsg
	FieldKeyLevel       = logrus.FieldKeyLevel
	FieldKeyTime        = logrus.FieldKeyTime
	FieldKeyLogrusError = logrus.FieldKeyLogrusError
	FieldKeyFunc        = logrus.FieldKeyFunc
	FieldKeyFile        = logrus.FieldKeyFile
	FieldKeyLine        = "line"
	TagBL               = "["
	TagBR               = "]"
	TagColon            = ":"
	TagVBar             = "|"
)

Variables

View Source
var (
	FunctionNameLength = 25
	FileNameLength     = 20
)

Functions

This section is empty.

Types

type HandlerFormatFile

type HandlerFormatFile func(fileName string) string

HandlerFormatFile format file name

type HandlerFormatFunc

type HandlerFormatFunc func(funcName string) string

HandlerFormatFunc format function name

type Level

type Level logrus.Level

func (Level) MarshalText

func (level Level) MarshalText() ([]byte, error)

func (Level) String

func (level Level) String() string

Convert the Level to a string. E.g. PanicLevel becomes "panic".

type TextFormatter

type TextFormatter struct {
	// TimestampFormat to use for display when a full timestamp is printed
	TimestampFormat string

	// QuoteEmptyFields will wrap empty fields in quotes if true
	QuoteEmptyFields bool
	NoQuoteFields    bool

	FormatFuncName HandlerFormatFunc
	FormatFileName HandlerFormatFile

	TagSource bool

	FieldKeys []string
	// contains filtered or unexported fields
}

TextFormatter formats logs into text

func (*TextFormatter) Format

func (f *TextFormatter) Format(entry *logrus.Entry) ([]byte, error)

func (*TextFormatter) RegisterFields added in v0.2.1

func (f *TextFormatter) RegisterFields(args ...string)

func (*TextFormatter) SetFormat

func (f *TextFormatter) SetFormat(args ...string)

func (*TextFormatter) SetFormatAndTagSource added in v0.1.4

func (f *TextFormatter) SetFormatAndTagSource(args ...string)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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