-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
79 lines (59 loc) · 2.18 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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)
}