MCPcopy Create free account
hub / github.com/TheThingsNetwork/lorawan-stack / ExampleMiddleware

Function ExampleMiddleware

pkg/log/example_test.go:22–63  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

20)
21
22func ExampleMiddleware() {
23 // build our custom handler (needed for example tests, because it expects us to use fmt.Println)
24 handler := HandlerFunc(func(entry Entry) error {
25 fmt.Printf("%s: %s\n", strings.ToUpper(entry.Level().String()), entry.Message())
26 return nil
27 })
28
29 logger := NewLogger(handler, WithLevel(InfoLevel))
30
31 // printer is a middleware that prints the strings be
32 printer := MiddlewareFunc(func(next Handler) Handler {
33 return HandlerFunc(func(entry Entry) error {
34 fmt.Println("== Before ==")
35 err := next.HandleLog(entry)
36 fmt.Println("== After ==")
37
38 return err
39 })
40 })
41
42 messages := 0
43
44 // counter is a middleware that counts the messages
45 counter := MiddlewareFunc(func(next Handler) Handler {
46 return HandlerFunc(func(entry Entry) error {
47 messages++
48 return next.HandleLog(entry)
49 })
50 })
51
52 logger.Use(printer)
53 logger.Use(counter)
54
55 logger.Info("Hey!")
56 fmt.Println("Number of messages:", messages)
57
58 // Output:
59 // == Before ==
60 // INFO: Hey!
61 // == After ==
62 // Number of messages: 1
63}

Callers

nothing calls this directly

Calls 11

UseMethod · 0.95
InfoMethod · 0.95
NewLoggerFunction · 0.85
WithLevelFunction · 0.85
HandlerFuncFuncType · 0.70
MiddlewareFuncFuncType · 0.70
StringMethod · 0.65
LevelMethod · 0.65
MessageMethod · 0.65
HandleLogMethod · 0.65
PrintfMethod · 0.45

Tested by

no test coverage detected