I'm trying to solve a common problem of logging each method that calls 3rd party operation and I don't understand how to scale it up. Current implementation:
public class ElasticsearchClient {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("localhost", 9200, "http"),
new HttpHost("localhost", 9201, "http")));
public void createIndex() throws IOException {
CreateIndexRequest request = new CreateIndexRequest("twitter");
client.indices().create(request, RequestOptions.DEFAULT);
}
}
public class ElasticsearchProxy {
ElasticsearchClient client = new ElasticsearchClient();
Logger logger = LoggerFactory.getLogger(this.getClass().getSimpleName());
public void createIndex() throws IOException {
logger.info("Calling to method " + this.getClass().getEnclosingMethod().getName() + " in ES");
client.createIndex();
}
}
As you can see now I only have 1 method - createIndex()
but what if I have multiple methods - reIndex()
, deleteIndex()
, getIndex()
and many more, for each of those methods I need to do:
public void reIndex() throws IOException {
logger.info("Calling to method " + this.getClass().getEnclosingMethod().getName() + " in ES");
client.reIndex();
}
public void deleteIndex() throws IOException {
logger.info("Calling to method " + this.getClass().getEnclosingMethod().getName() + " in ES");
client.deleteIndex();
}
public void getIndex() throws IOException {
logger.info("Calling to method " + this.getClass().getEnclosingMethod().getName() + " in ES");
client.getIndex();
}
I find it to be a crazy code duplication and it feels that there must be another way to do it so:
- I will not repeat the
logger.info(...)
line - I will not have to copy each method in the client class to be in the proxy class as well, I would like to have an option to add a method in the client class that will be automatically exposed in the proxy class but with the log message.
Thanks