Earlier today I can across this quote (via this tweet):
The most effective debugging techniques seem to be those that are designed and built into the program itself – many of today’s best programmers will devote nearly half of their programs to facilitating the debugging process in the other half; the first half, which usually consists of fairly straight forward routines that display relevant information in a readable format, will eventually be thrown away, but the net result is a surprising gain in productivity.
- The Art of Computer Programming Volume 1, Donald E. Knuth
One ubiquitous debugging tool is the log file. It is used to find and diagnose problems both during development and in production. At my present employer, Volumental, we have been using Googles logging library GLOG for this. GLOG writes a log file to disk, but can also be configured to write colored output to stderr. The log looks like this in the terminal:
As you can see it prefixes each line with a preamble of useful information. Personally, I find this cryptic and hard to read. Also, many things are missing. Most importantly the current date is not part of the the preamble, nor is the verbosity level. So I went ahead and created something better! Introducing: Loguru! Here's what the output of Loguru looks like:
Ah, isn't that a breath of fresh air? Note how the message stands out of the preamble, and how the higher verbosity levels (the last column in the preamble) have a slightly muted color. Loguru also features something I have not seen in any other logging library before: indentation. The scope above was created with a single line of code:
LOG_SCOPE_F(INFO, "complex_calculation");. The indentation extends to the end of the current scope and affects all threads. Indentation greatly improves the readability of long log files.
Loguru can do most of what GLOG can do, and can in fact be configured to use the same syntax (
LOG(INFO) << "Msg") as GLOG. So if you are using GLOG already, switching to Loguru should be a breeze! But Loguru can also do much, much more. Here's a few things in particular:
- You can hook up a callback in Loguru that is called on each log line. This can for instance be used to show warnings and errors in a debug UI (like an in-game console for you game-devs).
- The logs contain useful things like the thread name and uptime.
- The logs contain the verbosity level so you can grep on it after the fact for increased readability. (The verbosity is a single digit number you can supply when logging a line.)
- You can have more than one log file. One could be called
readable.logand contain only the most important messages, and one could be called
everything.logand contain everything.
- Scoped indentation.
- Much, much more.
So, give Loguru and try and report any bugs, issues and feature requests back to me! You can find it on GitHub at https://github.com/emilk/loguru.