Mark and igor have good suggestions. I would also advice reading on OWASP about error handling and logging.
One more thing, whatever you log be sure to sanitize logged information that comes from the user, such as url referrer, get/post variables, usernames, etc. In other words anything that can be manipulated by the remote user. You probably don want to end up logging a username that is 100,000 characters long.