Log4j2: How can I get the class name and line number without using Throwable?

I developed a wrapper class in Log4j2. Using OSGi declarative services, I published a special registration service using my own registrar interface with a wrapper class that is an implementation. The wrapper class is used only for programmatically processing the journal, formatting messages, and adding several methods; finally, it calls Log4j2 logging methods.

I want to print the source class / file name and line number of each log requested in the log file. The% C /% F and% L options only print location information inside my wrapper class, where I actually call the log method.

So, as a workout, I pass new Throwable as an argument every time to use the% throwable {short.lineNumber} layout. But this is an expensive process for embedded applications.

My main problem is getting the line number, because for the file name I could at least request a new logger from Log4j2 with the name of each service requesting the registration service, and save it on the map.

Is there a solution for caller tracking? I hope there is a similar solution for applications where you do not want to have LOG4j2 jars for each user of the registration service. Just for information, I do not want to use any XML files, all configurations are performed programmatically.

+4
source share
2

StackTraceElement[] stes = Thread.currentThread().getStackTrace();

, .

, , ( ) . IDE, . .

+4

Log4j2 , . , , FQCN ( ) org.apache.logging.log4j.spi.ExtendedLogger#logMessage.

Log4j2 , . ( ): http://logging.apache.org/log4j/2.x/manual/customloglevels.html#CustomLoggers

. FQCN .

+2

All Articles