Skip to content

WeiZhixiong/easy-logrus-formatter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Easy Logrus Formatter

Provide a user-friendly formatter for Logrus
Some inspiration taken from logrus-easy-formatter

Default output

When format options are not provided Formatter will output

2023-01-01T01:01:01+00:00 || INFO || Log message

Getting started

Getting easy-logrus-formatter

go get -u github.com/WeiZhixiong/easy-logrus-formatter

Sample Usage

package main

import (
	formatter "github.com/WeiZhixiong/easy-logrus-formatter"
	"github.com/sirupsen/logrus"
)

func main() {
	log := logrus.New()
	log.SetFormatter(&formatter.Formatter{})
	log.Info("Log message")
	log.WithFields(logrus.Fields{
		"animal": "walrus",
		"size":   10,
	}).Info("A group of walrus emerges from the ocean")
}

Above sample will produce:

2023-01-01T01:01:01+00:00 || INFO || Log message
2023-01-01T01:01:01+00:00 || INFO || A group of walrus emerges from the ocean||animal=walrus||size=10

custom Formatter

package main

import (
	formatter "github.com/WeiZhixiong/easy-logrus-formatter"
	"github.com/sirupsen/logrus"
	"time"
)

func main() {
	log := logrus.New()
	log.SetFormatter(&formatter.Formatter{
		Delimiter:  ",",
		TimeFormat: time.RFC3339Nano,
		LogFields:  []formatter.LogField{formatter.Level, formatter.LogTime, formatter.Msg},
	})
	log.Info("Log message")
}

Above sample will produce:

INFO,2023-01-01T01:01:01.77971+00:00,Log message

Enable ReportCaller

package main

import (
	formatter "github.com/WeiZhixiong/easy-logrus-formatter"
	"github.com/sirupsen/logrus"
)

func main() {
	log := logrus.New()
	log.SetReportCaller(true)
	log.SetFormatter(&formatter.Formatter{
		Delimiter: " || ",
		LogFields: []formatter.LogField{formatter.LogTime, formatter.Level, formatter.Caller, formatter.Msg},
	})
	log.Info("Log message")
	log.WithFields(logrus.Fields{
		"animal": "walrus",
		"size":   10,
	}).Info("A group of walrus emerges from the ocean")
}

Above sample will produce:

2023-01-01T01:01:01+00:00 || INFO || /Users/xxx/GoProject/test/test.go:15 || Log message
2023-01-01T01:01:01+00:00 || INFO || /Users/xxx/GoProject/test/test.go:19 || A group of walrus emerges from the ocean || animal=walrus || size=10

About

Provided a user-friendly formatter for Logrus

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages