caption_json_formatter

package module
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2025 License: MIT Imports: 7 Imported by: 9

README

Go Reference

caption-json-formatter

Feature

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

  • timestamp
  • logLevel

and also you can use this formatter for using simple json formatter (@since v0.2.0).

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

  4. use caption_json_formatter as simple json logger, to handled by other program. (like kibana, loki, whatever) json with default caption it printed as

    {
      "level": "info",
      "message": {
        "request": { "method": "GET", "url": "/user/123456/balance" },
        "response": { "balance": 1000, "user": 123456 }
      },
      "time_stamp": "2023-03-05T18:16:47.165173+09:00"
    }
    
Available Options
key ConsoleDefault JsonDefault description
TransportToJson false true if enabled, the whole log (include default caption) will be marshaled to json
UseDefaultCaption true true if enabled, timestamp and loglevel will be added
PrettyPrint true false if enabled, the message(object) is printed as pretty json
CustomCaption nil nil if has value, then the caption is attatched before message(object)
CustomCaptionPrettyPrint false false if enabled, the passed custom caption is printed as pretty json
Colorize true false if enabled, add predefined 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
  // option 1. helper function Console makes default formatter for console
  logger.SetFormatter(caption_json_formatter.Console())

  // if you want, use default constructor and modify some option.
  // console := caption_json_formatter.Console()
  // console.CustomCaptionPrettyPrint = true
  // logger.SetFormatter(console)

  // use JsonFormatter
  // json := caption_json_formatter.Json()
  // logger.SetFormatter(json)

  // option 2. if you no need helper constructing, you can do yourself. (not recommended)
  // 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 ...any)

func (*Entry) Error

func (entry *Entry) Error(args ...any)

func (*Entry) Fatal

func (entry *Entry) Fatal(args ...any)

func (*Entry) Info

func (entry *Entry) Info(args ...any)

func (*Entry) Panic

func (entry *Entry) Panic(args ...any)

func (*Entry) Trace

func (entry *Entry) Trace(args ...any)

func (*Entry) Warn

func (entry *Entry) Warn(args ...any)

type Formatter

type Formatter struct {
	/** if enabled, the whole log will be marshaled to json (including captions)
	  work as json Formatter. */
	TransportToJson bool
	/** if enabled, log prefixed with 'timestamp, level' */
	UseDefaultCaption bool
	/** if enabled, CustomCaption will be marshaled to json */
	CustomCaptionPrettyPrint bool
	/** if has value, it attached right before message(object). custom caption can be struct, string, whatever */
	CustomCaption any
	/** do PrettyPrint for message(object) */
	PrettyPrint bool
	/** if enabled, the message(object) will be colorized by predefined color code, along with logLevel */
	Colorize bool
}

func Console added in v0.2.0

func Console() *Formatter

* syntatic sugar for using formatter with predefined option (for console)

func Json added in v0.2.0

func Json() *Formatter

* syntatic sugar for using formatter with predefined option (for server, json)

func (*Formatter) Format

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

type JO added in v0.2.0

type JO map[string]any

type RootFields

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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