-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
43 lines (38 loc) · 1.07 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
package main
import (
"net/http"
"os"
"time"
log "github.com/sirupsen/logrus"
"github.com/go-logrusutil/logrusutil/errfield"
"github.com/go-logrusutil/logrusutil/logctx"
)
func main() {
// setup logging
log.SetOutput(os.Stdout)
// set errfield.Formatter to log the error fields
log.SetFormatter(&errfield.Formatter{
Formatter: &log.TextFormatter{
TimestampFormat: time.RFC3339Nano,
},
})
// set logctx.Default and use it in places when there is no context
logctx.Default = log.WithField("app", "example")
logctx.Default.Info("server starting")
// Output: time="2019-11-11T20:45:01.3777602+01:00" level=info msg="server starting" app=example
// bootstrap HTTP server
mux := http.NewServeMux()
registerHello(mux)
registerTry(mux)
registerGo(mux)
// create a server with structred contextual logging middleware
server := http.Server{
Addr: ":8080",
Handler: logMiddleware(mux),
}
err := server.ListenAndServe()
if err != http.ErrServerClosed {
logctx.Default.WithError(err).Fatal("server unexpectedly closed")
}
logctx.Default.Info("server closed")
}