Using Logger level wisely

Logger Level : why?

When it comes to logging, you have two essential and opposing forces, if you will. On the one hand, you want to capture every last detail you can because this might prove useful during troubleshooting or auditing your system. But on the other hand, all of that logging consumes resources. You can eat up disk space, overload people reading the logs, and even start to slow down your production code if you go overboard.

Usually, you don't need to know everything the application did. But if he starts malfunctioning, you might want to go back to "granularity 10" to zoom in on the problem as narrowly as possible.Logging levels work this way. Often you'll only want to know if there are problems or warning conditions. But sometimes you'll really want to dial up the information for troubleshooting purposes.

Common Logging Levels

So what are these logging levels? Here are some common ones that you'll see, listed in order from most severe to least severe, and followed up by some that are meta-considerations. When logging from your application, follow these guidelines:

FATAL

Fatal represents truly catastrophic situations, as far as your application is concerned. Your application is about to abort to prevent some kind of corruption or serious problem, if possible. This entry in the log should probably result in someone getting a 3 AM phone call.

ERROR

An error is a serious issue and represents the failure of something important going on in your application. Unlike FATAL, the application itself isn't going down the tubes. Here you've got something like dropped database connections or the inability to access a file or service. This will require someone's attention probably sooner than later, but the application can limp along.

WARN

Now we're getting into the grayer area of hypothetical . You use the WARN log level to indicate that you might have a problem and that you've detected an unusual situation. Maybe you were trying to invoke a service and it failed a couple of times before connecting on an automatic retry. It's unexpected and unusual, but no real harm done, and it's not known whether the issue will persist or recur. Someone should investigate warnings.

INFO

Finally, we can dial down the stress level. INFO messages correspond to normal application behavior and milestones. You probably won't care too much about these entries during normal operations, but they provide the skeleton of what happened. A service started or stopped. You added a new user to the database. That sort of thing.

DEBUG

With DEBUG, you start to include more granular, diagnostic information. Here, you're probably getting into "noisy" territory and furnishing more information than you'd want in normal production situations. You're providing detailed diagnostic information for fellow developers, sysadmins, etc.

TRACE

This is really fine-grained information-finer even than DEBUG. When you're at this level, you're basically looking to capture every detail you possibly can about the application's behavior. This is likely to swamp your resources in production and is seriously diagnostic.

ALL

This is just what it sounds like. Log absolutely everything, including any custom logging levels that someone has defined.

OFF

This is also just what it sounds like. Don't log anything at all.

How This Works ?

At this point, you might be wondering how all this happens between the log file and the application. Well, simply put, there are two participating parties in logging:
  • The logging framework, at runtime, has a configured log level.
  • The application code makes logging requests.
If the framework has a given log level enabled, then all requests at that level or higher priority wind up in the log file. Everything else is denied. So consider the following pseudo-code:
void logAppLogs(int x, int y) {
 log.trace(x);
 log.error(y);
}
If we had the log level set to ALL or TRACE, you would see both integers in the log file. If we changed the log level to, say, WARN, then we would only see y. And if we turned the log level all the way down to FATAL, we would see nothing.

Comments