Log message style guide

One of the fit-and-finish issues all systems developers need to address is the text of log messages. This is a log message style guide.

Once your product ships, it will be in customers’ hands. They will run it on their machines. You won’t be around, and neither will your debugger. Log messages could be a big part of your customer’s experience with your product, especially when something goes wrong. They could also be a big part of your experience with fixing problems at customer sites.

Be professional

These log messages will be some poor customer’s first view of you after a problem occurs. Make sure the messages make the customer feel better about the product, not worse. Part of it is writing professional English (or whatever language), and part is having the right attitude in the text.

Simply being consistent in your log messages will go a long way toward making them seem professional and authoritative. Below I give guidelines for wording, punctuation, and so on. Even if you don’t use these guidelines, pick some rules, and follow them.

Think like a support staffer

As a developer, remember that you will be debugging problems with only these log messages to go by. Something will go wrong at a customer site, and you will be sent a log session (more than likely incomplete). You will not have a debugger, you will not have access to the machine. You will have your log messages. Make sure they will be helpful to you.

Use a real log

Many programming environments provide logging facilities. If you have one, use it. If you don’t have one, search the web for one you can use. If you can’t find one, seriously consider spending two days to write one. They provide useful features like timestamping, run-time selection of logging detail and facilities, options for where to log (system console, log file, system event log, etc).

Python has the logging module described in PEP 282, and now standard as of Python 2.3. Java has Apache’s log4j. An example C++ facility is available from CodeProject.

Use your log now

As developers, we have a rich environment in which to experience our code: the debugger. When a problem occurs, our first impulse is to fix it: dive into the debugger, understand what’s happening, fix the code, recompile, no more problem. Except there could still be a problem: what if the same bug had happened at a customer site? Suppose you had to fix the bug with only the log messages to guide you? Would you have been able to?

Next time a bug appears, don’t jump into the debugger. Read the log. What did it tell you? Think hard about what information could have been presented there but was not. How could you make it more useful?

The first thing you should do is change the log to provide the information (ubiquitous stringification can help a great deal here). Run the code again (with the bug). Make sure that the log really is better. Then you can go ahead and fix the bug.

Of course, it is impossible to make logs rich enough to be a complete replacement for a debugger. And maybe you don’t want to put effort into improving your log. But using the log now will at least give you a sense of what your customers and support people (and likely, you) will be experiencing when the product is in the field.

See also


You've certainly addressed the most important part of log messages: the content. But if you're offering a style guide, there are other aspects that help the maintainer and supporter of the software. For example:

1. Metadata: Severity, type of message, location in the code and in the functionality.

2. Format: Fixed column widths and closed lists of values so that a standard or custom log viewer has an easy time to display, search, group, filter.
Along with Tom, I'd add the proviso that Every message should be unique. Much easier to identify problem code.

Add a comment:

Ignore this:
Leave this empty:
Name is required. Either email or web are required. Email won't be displayed and I won't spam you. Your web site won't be indexed by search engines.
Don't put anything here:
Leave this empty:
Comment text is Markdown.