Skip to content

zput/zxcTool

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Powerful-logrus-formatter(input error line and save log to files)

Human-readable log formatter:

Screenshot

Charactor

  1. print the name and line about file and the lastest function name while this log
  2. save log to file

Configuration:

type ZtFormatter struct{
  nested.Formatter
    // CallerPrettyfier can be set by the user to modify the content
    // of the function and file keys in the json data when ReportCaller is
    // activated. If any of the returned value is the empty string the
    // corresponding key will be removed from json fields.
  CallerPrettyfier CallBackFoo

  FormaterOperator FormaterOperatorInterface
}

Example

package main

import (
	"fmt"
	nested "github.com/antonfisher/nested-logrus-formatter"
	"github.com/sirupsen/logrus"
	"github.com/zput/zxcTool/ztLog/zt_formatter"
	"path"
	"runtime"
)

func main() {
	var exampleFormatter = &zt_formatter.ZtFormatter{
		CallerPrettyfier: func(f *runtime.Frame) (string, string) {
			filename := path.Base(f.File)
			return fmt.Sprintf("%s()", f.Function), fmt.Sprintf("%s:%d", filename, f.Line)
		},
		Formatter: nested.Formatter{
			//HideKeys: true,
			FieldsOrder: []string{"component", "category"},
		},
	}
	printDemo(exampleFormatter, "hello world")
}

func printDemo(f logrus.Formatter, title string) {
	l := logrus.New()

	l.SetLevel(logrus.DebugLevel)
	l.SetReportCaller(true)

	if f != nil {
		l.SetFormatter(f)
	}

	l.Infof("this is %v demo", title)

	lWebServer := l.WithField("component", "web-server")
	lWebServer.Info("starting...")

	lWebServerReq := lWebServer.WithFields(logrus.Fields{
		"req":   "GET /api/stats",
		"reqId": "#1",
	})

	lWebServerReq.Info("params: startYear=2048")
	lWebServerReq.Error("response: 400 Bad Request")

	lDbConnector := l.WithField("category", "db-connector")
	lDbConnector.Info("connecting to db on 10.10.10.13...")
	lDbConnector.Warn("connection took 10s")

	l.Info("demo end.")
}

use zxclog

	err = ztUtil.CodedNoPtrErrorf(errno.EN_InnerServer, errno.EN_InnerServer, "%v", err)
	logs.Error(err)
	return



	if err != nil {
		logs.Error("commonInfo:%s; PostSaleCenterUserActivity; error:%s", commonInfo, err.Error())
		if errReal, ok := err.(*ztUtil.Error); ok {
			util.OnError(this.Ctx, *errReal.Code, errReal.Message)
			return
		} else {
			util.OnError(this.Ctx, errno.EN_InnerServer, errno.EM_InnerServer)
			return
		}
	}

more example please reference: /ztLog/example

Reference

thanks nested-logrus-formatter

Welcome PR

About

some basic tool to construct web server

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages