The logz is Go library for grouping a access log and application logs
MIT License
The logz is logger library for grouping application logs related a access log. The logz uses OpenTelemetry(https://opentelemetry.io) to generate the trace id. This is for Google Cloud Logging (formerly known as Stackdriver Logging).
Use go111 package if your project is App Engine 1st generation.
The logz contribution packages that provides middlewares for 3rd-party Go packages.
For more details: https://github.com/glassonion1/logz/tree/main/contrib
$ go get github.com/glassonion1/logz
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
// Writes info log
logz.Infof(ctx, "writes %s log", "info")
})
logz.SetConfig(logz.Config{
NeedsAccessLog: true, // Writes the access log
})
logz.InitTracer()
// Sets the middleware
h := middleware.NetHTTP("tracer name")(mux)
log.Fatal(http.ListenAndServe(":8080", h))
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
// Writes info log
logz.Infof(ctx, "writes %s log", "info")
})
logz.SetConfig(logz.Config{
ProjectID: "your gcp project id",
NeedsAccessLog: false, // Writes no access log
})
logz.InitTracer()
// Sets the middleware
h := middleware.NetHTTP("tracer name")(mux)
log.Fatal(http.ListenAndServe(":8080", h))
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
// Writes info log
logz.Infof(ctx, "writes %s log", "info")
})
logz.SetConfig(logz.Config{
ProjectID: "your gcp project id",
NeedsAccessLog: true, // Writes the access log
})
logz.InitTracer()
// Sets the middleware
h := middleware.NetHTTP("tracer name")(mux)
log.Fatal(http.ListenAndServe(":8080", h))
}
See this sample projects for logz detailed usage https://github.com/glassonion1/logz/tree/main/example
The logz leverages the grouping feature of GCP Cloud Logging. See following references for more details.
The log format is based on LogEntry's structured payload
{
"severity":"INFO",
"message":"writes info log",
"time":"2020-12-31T23:59:59.999999999Z",
"logging.googleapis.com/sourceLocation":{
"file":"logger_test.go",
"line":"57",
"function":"github.com/glassonion1/logz/internal/logger_test.TestLoggerWriteApplicationLog.func3"
},
"logging.googleapis.com/trace":"projects/test/traces/00000000000000000000000000000000",
"logging.googleapis.com/spanId":"0000000000000000",
"logging.googleapis.com/trace_sampled":false
}
{
"severity":"DEFAULT",
"time":"2020-12-31T23:59:59.999999999Z",
"logging.googleapis.com/trace":"projects/test/traces/a0d3eee13de6a4bbcf291eb444b94f28",
"httpRequest":{
"requestMethod":"GET",
"requestUrl":"/test1",
"requestSize":"0",
"status":200,
"responseSize":"333",
"remoteIp":"192.0.2.1",
"serverIp":"192.168.100.115",
"latencyy":{
"nanos":100,
"seconds":0
},
"protocol":"HTTP/1.1"
}
}