Reputation: 321
I have spring boot microservices,I use sl4j logger factory and in each service I log messages as follows:
LOG error during an exception:
LOG.error("service:{};msg:{};ex:{}", serviceName, msg, ex.getMessage(), ex);
LOG error during validation:
LOG.error("service:{};msg:{}", serviceName, msg);
LOG information:
LOG.info("service:{};event:{};msg:{}", service, event, message);
This is common in all my microservices, centralise this logging messages using a wrapper or so ? Or put the wrapper in a separate maven application and add dependency as jar ?
I want to know what the best design could be, code snippets will help.
Upvotes: 2
Views: 1020
Reputation: 10882
In general, it's a good idea to separate concerns, domain logic from infrastructure.
For instance, if you deploy your service as an AWS Lambda, you can see it as a kind of deployment.
Then you should create a separate Maven module where just your Lambda handler's code is located. Further, you can have a module for an Azure Function or a Spring Boot application. You can glue them together with help of Spring Context.
Your logging, mentioned above, would then be a part of your deployment module, because the domain shouldn't know anything about "services".
If your error handling is complicated, you can create a lib module for it.
Example for AWS Lambda:
public class Hello implements RequestHandler<Object, String> {
private DomainLogic domain;
private ErrorHandling errHandling;
Hello(DomainLogic domain, ErrorHandling errHandling) {
this.domain = domain;
this.errHandling = errHandling;
}
@Override
public String handleRequest(Object input, Context context) {
try {
return domain.sayHello(input);
}
catch (Exception e) {
errHandling.handle(e);
throw e;
}
}
}
Upvotes: 1