views:

353

answers:

2

Hey everybody,

I'm currently struggling with two problems about the java.util.logging.Logger:
1) I'd like to have a (convenience-)method like the "info()/fine()" methods, only that those methods should not insert a line break after the output (functionality like System.out.print and println). I've already overridden the format() method - would it be coorect to just add a "no-linebreak-flag" to the log record and analyze this in the format method or would this be too slow (because I'm using a real time application". Which would be the correct way to achieve sthg like this?

2)Currently I have a factory class which creates the desired Logger (i differ between FileLogging and Console Logging) - but I cant really find out how its possible to tell the Logger in detail which packages/classes("modules") to log and which not. Or am I supposed to check a Debug flag in each class and then either create the Logger (debug==true) or not? Also I would like to do all the needed settings via the source code and not the Logging-Properties file if possible...

I have the slight feeling theres something about the Logger I havent quite understood yet^^

Thanks in advance!

+1  A: 

on #2 - you should put all that information in the logging config file. The whole point is that you 'slice' this kind of stuff out of your code, and into properties files.

Basically - you always call logger.debug() in your code, and if the logging is configured to filter debug messages, then you will not see the output from hem

I have seen code like

if(logger.debugenabled()){
   logger.debug();
}

and when I've questioned it, people have said they don't want the performance penality of the logger.debug call. My humble opinion is that this is only relevant to a miniscule proportion of applications, and you should probably just leave the logger.debug call, and let the logging framework take care of it.

phatmanace
The real reason for the 'if debugEnabled' idiom is that it avoids the (often considerable) cost of assembling the argument strings passed to debug.
Stephen C
A: 

Oh - and I checked the source code for console appender. Looks to be like the newline is hardcoded.

      for (int i = 0; i < len; i++) {
        this.writer.write(s[i]);
        this.writer.write(Layout.LINE_SEP);
      }
    }
  }
phatmanace
Hey phatmanace,thanks for your answers. I dont know if i get you right, but: I know i can add a LINE_SEP in my format() method - the point is: how can i tell my format method when to add a line break and when not?in my source code for example I'd like to have sthg like this: logger.info("regular info with the standard line break afterwards");logger.info_nb("info with NO line break");(sorta like the mechanism of println and print)For adding methods to the Logger, i'd have to inherit my own and add the method then - but isnt there a simpler way to suppress a line break?