-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add structured logging using zap (#2)
- Loading branch information
Showing
11 changed files
with
231 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
internal/server | ||
api/openapi.yaml | ||
.history/ | ||
zero-notification-service | ||
|
||
.history/ | ||
api/openapi.yaml | ||
internal/server/* | ||
!internal/server/logger.go |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,3 +26,4 @@ main.go | |
go.mod | ||
README.md | ||
Dockerfile | ||
internal/server/logger.go |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Helpers for ECS log formatting | ||
// https://www.elastic.co/guide/en/ecs/master/index.html | ||
package log | ||
|
||
import ( | ||
"time" | ||
|
||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
// ECSHTTP represents a subset of the fields of the ECS HTTP object | ||
type ECSHTTP struct { | ||
Request ECSRequest | ||
Response ECSResponse | ||
} | ||
|
||
func (o ECSHTTP) MarshalLogObject(enc zapcore.ObjectEncoder) error { | ||
enc.AddObject("request", o.Request) | ||
return enc.AddObject("response", o.Response) | ||
} | ||
|
||
// ECSResponse represents a subset of the fields of the ECS HTTP Response object | ||
type ECSResponse struct { | ||
StatusCode int | ||
} | ||
|
||
func (o ECSResponse) MarshalLogObject(enc zapcore.ObjectEncoder) error { | ||
enc.AddInt("status_code", o.StatusCode) | ||
return nil | ||
} | ||
|
||
// ECSRequest represents a subset of the fields of the ECS HTTP Request object | ||
type ECSRequest struct { | ||
Method string | ||
} | ||
|
||
func (o ECSRequest) MarshalLogObject(enc zapcore.ObjectEncoder) error { | ||
enc.AddString("method", o.Method) | ||
return nil | ||
} | ||
|
||
// ECSURL represents a subset of the fields of the ECS URL object | ||
type ECSURL struct { | ||
Original string | ||
} | ||
|
||
func (o ECSURL) MarshalLogObject(enc zapcore.ObjectEncoder) error { | ||
enc.AddString("original", o.Original) | ||
return nil | ||
} | ||
|
||
// ECSEvent represents a subset of the fields of the ECS Event object | ||
type ECSEvent struct { | ||
Action string | ||
Duration time.Duration | ||
} | ||
|
||
func (o ECSEvent) MarshalLogObject(enc zapcore.ObjectEncoder) error { | ||
enc.AddString("action", o.Action) | ||
enc.AddInt64("duration", int64(o.Duration)) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package log | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/commitdev/zero-notification-service/internal/config" | ||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
// Init sets up logging based on the current environment | ||
func Init(config *config.Config) { | ||
var rawLogger *zap.Logger | ||
var err error | ||
if config.StructuredLogging { | ||
// Info level, JSON output | ||
zapConfig := zap.NewProductionConfig() | ||
zapConfig.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder | ||
zapConfig.EncoderConfig.MessageKey = "message" | ||
rawLogger, err = zapConfig.Build() | ||
} else { | ||
// Debug level, pretty output | ||
zapConfig := zap.NewDevelopmentConfig() | ||
zapConfig.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder | ||
rawLogger, err = zapConfig.Build() | ||
} | ||
|
||
if err != nil { | ||
log.Fatalf("can't initialize zap logger: %v", err) | ||
} | ||
|
||
zap.ReplaceGlobals(rawLogger) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Zero Notification Service | ||
* | ||
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator) | ||
* | ||
* API version: 1.0.0 | ||
* Generated by: OpenAPI Generator (https://openapi-generator.tech) | ||
*/ | ||
|
||
package server | ||
|
||
import ( | ||
"net/http" | ||
"time" | ||
|
||
"github.com/commitdev/zero-notification-service/internal/config" | ||
"github.com/commitdev/zero-notification-service/internal/log" | ||
"go.uber.org/zap" | ||
) | ||
|
||
// HTTP Reponse Writer with a little wrapper to collect the status code | ||
type loggingResponseWriter struct { | ||
http.ResponseWriter | ||
statusCode int | ||
} | ||
|
||
func (lrw *loggingResponseWriter) WriteHeader(code int) { | ||
lrw.statusCode = code | ||
lrw.ResponseWriter.WriteHeader(code) | ||
} | ||
|
||
func Logger(inner http.Handler, name string) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
start := time.Now() | ||
|
||
lrw := &loggingResponseWriter{w, http.StatusOK} | ||
inner.ServeHTTP(lrw, r) | ||
|
||
if config.GetConfig().StructuredLogging { | ||
http := log.ECSHTTP{ | ||
Request: log.ECSRequest{ | ||
Method: r.Method, | ||
}, | ||
Response: log.ECSResponse{ | ||
StatusCode: lrw.statusCode, | ||
}, | ||
} | ||
url := log.ECSURL{Original: r.RequestURI} | ||
event := log.ECSEvent{Action: name, Duration: time.Since(start)} | ||
|
||
zap.S().Infow("HTTP Request", zap.Any("http", http), zap.Any("url", url), zap.Any("event", event)) | ||
} else { | ||
zap.S().Infow("HTTP Request", | ||
"method", r.Method, | ||
"status_code", lrw.statusCode, | ||
"url", r.RequestURI, | ||
"action", name, | ||
"duration", time.Since(start), | ||
) | ||
} | ||
}) | ||
} |