Is it better to log exception or exception.getMessage()while logging an info log or an error log. I'm pretty sure that is related to the context but I'd like to know from a technical perspective on which would be better for any application or if its all the same.

Personally I always want the complete stack trace, not just the error message

the error message itself is usually pointless. for all you know, some funny guy had code like this:

try{
// code throwing whatever Exceptions
}
catch(AnyException aE){
  throw new Exception("This message is pointless");
}
catch(Exception e){
  throw new Exception("Huh ... didn't see this one coming");
}

Sure, you can print the error message, but what good 'll it do? I second James' advice: log the stacktrace. Not only does it tell what the underlying cause was, but it'll also tell you exactly what line(s) of your code to investigate.

But, in addition: I would recommend having several logging 'profiles':
Info (logging only informational messages), debug (logging all you need to debug your code), error (logging errors, and not just superficial), ...

this way, if a product has run for years without any single issue (well, I'm allowed to dream, right?) you might want to loosen what you log, so you don't get too large log files with tons of information you'll never even look at.

Doesn't having the stacktrace for everything affect the performance? Yes, I do use info, debug and error logs.

You wouldn't need it for info, and probably not for well-designed debug logs, but if you need to trace an error then it's near essential. It doesn't cost much, and then only when you actually have an Exception.

if you have so many exceptions that need logging that it affects performance there's more wrong with the system than just logging performance...

Exceptions should be rare, exceptions that need logging a stacktrace of because you don't know how to handle them should be even more rare.

Just to be clear. When you guys say stacktrace do you refer to something like printStackTrace() or just the exception. From what I heard and read using
printStackTrace() is definitely not good.

printStackTrace() is really valuable when debugging, but is not something you would typically do in a live production system for the reasons discussed in that link.
When logging in a live system, and when a fatal Exception occurs I would log everything that could be relevant, including the stack at the time, obtained from getStackTrace()

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.