15

I want to understand the basics of a logging library.

  1. What exactly is the purpose of a logging library? I understand that a log is basically information about your application process during execution. One way to do it is by writing information in a file directly.
  2. What is the purpose of designing a dedicated library such as glog for logging purposes? Is my understanding of logging correct, or do I need to change it? Can someone give a practical example to exhibit the importance of using a logging library?
  3. What features should one look at while choosing a logging library?
  4. How can logging be effectively employed during implementations?
6
  • 4
    Logging: is anything useful that reduces the pain felt during those 3am debugging sessions. Using a standard library allows you to exploit precedents and design choices taken by experienced authors.
    – Bathsheba
    Aug 18, 2015 at 16:23
  • 4
    Writing consistent, fault tolerant structured data to a single target in a thread safe fashion using efficient io can be non trivial ...
    – Alex K.
    Aug 18, 2015 at 16:30
  • For a beginner it would be helpful to get a more detailed answer or at least a link that explains the issues clearly. Aug 18, 2015 at 16:33
  • 1
    Some libraries come with more features like being able to redirect output to a network port, thus allowing remote logging when deployed on a client machine. Aug 18, 2015 at 16:35
  • @Bathsheba "3am debugging sessions"?What's that?
    – John
    Jan 18, 2022 at 8:13

2 Answers 2

18

Logging information during the execution of your application can help you understand what led to a bug or crash, giving you more context than you get from simply a report of a crash, a call stack or even a minidump. This is particularly important when you are getting bug or crash reports from people who are not developers and are not running under a debugger, either end users / customers or non-developers on your team.

My background is in games and logging can be particularly valuable with games for a few reasons. One is that many issues can relate to the specifics of the hardware on a system so logging information like what kind of GPU the user has, which graphics driver version they are running, etc. can be essential to debugging problems that only show up on a specific configuration. Another is that games have a simulation aspect where the state of the game is evolving over time in response to user input combined with simulation of things like physics, AI and the game rules. Understanding what was going on in the run up to a crash or bug helps figure out how to reproduce it and can give valuable clues to the root cause of the issue.

A logging library adds functionality that is useful for logging and goes beyond what is available from a simple printf. This includes things like:

  • The ability to control the amount of logging based on factors like debug vs. release builds and runtime settings like a -verbose flag.
  • The concept of 'channels' that can be independently enabled, disabled or set to a particular verbosity. For example, to debug a graphics issue you may want the 'graphics' channel set to maximum verbosity while muting the 'network' and 'audio' channels.
  • A flexible back end ranging from logging to a local file on disk to logging to a remote database over a network.
  • Thread safety so that logging behaves itself when potentially logging simultaneously from multiple different threads.
  • Automatic tagging of log entries with a timestamp and any other relevant information (channel, verbosity level, etc.).

As for how to make use of a logging library, that is somewhat dependent on your application, but here's some general suggestions:

  • Make good use of channels and verbosity levels if your logging library provides them (and it should). This will help you manage what can become a very large volume of log messages as your application grows.
  • If you encounter an unexpected but non-fatal condition and handle it, log some information about it in case it leads to unforeseen problems later on.
  • On application startup, log any information that might come in useful for reproducing rare errors later if you receive a bug or crash report from a customer. Err on the side of too much information, you never know what might be useful in advance. This might include things like CPU type, GPU model and driver version, available memory, OS version, available hard drive space, etc.
  • Log key state transitions so you can track what state your application was in and how it got there when you are debugging an issue.
5
  • 1
    This is very helpful. Thanks a ton. Aug 18, 2015 at 17:07
  • This is an excellent answer, but @UjjwalAryan, you should also learn how to use pdb files when debugging.
    – Bathsheba
    Aug 19, 2015 at 6:48
  • Yeah. I use GNU debugger. But I was reading source code of another open-source project and I came across the use of glog library there which has been used extensively. I could not find any information online about the use of such libraries and how they can be efficiently employed. So, I asked the question. I do not know why this has been flagged as too broad. There are no resources online about it and unfortunately most of budding coders face issues with such basic questions. Aug 19, 2015 at 12:58
  • @mattnewport "If you encounter an unexpected but non-fatal condition and handle it, log some information about it in case it leads to unforeseen problems later on." Could you please explain that in more detail for me?
    – John
    Jan 18, 2022 at 8:17
  • @John it is reasonably common in my experience for unusual / edge cases to not be as well tested as common cases. Sometimes some code may be written that tries to deal with an unexpected situation but that code path is rarely executed and not well tested. In these type of situations there may be code later on that makes assumptions that are not valid in this uncommon scenario and fail. Those failures may be easier to debug if you have previously logged some info about the unexpected scenario, even though you tried to handle it at that point. Jan 20, 2022 at 18:57
3

A lot of programs use some sort of logging, and there is little point to re-inventing the wheel every time, even if the code is relatively simple.

Other libraries can use the logging library too, so instead of having to configure the log files for each library you include in a project, you can just configure the one logging library. This also means that any bugs that might appear in the logging code can be fixed by just replacing the one library instead of having to replace multiple libraries.

Finally, it makes code easier to read for other developers because they don't have to figure out how you implemented your custom logging.

Not the answer you're looking for? Browse other questions tagged or ask your own question.