Is it a good idea to print method name in the log by defining method name as a string in the production like below?
private void foo() {
String methogTag = "foo"; // here defining method name
int updateCount = // db update operation
log.info("{} : {} record/s are updated", methogTag, updateCount);
int deleteCount = // db delete operation
log.info("{} : {} record/s are deleted", methogTag, deleteCount);
}
log output:
2019-04-22 13:24:41.572 ClazzName - foo : 20 record/s are updated
2019-04-22 13:24:41.600 ClazzName - foo : 12 record/s are deleted
We can get method name printed in logs using %M
in log4j
pattern layout.
pattern layout:
%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5level] [%t] %c{3.}%M - %msg%n
And there is a warning in the log4j documentation
WARNING Generating caller location information is extremely slow and should be avoided unless execution speed is not an issue.
Or, is there any alternative way to get the method name in the loggers without any memory overhead?
Thanks.