-4

To easily enable logging for plurality of my project classes, I have decided to abuse the new default keyword to create a simple method trait for my classes:

  default void Log(Level lvl, String msg) {
    Logger log = Logger.getLogger(this.getClass().getName()); 
    //Log something
  }

What I really dislike about this is the need to get the log every time. Were I working with C++, I'd declare:

static Logger log = ...;

Every other call to the function, the logger would already be initialized in the variable. In normal classes, I use this pattern to simulate static variable:

class A {
    //By default, this is null until needed
    private Obj cached_obj = null;
    public void doSomethingWithObj(Something thing) {
        //Once needed, it only initialises once
        if(cached_obj==null)
            cached_obj = Obj.generateObj();
        cached_obj.doSomething(thing);
    }
}

But this is not possible with interface. Interface cannot have any properties.

So is there some other workaround, or is Java going to hold my performance back again?

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
  • Doesn't seem all that hard to just have a static final `Logger` field in the classes that need to log. If you are worried about copy-paste errors, you could even write a helper method that gets a stack trace to determine what the calling class is so it can pass that to the `getLogger` method – NamshubWriter Feb 25 '15 at 14:56
  • @Namshub That's really expensive because you have to generate a stack trace. – fps Feb 25 '15 at 15:04
  • How would you get the implementation's class name statically in C++? – Sotirios Delimanolis Feb 25 '15 at 15:08
  • 1
    Duplicate? http://stackoverflow.com/questions/28720845/logging-from-default-interface-methods – assylias Feb 25 '15 at 15:17
  • @NamshubWriter The logging method will become a lot more complex. – Tomáš Zato Feb 25 '15 at 15:21
  • 2
    Can you post a full example in C++ so I can understand what you would actually do/want? – Sotirios Delimanolis Feb 25 '15 at 16:59
  • What do you not understand? In C++, there are no loggers, so I see no need to create an example which would be misleading in effect. If you don't know what's static variable just look it up. – Tomáš Zato Feb 25 '15 at 19:02
  • 1
    You said _Were I working with C++, I'd declare_ but then post an incomplete example. I don't see how C++ could achieve what you're asking for, so I'd like you to clarify. – Sotirios Delimanolis Feb 25 '15 at 19:41
  • _In effect, you don't know how could C++ allow me to cache function call result - which is what I want to do._ Right. That's what I want you to clarify, because I don't think C++ can do what you expect. You seem to want a `Logger` for each implementation of the interface, but you only want to retrieve that `Logger` instance once. Is that correct? – Sotirios Delimanolis Feb 25 '15 at 20:51
  • Yes, sorry that I didn't understand what's unclear. You can suggest an edit that improves the clarity - I'd be graceful. – Tomáš Zato Feb 25 '15 at 20:56
  • The expense of generating a stack trace isn't an issue, because the logger can be saved in a static field. That being said, I don't see what's difficult about just calling getLogger(CurrentClass.class) – NamshubWriter Feb 26 '15 at 15:43

1 Answers1

0

There's really no way to cache this as, by design, interfaces have no state associated with them.

That being said, this may not as big of an issue as it appears at first glance.

If you are logging enough to make it an issue, the JVM will probably optimize it enough that the difference between this version and the hypothetical cached version will be negligible. In addition, any performance bottlenecks are much more likely to be in the act of recording the log than in retrieving the logger.

My advice would be to profile your application to see if it actually make enough of a difference to worry about.

Morgen
  • 1,010
  • 1
  • 11
  • 15