Skip to content

Commit

Permalink
log with color
Browse files Browse the repository at this point in the history
  • Loading branch information
zerolethanh committed Nov 15, 2022
1 parent ac58360 commit 28e4711
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 10 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.19

require (
github.com/andybalholm/brotli v1.0.4 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/gofiber/fiber/v2 v2.39.0 // indirect
github.com/klauspost/compress v1.15.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY3JY=
github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/gofiber/fiber/v2 v2.39.0 h1:uhWpYQ6EHN8J7FOPYbI2hrdBD/KNZBC5CjbuOd4QUt4=
github.com/gofiber/fiber/v2 v2.39.0/go.mod h1:Cmuu+elPYGqlvQvdKyjtYsjGMi69PDp8a1AY2I5B2gM=
github.com/klauspost/compress v1.15.0 h1:xqfchp4whNFxn5A4XFyyYtitiWI8Hy5EW59jEwcyL6U=
github.com/klauspost/compress v1.15.0/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
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.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
Expand All @@ -25,9 +30,12 @@ golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 h1:3MTrJm4PyNL9NBqvYDSj3DHl4
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab h1:2QkjZIsXupsJbJIdSjjUOgWK3aEtzyuh2mPt3l/CkeU=
Expand Down
58 changes: 48 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package frlog
import (
"encoding/json"
"fmt"
"github.com/fatih/color"
"github.com/gofiber/fiber/v2"
"github.com/samber/lo"
"sort"
)

type Options struct {
Expand All @@ -17,6 +19,7 @@ var DefaultOptions = Options{
RawPrint: false,
}

// PrintAppStacks will print all routes
func PrintAppStacks(app *fiber.App, options ...*Options) {

var opt Options
Expand All @@ -28,6 +31,7 @@ func PrintAppStacks(app *fiber.App, options ...*Options) {

stacks := lo.Flatten[*fiber.Route](app.Stack())

color.Blue("-- App Route Stacks (%d) --", len(stacks))
if opt.PrintByPath {
printByPathStacks(stacks)
}
Expand All @@ -51,18 +55,52 @@ func printByPathStacks(stacks []*fiber.Route) {
byPathStacks := lo.GroupBy(stacks, func(stack *fiber.Route) string {
return stack.Path
})
for path, stacks := range byPathStacks {
fmt.Println("--", path)
//var methods string
for _, stack := range stacks {
var params string
//methods += stack.Method + " "
if len(stack.Params) > 0 {
params = fmt.Sprintf("Params: %v", stack.Params)
paths, sortedByPathStacks := sortMapByKey(byPathStacks)

for _, path := range paths {
color.HiYellow(path)
routes := sortedByPathStacks[path]
fmt.Print(" ➜")
for _, route := range routes {
params := getRouteParams(route)
method := route.Method
c := color.WhiteString

switch method {
case "OPTIONS":
c = color.HiCyanString
case "GET", "POST", "PUT", "PATCH":
c = color.HiGreenString
case "DELETE":
c = color.HiRedString
default:
c = color.WhiteString
}
fmt.Print(fmt.Sprintf(" %s %s", stack.Method, params))
fmt.Print(c(" %s %s", method, params))
}
fmt.Println("")
//fmt.Println(" ", methods)
}
}

func getRouteParams(route *fiber.Route) string {
var params string
if len(route.Params) > 0 {
params = fmt.Sprintf("Params: %v", route.Params)
}
return params
}

func sortMapByKey(m map[string][]*fiber.Route) ([]string, map[string][]*fiber.Route) {
var keys []string
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)

sortedMap := make(map[string][]*fiber.Route)
for _, k := range keys {
sortedMap[k] = m[k]
}

return keys, sortedMap
}

0 comments on commit 28e4711

Please sign in to comment.