5 Things to Avoid When Logging From Your Application
What are the problems that come along with logging and how do you avoid them? In this article, Erik Dietrich discusses some things that can come back to bite you.
Join the DZone community and get the full member experience.Join For Free
it seems almost strange to talk about avoiding things while logging. after all, logging is your last line of defense or your salvation in many cases. some crazy bug in the field that shows up every third full moon? an external auditor looking at your app’s runtime behavior? logging to the rescue.
naturally, is stands to reason that you would want to log just about everything your application. whenever there’s any doubt, slam a logger call in there and let log level sort out the details. you can always filter logs, but you can’t magic stuff into them after the fact. so why, then, talk of avoidance?
well, it turns out that while logging may be a highly inclusive activity in terms of what should be included, there are ways to create problems. you want to be liberal in terms of what you log, but judicious and wise in terms of how you log it. you don’t want to indulge in a feckless free-for-all when it comes to the calls you make to your application’s logger.
what are these problems and how do you avoid them? let’s take a look at some things that can come back to bite you.
1. forgetting context
if you want to avoid being hated by someone in the future who's debugging with the log file, avoid forgetting context. did a message just arrive? perhaps instead of just saying “message arrived,” you should state what was in the message, as well. to see what i mean, consider this code:
publicvoidprocessfile() try _fetcher.fetchfile(); catch(invalidfiletypeexception ifte) _logger.log("invalid file type.")
there’s a collaborator field called fetcher tasked with fetching a file. the code anticipates that this action could result in the file type being invalid and, if that happens, it informs the user that the file type was invalid.
you can’t go the extra mile and log the exception? or, absent that, at least inspect it to see what the offending file type is?
try to put yourself in the position of the person reading this message and ask what they might be thinking. “oh, i see there was an invalid file type.” it’s pretty much a certainty that they’re going to then ask, “what was this file type that proved invalid?” supply them with that context.
2. cryptic codes
another source of log file woe related to lack of context (and, perhaps even a specific case of it) is having some sort of mystery code in a log entry. imagine perusing a log file and finding “error reading file: error 9024.”
the absolute best case scenario here is that as a maintenance programmer, i have an encyclopedic knowledge of the application’s error codes. i’m in luck because i can say, “oh, that’s what happens when we process a file that uses the deprecated pipe-delimited format.” however, the next best scenario is a lot less rosy — i have to hunt down in the source code or in some documentation what that error code means. worst of all would be this search dead-ending at some magic number in the code. at this point, the information in the log is basically useless.
if you’re going to log some code that’s fine, but also log along with it what it means, the way an exception is logged alongside an explanatory message.
3. spamming the log file
have you ever seen a string of identical entries inside of a file that provided you with no valuable insight at all? it is possible for your application to “spam” the log file, recording sequences of vacuous entries. as a rather obvious example, consider a revamped version of the file fetcher from above.
publicconfigfile processfile() while(_fetcher.getstatus()!=filefetcherstates.ready) _logger.log("waiting for filefetcher ready state."); varfile=_fetcher.fetchfile(); _logger.log("file "+file.name+" successfully fetched."); returnfile;
this could, quite possibly, result in tens of thousands of lines of “waiting for filefetcher ready state,” differing by only a timestamp. is that really a valuable use of your application’s logging? at best, this provides you some statistics about the performance of the getstatus() call, but there are other, less deluge-inducing options. you could simply keep track of when you started polling, when you stopped, and how many times it happened, recording that information only.
as with context and codes, bear in mind what the log file will look like. logging in a tight loop is going to bury maintenance folks in quasi-useless information that would serve better in digest form.
4. unsafe logging calls
take a look at the code from the last example and see if you notice anything else that’s amiss. does something look fishy about that second call to the logger?
what happens if _fetcher.fetchfile() returns null? aside from the logger calls, the method would return the null value. with that second logging call in the mix, the method will throw a null reference exception. that’s not good.
as a general rule of thumb, logging should be extremely safe as far as your application code goes. after all, logging is important, but it tends not to be mission critical. you don’t want it to affect actual runtime behavior; you want it to do its best to bear quiet witness to everything that happens. avoid invoking your logger in ways that can generate exceptions or cause other similar dependencies.
5. mixing application logic with logging
had i not seen it for myself on various occasions, i might think that this one goes without saying. apparently, it does not, so i’ll say it. avoid having your logging implementation do things that your application needs besides logging.
if you don’t understand what i mean, i’ve encountered situations in which people have thought to wrap standard or home-grown logging implementations and couple them with other concerns like other aspects or other types of file i/o. in perhaps the most egregious example that i can recall, a home-rolled logging implementation became a handy repository for global variables, since it was universally acceptable and implemented as a static. needless to say, this created a hopelessly tangled mess in which the logger was actually critical to the application’s normal operation.
in general, it’s not good application architecture practice to couple things together unnecessarily, as the single responsibility principle advises. logging is an extremely unique application concern that behaves like nothing else, making it a particularly terrible candidate for this sort of thing. avoid having a logging implementation do anything but log.
as i mentioned in the beginning of the post, when it comes to logging, more is more. you want to be capturing as much information about the state of your application as you can to arm maintenance programmers, operations folks, people reproducing errors, and really, any interested party. to do that, you need to make sure you’re capturing the information, but you also need to make sure that you’re doing so in a sensible way.
Published at DZone with permission of , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.