ctxlog

package module
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2025 License: MIT Imports: 4 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Go Reference

ctxlog


echo middleware that print log for a api's request / response

if you write request / response like this,

example

then get ..

example

you need only one line to show this request/response log !

e.Use(middleware.CtxLogger())

more example...

set formatter as warning level ctxlog

warning_level_ctx_log

set formatter as CaptionJsonFormatter.Json()

json_log

configuration

you can also configuration your ctxlog. colorize, json format.. default ctxlog will be formatted as caption_json_formatter.Console(). see more here

you can change the default option, formatter, as you wish.

import (
  cjf "github.com/nolleh/caption_json_formatter"
  "github.com/nolleh/ctxlog"
  "github.com/nolleh/ctxlog/middleware"
)

func main() {
  // [optional] retreive ctxlogs formatter (CaptionJsonFormatter)
  formatter := ctxlog.CaptionJsonFormatter()
  // [optional] you can modify format configruation from default.
  formatter.PrettyPrint = true
  // [optional] or, you reset your own formatter
  formatter = cjf.Json()
  // [optional] and set 'ANY FORMATTER' compatiable with logrus.formatter
  ctxlog.Logger().SetFormatter(formatter)

  // Echo instance
  e := echo.New()
  // [required] use ctxlog middleware (if you want use middleware. or,
  // just use ctxlog and declare your own, as you wish)
  e.Use(middleware.CtxLogger())
  // print level can be modified.
  // e.Use(middleware.CtxLoggerWithLevel(logrus.WarnLevel))
}

Example Details


request
{
  "name": "nolleh",
  "say": "hello happy new year!"
}
response
{
  "message": "Hello, World!"
}
go

example/main.go

func main() {

    // Echo instance
    e := echo.New()

    /// this one line! (other codes are for start echo frameworks / http handler)
    e.Use(middleware.CtxLogger())

    e.POST("/", hello)
    e.Logger.Fatal(e.Start(":1323"))
}

// http Handler
func hello(c echo.Context) error {
	// type Request struct {
	// 	Name string `json:"name"`
	// 	Say string `json:"say"`
	// }
	// var request Request
	// c.Bind(&request)
	type Result struct {
		Message string `json:"message"`
	}

	// and if you want to add some additional log, use it as log stream!
	// experience colorized, and pretty json formatting
	// ctxlog.Log().Info(request)

	result := Result{"Hello, World!"}
	json, _ := json.Marshal(result)
	return c.String(http.StatusOK, string(json))
}


Output

colored by log level

// if you uncomment ctxlog.Log().Info(request), above
2020-10-08T16:40:46.4543276+09:00 [INFO] {
 "name": "nolleh",
 "say": "hello happy new year!"
}

//
2020-10-08T16:40:46.5171606+09:00 [TRACE] {
 "request": {
  "body": {
   "name": "nolleh",
   "say": "hello happy new year!"
  },
  "header": {
   "Accept": [
    "*/*"
   ],
   "Accept-Encoding": [
    "gzip, deflate, br"
   ],
   "Cache-Control": [
    "no-cache"
   ],
   "Connection": [
    "keep-alive"
   ],
   "Content-Length": [
    "54"
   ],
   "Content-Type": [
    "application/json"
   ],
   "Postman-Token": [
    "b1556c81-bf31-4e6a-8c2a-e1bd0787ebdd"
   ],
   "User-Agent": [
    "PostmanRuntime/7.22.0"
   ]
  },
  "method": "POST",
  "uri": "/"
 },
 "requestId": "",
 "response": {
  "body": {
   "message": "Hello, World!"
  },
  "status": 200
 }
}

Dependency

  • echo framework (for middlware implementation)
  • caption_json_formatter

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CaptionJsonFormatter added in v0.2.1

func CaptionJsonFormatter() *formatter.Formatter

func Log

func Log() *formatter.Entry

Log makes extended logrus entry. @example ctxlog.Log().Debug(Message) where Message is struct or string, whatever

func Logger added in v0.2.1

func Logger() *logrus.Logger

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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