views:

690

answers:

8

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?
+4  A: 

In the first phases of component development log everything. Usually I do all of my development on the console that way I can see all of my logic being output line by line if need be. As you move forward logging errors is always a must and logging values after complex data manipulation can be helpful as well. Think of military disasters due to rounding floats off to soon.

I recommend logging to a flat file over the database.

Woot4Moo
"I recommend logging to a flat file over the database."I'd only use either of these methods as a last resort. STDERR and SYSLOG should be the first ports of call. Using a flat file can cause a lot of complications when there is more than one program trying to write to it....and the level of logging should be configurable at run time (from a config file or start up options).C.
symcbean
At the individual component level this should not be an issue. Plus logging to a flat file gives you the ability to do simple ctrl+f to find things that and I haven't run into that issue with a simple file stream being open. Databases grow astronomically in practice.
Woot4Moo
Haven't had a problem with flat files either, you just need to make sure that EVERY app (or instance of your app) has it's own log file.
Peter Schuetze
I would definitely avoid multiple processes writing to the same file. The problem with logging to stdout/stderr over files is that stdout/stderr are *transient*
Brian Agnew
+11  A: 

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.

Brian Agnew
"removed post development, and this never occurs" - lucky you. My previous customers used the logging settings extensively. For one it was needed since the whole installation was writing more than 10 GB logs per day. ;) We decided it was too much debug info for the production environment and gave him a new configuration file for his logging needs.
Peter Schuetze
Yes. I *have* canned logging in the past when buying a disk drive manufacturing company was the only other option :-)
Brian Agnew
+1  A: 

Definetly log errors to flat files consistently formatted so that you can retreive them from your customer and read them into excel or a db for analysis if needed.

fupsduck
+6  A: 

Logging is essential and useful. Why? For tracing purposes. As long as you develop on a local machine, you can quickly debug, set breakpoints and figure out where things go wrong. Once you then deploy in production your customer will phone you, telling that he didn't see what he expected to (an error message appeared somewhere). Trust me, you will have a hard time figuring out what happened if you don't have logs.

Hmm...Brian's answer just popped in saying basically where I wanted to continue :)

Anyway, you may consider also some Aspect Oriented techniques. They're pretty suited for logging purposes and you keep your code clean. May be an option for your project.

Regarding the type of logging: I usually work just fine with plain text files (with a certain max size), unless you have to keep track about all of your user's activities (legal reasons, whatever). In such a case a DB log would probably be more appropriate.

Juri
Sorry, Juri! (15 char padding)
Brian Agnew
+1  A: 

Logging is always essential, because there may be errors that occur in production and information needs to be output about what happened.

Apart from errors, however, whether you log key entry and exit points, possible time consuming methods/operations etc. is dependent on the applicatoin and environment. If you don't have performance analysis built into your system, then you either need to log, or be able to turn on detailed logging, in order to track down performance issues.

Similarly, if you don't have real-time debugging tools, you need to log, or be able to turn on logging, for key entry/exit points.

If the system is handling millions of transactions, each with millions of operations, then you don't want to have this level of logging on all the time on every subsystem/process, otherwise your application quickly fills up all available diskspace - that's an issue.

For small, simple systems, it is probably sufficient to have a general default level of logging for production, and a debug level for tracking down problems.

Larry Watanabe
"If the system is handling millions of transactions," - I used rolling log files. However, the end user needs to be aware of it to capture the log files right after the error occurred. My customer had 30 to 40 minutes to copy the log files, before they were overwritten. This was a pretty short time span considering it was a client server architecture and a big company. (someone needed to tell the admin to capture the logs)
Peter Schuetze
+2  A: 

This is a very general question... For starters, I'd say you should have several logging levels. On the trace level, log everything you have in mind. On the errors level, have only sever errors logged. Make it possible to choose logging level on runtime, though the selection should not be available for end users. Also, have in mind that if you make your logs available to people other than you (your support team, for example), make sure your messages are human readable and not something only you can understand.

There are a lot of logging infrastructures, look around to see what fits your development environment. It might be easier to use a well tested infrastructure than invent something of your own.

eran
+2  A: 
  • It's good to log actions or things that happend in programs(like errors)?

Yes, you always need to have a way to find our whats wrong. You should also classify them, so you can later turn off logging for a certain class of log messages (e.g. while developing you want to have debug messages; after you shipped your app, you only care for errors and warnings).

  • In my case, it's good to log things?

see above.

      o What I need to log?
  • Every Error (app/function does not work)
  • Every Warning (app/function still work, but it is worth keeping a record of it)
  • Information (something nice to have)
  • Debug (stuff that only a developer cares about)

With ever class you should also log relevant data.

  • What is the best way to log things(text files, databases...)?

I prefer files, since they are easy to read and can be shared easily with others. But as long as you can access the logs the medium is the smaller problem.

I would recommend to use a logging framework, since logging is an essential part of your app and way to many people already came up with pretty good solutions. This way you don't invent the wheel over and over again and you have more time developing your app.

Peter Schuetze
The other benefit of the various logging frameworks is you can easily subscribe/filter the traces in various ways. So maybe you want to log *everything* to a database, but you want to send an e-mail on critical errors. You can do that. :)
GalacticCowboy
Good point I forgot t mention. Instead of figuring out how to sent emails or write to a system log/central logging server you just need to change the configuration for your framework. That leaves you more time to develop the app.
Peter Schuetze
A real-world example: I worked on an app a while back where the original dev decided to log exceptions in the Windows event log. Which was fine, until we deployed to a host that didn't allow access to the event log, so every exception resulted in a subsequent exception that the original exception couldn't be logged. Had he used a logging framework, the configuration would have managed this. As it was, it took a lot of rework...
GalacticCowboy
+1  A: 

Logging during development doesn't really matter--do it if it helps; stop doing it when it stops helping.

Good logs become important during production. List the kinds of problems you'll run into and log the information you need to solve those problems. Will you be sued for copyright infringement? Then log what you need to defend yourself. Will people report bugs with their readers and ask for help? Then log what you need to answer the questions and minimize your support costs.

It's easy to log too much (useless) information. If you don't need it, don't log it. Will servers fail? Will networks fail? Will you run out of storage? Yes, and you can log to help diagnose those problems, but that's mainly a job for monitoring, not logging.

Syslog provides pretty good management and centralized control. There are many logging frameworks that work about the same--it's really personal preference which one you use. I use a structured, searchable log format on top of syslog. All ad hoc development logging is removed before going live.

Ken Fox