Logging Middleware Best Practices

Logging Middleware Best Practices

Authentication and Authorization

API Gateway should handle initial authentication, but it's crucial to use auth tokens between the Gateway and services for added security. "Good practice is to authorize in API Gateway, but then also to use auth tokens between your Gateway and services."

Misconfiguration errors can bypass the API Gateway, making multiple layers of protection essential. "A misconfiguring error is easily made. Thus it would bypass your API gateway."

Logging Scope

Gateway should log incoming request metadata, rate-limiting events, authentication/authorization outcomes, latency, status codes, routing decisions, and rejected requests. "Gateway should log: Incoming request metadata, Rate-limits / throttling events, Authentication & authorization outcomes, Latency, status codes, Routing decisions, Rejected requests (bad tokens, blocked IPs, throttling)"

API services should log business logic events, errors/exceptions, DB queries timing/failures, external service calls, validation failures, and state changes. "API service should log: Business logic events, Errors or exceptions, DB queries timing + failures, External service calls, Validation failures, State changes (create/update/delete events)"

Structured Logging

Use structured logging formats like JSON for better parseability and indexing in production environments. "Structured logs FTW. Want pretty logs? Pipe structured logs to a formatter."

Avoid colored logs in production as they can be difficult to parse and manage. "In a production environment, I'd recommend using something like a standardized JSON log format."

Performance Considerations

Avoid making database calls from middleware as it can block the entire stream and impact performance. "You don't access the database in middleware."

Defer intensive operations like database checks to server components or actions. "The best IMO is to do access control in the data layer when the private data is read."

Custom Middleware Implementation

For Go, a basic middleware follows the func(next http.Handler) http.Handler signature. "A middleware at its most basic follows the following function signature func(next http.Handler) http.Handler"

You can chain multiple middlewares to create a comprehensive logging system. "Chain(middleware.Log, myHandler)"

Error Logging

Implement middleware to log errors to a database or a dedicated logging service. "The company I am interning at provided me with the task of error logging in the database through middleware."

Use app.UseExceptionHandler in ASP.NET Core to catch and log unhandled exceptions. "app.UseExceptionHandler('/Error');"

Context Enrichment

If middleware needs to access data added by other middlewares, ensure the order is correct or use a custom ResponseWriter to store additional data. "You want to access some data added in lower middleware in upper middleware. For this you can rearrange the middleware order."

Back to Blog