Seite 1 von 1

EX00 - Logger

Verfasst: 23. Apr 2018 19:45
von UdoWeber
Hi,

I have some question:

"The logger should be cached inside of the interface and it should not be looked up every time it is used"

If I understand this right, cached means the logger should be a final static field inside the interface, as in the example?
If yes, I'm not sure how to initialize the log4j logger correctly inside the interface?
How do I get the class which implements the interface? (naturally in this case this.getClass() does not work)

Thanks for your help.
Greets Udo

Re: EX00 - Logger

Verfasst: 24. Apr 2018 09:06
von AizazZaidee
I wrote an initialize method inside an interface and called it in the constructor of the class. I don't know if that's the right thing to do but I cannot think about anything else.

Re: EX00 - Logger

Verfasst: 24. Apr 2018 09:09
von AizazZaidee
How do I get the class which implements the interface? (naturally in this case this.getClass() does not work)
Use static method instead of using this.getClass

Re: EX00 - Logger

Verfasst: 24. Apr 2018 17:55
von UdoWeber
How do I get the class which implements the interface? (naturally in this case this.getClass() does not work)
Use static method instead of using this.getClass
Ok, but then you have a fixed class, but you normally want the class that implements the interface and this class can change.
I wrote an initialize method inside an interface and called it in the constructor of the class. I don't know if that's the right thing to do but I cannot think about anything else.
This seems to be a possible idea, thanks :)

Re: EX00 - Logger

Verfasst: 24. Apr 2018 18:12
von pmueller
Hi,

for this task it is fine if the logger is initialized with the Class of the Interface statically.

Best,
Patrick

Re: EX00 - Logger

Verfasst: 25. Apr 2018 16:52
von nknauber
Hi,
for this task it is fine if the logger is initialized with the Class of the Interface statically.
So it would be fine to initialize the logger simply with the name of the interface as getting the classname of the class that implements the interface would be "too hard" for this task? Do I understand this correctly? Even though this way, the logmessage will only contain the name of the interface?

Thanks,
Nicolas

Re: EX00 - Logger

Verfasst: 25. Apr 2018 17:51
von pmueller
Hi,

yes this correct.

Best,
Patrick

Re: EX00 - Logger

Verfasst: 26. Apr 2018 15:56
von topracer
I implemented a nested "cache class" inside the interface which holds the current logger as a static variable. The logger is returned by a static getter method and is only exchanged if the class of the requesting object differs from a previous one. That way, displaying the particular class name works, but the logger is actually not really cached "inside" the interface...

Is this still a valid solution for the task or should I go for the static initialization instead?

Regards,
Frederic

Re: EX00 - Logger

Verfasst: 26. Apr 2018 16:47
von pmueller
Hi,

since I have not seen your solution, I think it would be a good Idea to revert to the simpler solution.

Best,
Patrick