-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit df50ac9
Showing
12 changed files
with
545 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Binaries for programs and plugins | ||
*.exe | ||
*.exe~ | ||
*.dll | ||
*.so | ||
*.dylib | ||
|
||
# Test binary, built with `go test -c` | ||
*.test | ||
|
||
# Output of the go coverage tool, specifically when used with LiteIDE | ||
*.out | ||
|
||
# Dependency directories (remove the comment below to include it) | ||
# vendor/ | ||
|
||
/vendor/ | ||
/Godeps/ | ||
|
||
# Directory Generated by air | ||
tmp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Changelog | ||
|
||
## [v1.0.0] - `2024-09-13` | ||
|
||
- Updated dependencies version | ||
- Transfer project to `owlsome-official` | ||
|
||
## v0 is Legacy version | ||
|
||
[v1.0.0]: https://github.com/owlsome-official/zlogres/releases/tag/v1.0.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
# zlogres | ||
|
||
[![made-with-Go](https://img.shields.io/badge/Made%20with-Go-1f425f.svg)](http://golang.org) | ||
[![Watsize-Library](https://img.shields.io/badge/Watsize-Library-289548)](https://github.com/owlsome-official) | ||
[![CodeQL](https://github.com/owlsome-official/zlogres/actions/workflows/codeql.yml/badge.svg)](https://github.com/owlsome-official/zlogres/actions/workflows/codeql.yml) | ||
|
||
zlogres is a middleware for GoFiber that logging about api elapsed time since request to response. | ||
|
||
## Table of Contents | ||
|
||
- [zlogres](#zlogres) | ||
- [Table of Contents](#table-of-contents) | ||
- [Run this on first time only](#run-this-on-first-time-only) | ||
- [Installation](#installation) | ||
- [Signatures](#signatures) | ||
- [Examples](#examples) | ||
- [Config](#config) | ||
- [Default Config](#default-config) | ||
- [Dependencies](#dependencies) | ||
- [Example Usage](#example-usage) | ||
|
||
## Installation | ||
|
||
```bash | ||
go get -u github.com/owlsome-official/zlogres | ||
``` | ||
|
||
## Signatures | ||
|
||
```go | ||
func New(config ...Config) fiber.Handler | ||
``` | ||
|
||
## Examples | ||
|
||
Import the middleware package that is part of the Fiber web framework | ||
|
||
```go | ||
import ( | ||
"github.com/gofiber/fiber/v2" | ||
"github.com/owlsome-official/zlogres" | ||
) | ||
``` | ||
|
||
After you initiate your Fiber app, you can use the following possibilities: | ||
|
||
```go | ||
// Default | ||
app.Use(zlogres.New()) | ||
|
||
// this middleware supported the `requestid` middleware | ||
app.Use(requestid.New()) | ||
app.Use(zlogres.New()) | ||
|
||
// Or extend your config for customization | ||
app.Use(requestid.New(requestid.Config{ | ||
ContextKey: "transaction-id", | ||
})) | ||
app.Use(zlogres.New(zlogres.Config{ | ||
RequestIDContextKey: "transaction-id", | ||
})) | ||
``` | ||
|
||
## Config | ||
|
||
```go | ||
// Config defines the config for middleware. | ||
type Config struct { | ||
// Optional. Default: nil | ||
Next func(c *fiber.Ctx) bool | ||
|
||
// Optional. Default: "requestid" | ||
RequestIDContextKey string | ||
} | ||
``` | ||
|
||
## Default Config | ||
|
||
```go | ||
var ConfigDefault = Config{ | ||
Next: nil, | ||
RequestIDContextKey: "requestid", | ||
} | ||
``` | ||
|
||
## Dependencies | ||
|
||
- [Zerolog](https://github.com/rs/zerolog) | ||
- [Fiber](https://github.com/gofiber/fiber) | ||
|
||
## Example Usage | ||
|
||
Please go to [example/main.go](./example/main.go) | ||
|
||
**Don't forget to run:** | ||
|
||
```bash | ||
go mod tidy | ||
``` | ||
|
||
Note: Custom usage please focus on `Custom` section |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package zlogres | ||
|
||
import ( | ||
"github.com/gofiber/fiber/v2" | ||
) | ||
|
||
type Config struct { | ||
// Optional. Default: nil | ||
Next func(c *fiber.Ctx) bool | ||
|
||
// Optional. Default: "requestid" | ||
RequestIDContextKey string | ||
|
||
// Optional. Default: "info" | ||
LogLevel string | ||
|
||
// Optional. Default: "micro". Possible Value: ["nano", "micro", "milli"] | ||
ElapsedTimeUnit string | ||
|
||
// Optional. Default: "message". Use ContextMessageKey by set `c.Locals("message", "WHATEVER_MESSAGE")` | ||
ContextMessageKey string | ||
} | ||
|
||
var ConfigDefault = Config{ | ||
Next: nil, | ||
RequestIDContextKey: "requestid", | ||
LogLevel: "info", | ||
ElapsedTimeUnit: "micro", | ||
ContextMessageKey: "message", | ||
} | ||
|
||
func configDefault(config ...Config) Config { | ||
// Return default config if nothing provided | ||
if len(config) < 1 { | ||
return ConfigDefault | ||
} | ||
|
||
// Override default config | ||
cfg := config[0] | ||
|
||
// set default | ||
if cfg.RequestIDContextKey == "" { | ||
cfg.RequestIDContextKey = ConfigDefault.RequestIDContextKey | ||
} | ||
|
||
if cfg.LogLevel == "" { | ||
cfg.LogLevel = ConfigDefault.LogLevel | ||
} | ||
|
||
if cfg.ElapsedTimeUnit == "" { | ||
cfg.ElapsedTimeUnit = ConfigDefault.ElapsedTimeUnit | ||
} | ||
|
||
if cfg.ContextMessageKey == "" { | ||
cfg.ContextMessageKey = ConfigDefault.ContextMessageKey | ||
} | ||
|
||
return cfg | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Example Usage | ||
|
||
## Step 1: Initialize | ||
|
||
```bash | ||
go mod tidy | ||
``` | ||
|
||
## Step 2: Go run `main.go` | ||
|
||
```bash | ||
go run main.go | ||
``` | ||
|
||
## Step 3: Try to call: GET `/` API | ||
|
||
```bash | ||
curl --request GET --url http://localhost:8000/ | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Try to send a request | ||
// Curl: `curl --request GET --url http://localhost:8000/` | ||
// or with REST Client VSCode Extension | ||
// Note: this lib can be receive a message via Context (see more in code) | ||
|
||
### Default | ||
GET http://localhost:8000/ | ||
|
||
### With Context | ||
GET http://localhost:8000/msg/mynameiswat | ||
|
||
### With Context (empty msg, will be not showing in logs) | ||
GET http://localhost:8000/msg | ||
|
||
### CUSTOM | ||
GET http://localhost:8001/msg_custom |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module example | ||
|
||
go 1.22.1 | ||
|
||
require ( | ||
github.com/gofiber/fiber/v2 v2.52.5 | ||
github.com/owlsome-official/zlogres v0.2.4 | ||
) | ||
|
||
require ( | ||
github.com/andybalholm/brotli v1.1.0 // indirect | ||
github.com/google/uuid v1.6.0 // indirect | ||
github.com/klauspost/compress v1.17.9 // indirect | ||
github.com/mattn/go-colorable v0.1.13 // indirect | ||
github.com/mattn/go-isatty v0.0.20 // indirect | ||
github.com/mattn/go-runewidth v0.0.16 // indirect | ||
github.com/rivo/uniseg v0.4.7 // indirect | ||
github.com/rs/zerolog v1.33.0 // indirect | ||
github.com/valyala/bytebufferpool v1.0.0 // indirect | ||
github.com/valyala/fasthttp v1.55.0 // indirect | ||
github.com/valyala/tcplisten v1.0.0 // indirect | ||
golang.org/x/sys v0.25.0 // indirect | ||
) | ||
|
||
replace github.com/owlsome-official/zlogres => ../ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M= | ||
github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY= | ||
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= | ||
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= | ||
github.com/gofiber/fiber/v2 v2.52.5 h1:tWoP1MJQjGEe4GB5TUGOi7P2E0ZMMRx5ZTG4rT+yGMo= | ||
github.com/gofiber/fiber/v2 v2.52.5/go.mod h1:KEOE+cXMhXG0zHc9d8+E38hoX+ZN7bhOtgeF2oT6jrQ= | ||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= | ||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= | ||
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA= | ||
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw= | ||
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= | ||
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= | ||
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= | ||
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= | ||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= | ||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= | ||
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc= | ||
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= | ||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= | ||
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= | ||
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= | ||
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= | ||
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= | ||
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8= | ||
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= | ||
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= | ||
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= | ||
github.com/valyala/fasthttp v1.55.0 h1:Zkefzgt6a7+bVKHnu/YaYSOPfNYNisSVBo/unVCf8k8= | ||
github.com/valyala/fasthttp v1.55.0/go.mod h1:NkY9JtkrpPKmgwV3HTaS2HWaJss9RSIsRVfcxxoHiOM= | ||
github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8= | ||
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc= | ||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34= | ||
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
|
||
"github.com/gofiber/fiber/v2" | ||
"github.com/gofiber/fiber/v2/middleware/requestid" | ||
"github.com/owlsome-official/zlogres" | ||
) | ||
|
||
var ( | ||
MY_REQUEST_ID_KEY string = "myrequestlonglongid" | ||
MY_CONTEXT_MESSAGE_KEY string = "msg" | ||
) | ||
|
||
func main() { | ||
|
||
// Default | ||
app := fiber.New(fiber.Config{DisableStartupMessage: true}) | ||
app.Use(zlogres.New()) | ||
|
||
app.Get("/", HandlerDefault) // GET http://localhost:8000/ | ||
app.Get("/msg/*", HandlerMsgParam) // GET http://localhost:8000/msg/{MESSAGE} | ||
|
||
fmt.Println("Listening on http://localhost:8000") | ||
fmt.Println("Try to send a request :D") | ||
go app.Listen(":8000") | ||
|
||
fmt.Println("// ----------------------------------------------- //") | ||
|
||
// Custom | ||
customApp := fiber.New(fiber.Config{DisableStartupMessage: true}) | ||
customApp.Use(requestid.New(requestid.Config{ContextKey: MY_REQUEST_ID_KEY})) | ||
customApp.Use(zlogres.New(zlogres.Config{ | ||
RequestIDContextKey: MY_REQUEST_ID_KEY, | ||
LogLevel: "debug", | ||
ElapsedTimeUnit: "nano", | ||
ContextMessageKey: MY_CONTEXT_MESSAGE_KEY, | ||
})) | ||
|
||
customApp.Get("/msg_custom", HandlerCustomMsgParam) // GET http://localhost:8000/msg_custom | ||
|
||
fmt.Println("[CUSTOM] Listening on http://localhost:8001") | ||
fmt.Println("[CUSTOM] Try to send a request :D") | ||
customApp.Listen(":8001") | ||
|
||
} | ||
|
||
func HandlerDefault(c *fiber.Ctx) error { | ||
beautyCallLog("HandlerDefault") | ||
return c.SendString("Watch your app logs!") | ||
} | ||
|
||
func HandlerMsgParam(c *fiber.Ctx) error { | ||
beautyCallLog("HandlerMsgParam") | ||
|
||
msg := c.Params("*") | ||
c.Locals("message", msg) // Set context "message" | ||
|
||
return c.SendString("Watch your app logs! and see the difference (Hint: `message` will show on your logs)") | ||
} | ||
|
||
func HandlerCustomMsgParam(c *fiber.Ctx) error { | ||
beautyCallLog("HandlerCustomMsgParam") | ||
|
||
msg := "CUSTOM CONTEXT MESSAGE" | ||
c.Locals(MY_CONTEXT_MESSAGE_KEY, msg) // Set context "message" for zlogres | ||
|
||
return c.SendString("Watch your app logs! and see the difference (Hint: `message` will show on your logs)") | ||
} | ||
|
||
func beautyCallLog(called string) { | ||
m := regexp.MustCompile(".") | ||
dashed := "------------" + m.ReplaceAllString(called, "-") + "----" | ||
fmt.Println(dashed) | ||
fmt.Printf("--- Called: %v ---\n", called) | ||
fmt.Println(dashed) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module github.com/owlsome-official/zlogres | ||
|
||
go 1.22.1 | ||
|
||
require ( | ||
github.com/gofiber/fiber/v2 v2.52.5 | ||
github.com/rs/zerolog v1.33.0 | ||
github.com/valyala/fasthttp v1.55.0 | ||
) | ||
|
||
require ( | ||
github.com/andybalholm/brotli v1.1.0 // indirect | ||
github.com/google/uuid v1.6.0 // indirect | ||
github.com/klauspost/compress v1.17.9 // indirect | ||
github.com/mattn/go-colorable v0.1.13 // indirect | ||
github.com/mattn/go-isatty v0.0.20 // indirect | ||
github.com/mattn/go-runewidth v0.0.16 // indirect | ||
github.com/rivo/uniseg v0.4.7 // indirect | ||
github.com/valyala/bytebufferpool v1.0.0 // indirect | ||
github.com/valyala/tcplisten v1.0.0 // indirect | ||
golang.org/x/sys v0.25.0 // indirect | ||
) |
Oops, something went wrong.