Giter Club home page Giter Club logo

zxctool's Introduction

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

zxctool's People

Contributors

zput avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

zxctool's Issues

Remove Beego please

It looks a bit odd if a simple small tool includes a complete web framework. :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.