Log4j: quick way to display a method name?

In my logging messages, I need to insert the name of the method in which the messages were created. I looked at the Log4J documentation and the conversion characters "M" and "l", which also have a WARNING warning. Generating caller location information is extremely slow and should be avoided unless execution speed is a problem. "Therefore, I have (at least) two options:

  • Use these characters, but slow down my code.
  • Manually enter the method name in the message, i.e. something like this log.info("myMethod: message"); which will be faster but not so elegant

Are there any other options that would not slow down my code?

Thanks!

+8
performance methods log4j
source share
3 answers

I'm not sure if you are using an IDE, but if you are using an Eclipse-based IDE, you can use some Java templates. This will still use the method names as strings, but will save your input time.

I created a whole set of templates, such as:

  • Template Name - li
  • Sample - logger.info("${cursor}");

etc. for warn , error and debug .

To start and end a method, or simply to add a method name for each logout, use the following:

  • Template Name - lie
  • Sample - logger.info("End: ${enclosing_method}${cursor}");

etc.

The only limitation is that your reflog variable should always have the name logger . Now you just need to enter lie and (optionally press ctrl + space if you uncheck the "Automatic insertion" box while creating the template.)

NTN!

+4
source share

I use the main configurator most of the time and it gives me the default method name. Thus, technically adding just this line of code and the import statement is the fastest way to get what you are asking for.

 BasicConfigurator.configure(); 
+2
source share

If there was a slow way to get this information programmatically, log4j would use it, unfortunately, not, because this is not information that the JVM was designed to make it accessible to the runtime.

That's why most logging protocols use only templates that extract this information at the debug level and then wrap all debug calls in the if(logger.isDebugEnabled()) { state, so that information is only retrieved when the program is in debug mode. This approach allows you to get fine-grained information about the runtime during debugging without affecting the performance of your code when it is in production.

It is also important to remember that not all code has high performance, and if your code works and scales using these templates, you can simply use them. If you run into performance issues, you can review.

Aside, Groovy (starting from version 1.8) has AST transformations that automatically insert registrars (from java.util, apache commons, log4j or flf4j flavors) into your classes, and also automatically complete all recording calls in conditions at compile time, so that they are executed only when logging is at an appropriate level. This means that you can do what I described above without having to explicitly code conditional expressions, but, of course, is only available if you're writing Groovy.

0
source share

All Articles