Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To Log Or To Not Log?

I'm now building a program that is a eBook manager, reader, organizer and publisher, that is also a eBook transfer(to eReaders like Kindle), but I was developing it and a question have poped-up on my mind: "Log or not?"

Then I started to think about logging. As many programs log actions, I started to seek for they and see how they log things, then I want to know:

  • It's good to log actions or things that happend in programs(like errors)?
  • In my case, it's good to log things?
    • What I need to log?
  • What is the best way to log things(text files, databases...)?
  • There is any tool to logging for Lazarus?
like image 496
Nathan Campos Avatar asked Dec 29 '09 19:12

Nathan Campos


1 Answers

It's essential. Log

  1. all errors with related data (method args etc.). Otherwise you're going to have a range of bug-related behaviours that you can't analyse.
  2. key entry and exit points (what's your program doing? Should it be calling these methods at this point?)
  3. possible time consuming methods/operations (entry and exit), so you can measure what's going on and know what your program is doing/where it is.
  4. where you're loading configurations from, if applicable. This means you can tell how your program is configuring itself (which config is it using?)

If you get this right, you may find you spend less and less time in the debugger.

I would err on logging more rather than little, and removing or filtering if/when this becomes a problem (as noted below, logging Gbs per day is probably counterproductive). I've worked on numerous systems where it's been decreed that logging will be turned down or removed post development, and this never occurs, since it's so useful. Some people object to logging on the basis that it affects performance. If this is the case, remove it when it's known to be a problem, not before.

You should be able to find suitable frameworks for logging (e.g. log4c for C, log4j for Java) for your particular platform that allow appropriate filtering and destination selection. That means you can log to files (and restrict log sizes), databases, remote monitors etc. and change this decision on the fly (via a configuration file or command-line arguments). The right framework should require very little initially other than your inserting appropriate logging statements. You shouldn't have to write much in the way of file management or other log management code.

Here's a useful blog entry on this subject, with further guidelines.

like image 135
Brian Agnew Avatar answered Sep 20 '22 16:09

Brian Agnew