caption_json_formatter

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2023 License: MIT Imports: 7 Imported by: 9

README

Go Reference

caption-json-formatter

logrus's 'message(object)' json formatter with human-readable caption added. default available caption is

  • timestamp
  • logLevel

Example

OutPut
  1. with custom caption (nollehLog) with caption

  2. set logger option with unpretty print (off pritty print) unpretty

  3. set logger without custom caption and set as pretty no caption

Available Options
key default description
PrettyPrint false if enabled, the message(object) is printed as pretty json
CustomCaption nil if has value, then the caption is attatched before message(object)
CustomCaptionPrettyPrint false if enabled, the passed custom caption is printed as pretty json
Colorize false is enabled, add pre defined term color code by loglevel
Source
  • initalization
// initialize log
var (
	logger = NewLogger()
)

func NewLogger() *logrus.Logger {
	log.SetFlags(log.LstdFlags | log.Lshortfile)
	log.SetOutput(os.Stdout)

	logger := logrus.New()
	logger.Level = logrus.TraceLevel
	logger.SetFormatter(&caption_json_formatter.Formatter{ PrettyPrint: true })
	return logger
}

func Log() *caption_json_formatter.Entry {
	return &caption_json_formatter.Entry{ Entry: logrus.NewEntry(logger) }
}
  • printLog
func main() {

    type Request struct {
            Url string `json:"url"`
            Method string `json:"method"`
        }
        type Response struct {
            User int `json:"user"`
            Balance int `json:"balance"`
        }
        type Message struct {
            Request Request `json:"request"`
            Response Response `json:"response"`
        }

        message := Message { Request{ "/user/123456/balance", "GET" }, Response{123456, 1000} }

        /* in current logrus implementation, there isn't way for set Custom Entry
         * hook or formatter, doesn't have opportunity for marshaling message.
         * so before pull request was made, you need to use custom function to use extended entry.
         */

        Log().Debug(message)
}

When To Use

it is useful when shows 'message' that include json to human. show as pretty format, eventhought there is nested json. (not strigified)

for example, for message

// accessData is json type that has request / reponse data
log.Debug(accessData)
[2020-01-20T16:46:08.7452971+09:00] [Debug]
{
    "request": {
        "headers": { "content-type": "application/json" },
        "method": "GET",
        "route": "/user/{userId}/balance",
        "url": "/user/123456/balance"
    },
    "response": {
        "body": {
            "userId": 123456,
            "balance": 1000
        }
    }
}

not like other formatters doing. (like below)

[2020-01-20T16:46:08.7452971+09:00] [Debug] {/"request/": { /"headers/": { /"content-type/": /"application/json/" }, /"method/": /"GET/", /"route/": /"/user/{userId}/balance/", /"url/": /"/user/123456/balance/" }, /"response/": { /"body/": { /"userId/": 123456, /"balance/": 1000 } } }

or

{ "time": "2020-01-20T16:46:08.7452971+09:00", "msg": {/"request/": { /"headers/": { /"content-type/": /"application/json/" }, /"method/": /"GET/", /"route/": /"/user/{userId}/balance/", /"url/": /"/user/123456/balance/" }, /"response/": { /"body/": { /"userId/": 123456, /"balance/": 1000 } }

or

"2020-01-20T16:46:08.7452971+09:00" {{/user/123456/balance GET} {123456 1000}}"

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsObject

func IsObject(v interface{}) bool

func Stringify

func Stringify(v interface{}) string

Types

type Entry

type Entry struct {
	*logrus.Entry
}

func (*Entry) Debug

func (entry *Entry) Debug(args ...interface{})

func (*Entry) Error

func (entry *Entry) Error(args ...interface{})

func (*Entry) Fatal

func (entry *Entry) Fatal(args ...interface{})

func (*Entry) Info

func (entry *Entry) Info(args ...interface{})

func (*Entry) Panic

func (entry *Entry) Panic(args ...interface{})

func (*Entry) Trace

func (entry *Entry) Trace(args ...interface{})

func (*Entry) Warn

func (entry *Entry) Warn(args ...interface{})

type Formatter

type Formatter struct {
	CustomCaptionPrettyPrint bool
	// custom caption can be struct, string, whatever
	CustomCaption interface{}
	PrettyPrint   bool
	Colorize      bool
}

func (*Formatter) Format

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

type RootFields

type RootFields struct {
	Timestamp string
	Func      string
	Level     logrus.Level
	Fields    interface{}
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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