File tree Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments