Skip to content

Commit e7870ed

Browse files
committed
feat: add echo example
1 parent 1fa9fcc commit e7870ed

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

example/echo/main.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"os"
7+
8+
"github.com/gcp-kit/stalog"
9+
"github.com/labstack/echo/v4"
10+
)
11+
12+
func main() {
13+
e := echo.New()
14+
15+
// Set request handler
16+
e.GET("/", func(c echo.Context) error {
17+
// Get request context logger
18+
logger := stalog.RequestContextLogger(c.Request())
19+
20+
// These logs are grouped with the request log
21+
logger.Debugf("Hi")
22+
logger.Infof("Hello")
23+
logger.Warnf("World")
24+
25+
return c.String(http.StatusOK, "OK")
26+
})
27+
28+
projectId := "my-gcp-project"
29+
30+
// Make config for this library
31+
config := stalog.NewConfig(projectId)
32+
config.RequestLogOut = os.Stderr // request log to stderr
33+
config.ContextLogOut = os.Stdout // context log to stdout
34+
config.Severity = stalog.SeverityInfo // only over INFO logs are logged
35+
config.AdditionalData = stalog.AdditionalData{ // set additional fields for all logs
36+
"service": "foo",
37+
"version": 1.0,
38+
}
39+
40+
// Set middleware for the request log to be automatically logged
41+
e.Use(stalog.RequestLoggingWithEcho(config))
42+
43+
// Run server
44+
fmt.Println("Waiting requests on port 8080...")
45+
if err := e.Start(":8080"); err != nil {
46+
panic(err)
47+
}
48+
}

0 commit comments

Comments
 (0)