architecture - Where does request logging belong? -


should logging logic put in view layer or service layer?

i'm thinking service layer makes more sense since common layer underlying multiple different views (http, rpc, etc). if logging done in view layer, have implemented each different view.

but on other hand, if logging logic in service layer, there nothing log incoming requests , failures @ view level (for example jwt authentication failures, examine http request headers before request ever reaches service).

where should logging take place?

logging cross-cutting concern, means takes place in many layers (similar authentication). so, if feel need log e.g. presentation layer - using common interface, independent actual implementation. example, in java, use slf4j, facade underlying logger implementation. however, right it's idea have 1 place uses logging facade. log exceptions, warnings etc, log them in component responsible handling exceptions. in spring, @controlleradvice annotation used tag such component (it gives options intercept controller calls , e.g. handle exceptions in central place). other option use aop programming (e.g. aspectj) log methods/exceptions (but doesn't allow configure messages log during runtime of adviced join point). can use events, elegant solution, because code raise meaningful events, handled somewhere else (e.g. component responsible logging events).

so, sum - logging can happen in every layer, should avoid scatterring logger.log() calls everywhere in code , use more structured approach (central exception handling class, aop or events) instead.

btw, question not related ddd, think should remove domain-driven-design tag.


Comments