Golang App with Hexagonal Architecture with logging middleware

I am developing an application in golang with hexagonal architecture. I required to print the requestId in the logs where ever I want to use logs.

To do this, I am generating a random request Id and attaching it with the GIN context. And suppose I want to log info at repository adapter. My problem is that, in this case to be able to log I need to pass the context object to each and every components(layers), adapter to ports to domain to repository and each methods where I want to log request Id. This will be not according to hexagonal architecture and clean code. So, I dont want to pass context to each components and methods to be able to get the information set in GIN Context Is there a way like we do in spring, we can get the principle object anywhere once it get set in spring security context.

two things:

  1. passing a context down form transport (adapter in Hexagonal Architecture) to Port and then Business rule is perfectly fine in Go,
  2. For the logging I prefer to build middlewares and apply to all layers (adapter logging middleware, port logging middleware, business logic log middleware, …).

An example on how extensively using logging middleware is how go-kit is designed; have a look to the examples to see what is the idea; the same principles are applicable to other frameworks

I am as well using logging middleware, setting up requiestId and tenantId(as it multitenant app) to the Gin Context. But I do not want the burden of passing the context object with other business parameter to different functions of different components to be able to login the detail set in context. I want this context object injected in functions where i want to logging. Passing the context object and mixing with business parameter in each and every function looks very ugly and redundant code.

well, never used Gin (in general I prefer to stay away from frameworks and go with toolkits) so what I’m saying might be not correct but Isn’t the main purpose of a context to be enriched on each level of a call stack?

as I said I use go-kit so I have more freedom to not put non business related info in context but if you need to do it I don’t think is bad

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.