https://github.com/nolleh/ctxlog
golang context logger (access logger, which print request/response per http request), and also provide middleware for echo framework (https://github.com/labstack/echo)
https://github.com/nolleh/ctxlog
echo echo-framework go golang http json log logrus middleware msa
Last synced: 9 months ago
JSON representation
golang context logger (access logger, which print request/response per http request), and also provide middleware for echo framework (https://github.com/labstack/echo)
- Host: GitHub
- URL: https://github.com/nolleh/ctxlog
- Owner: nolleh
- License: mit
- Created: 2020-01-21T15:01:15.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2025-03-09T11:00:47.000Z (11 months ago)
- Last Synced: 2025-04-28T15:14:49.837Z (9 months ago)
- Topics: echo, echo-framework, go, golang, http, json, log, logrus, middleware, msa
- Language: Go
- Homepage:
- Size: 255 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: ReadMe.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://pkg.go.dev/github.com/nolleh/ctxlog)
[](https://opensource.org/licenses/MIT)
## ctxlog
---
echo middleware that print log for a api's request / response
if you write request / response like this,

then get ..

> you need only one line to show this request/response log !
`e.Use(middleware.CtxLogger())`
#### more example...
set formatter as warning level ctxlog

set formatter as [CaptionJsonFormatter.Json()](https://github.com/nolleh/caption_json_formatter)

---
### configuration
you can also configuration your ctxlog.
colorize, json format..
default ctxlog will be formatted as caption_json_formatter.Console(). see more [here](https://github.com/nolleh/caption_json_formatter)
you can change the default option, formatter, as you wish.
```go
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
```json
{
"name": "nolleh",
"say": "hello happy new year!"
}
```
#### response
```json
{
"message": "Hello, World!"
}
```
#### go
[example/main.go](https://github.com/nolleh/ctxlog/blob/master/example/main.go)
```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
```go
// 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) [](http://github.com/labstack/echo)
- caption_json_formatter [](http://github.com/nolleh/caption_json_formatter)