In my logging messages, I need to insert the name of the method where the messages were produced. I've looked at Log4J documentation and "M" and "l" conversion chars that also have warning like "WARNING Generating caller location information is extremely slow and should be avoided unless execution speed is not an issue". So I have (at least) two options:
log.info("myMethod: message");
which will be faster but not as elegantAre there any other options that would not slow down my code?
Thanks!
I am not sure of the IDE that you are using, but if you are using Eclipse based IDE's you should be able to use some Java templates. This will still be using the method names as strings,but will save you typing time.
I created a whole set of templates like this:
li
logger.info("${cursor}");
and so on for warn
, error
, and debug
.
For start and end of method, or just to add method name to every log out - use something like:
lie
logger.info("End: ${enclosing_method}${cursor}");
and so on.
The only constraint is your logger ref variable will always have to be named logger
.
Now you will just need to type lie
and (optionally hit ctrl+space if you uncheck the Automatically Insert checkbox at template creation time.)
HTH!
I use the basic configurator most of the times and it gives me the method name by default. So technically adding just this single line of code and the import statement is the fastest way to get what you are asking for.
BasicConfigurator.configure();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With