Chiseled
Chiseled

Reputation: 2300

Logger object for a single software component in the Log4j2 environment

As per log4j2 user guide , it looks like only one Logger object gets created for a class.

If i call the static method LogManager.getLogger("SomeClass") from anywhere in my code , it should give me back the reference to the same logger object.

Do multiple threads in the application that have the above line of code , end up using the same logger object ?

Upvotes: 0

Views: 47

Answers (1)

Acewin
Acewin

Reputation: 1727

LogManager.getLogger("SomeClass") is a static call so, on that basis I will say yes If you make call to get logger for same class it will return the same logger object.

Different threads will not make any difference till it is same container.

All threads calling with same Class Name will provide same instance of logger. That is until and unless you setup a custom logger repository which returns a different logger(object). Custom logger repo will mean that you can manipulate how the logger objects get stored and returned.

I tested this in theory. Created 2 separate classes. Created logger for both the classes. Leaving the class name passed to the call same for both.

Called up logger to print their hash which comes out to be same.

Upvotes: 1

Related Questions