graniticio / granitic

Web/micro-services and IoC framework for Golang developers
https://granitic.io/
Apache License 2.0
35 stars 12 forks source link

Random failure on server start when a component implements logging.ContextFilter #61

Closed subeshb1 closed 4 years ago

subeshb1 commented 4 years ago

Summary

When a field that needs to access values from the context is passed in the config of LogWriting, it expects a component implementing logging.ContextFilter to be injected. Even if a component implementing logging.ContextFilter is passed, there is a random failure that arises when starting the server with the message:

your JSON application logging configuration includes fields that display information from the context, but no component is available that implements logging.ContextFilter

This is thrown by a check done in JSONLogFormatter's StartComponent(). JSONLogFormatter requires a context filter to be injected if it requires one. It expects ComponentLoggerManager to set the context filter in JSONLogFormatter. Since the context filter is set in StartComponent() of ComponentLoggerManager, when JSONLogFormatter's StartComponent() is called first the service fails.

Solution

The check for context.Filter should be done only after ComponentLoggerManager has exectued StartComponent. Move the check for context filter in to AlllowAccess() lifecycle instead of StartComponent() in JSONLogFormatter component