<p>I've found out how to convert errors into exceptions, and I display them nicely if they aren't caught, but I don't know how to log them in a useful way. Simply writing them to a file won't be useful, will it? And would you risk accessing a database, when you don't know what caused the exception yet?</p>

[{'answer_id': 20354, 'author': 'Ross', 'author_id': 2025, 'author_profile': 'https://Stackoverflow.com/users/2025', 'pm_score': 4, 'selected': False, 'text': '<p>You could use <a href="http://www.php.net/manual/en/function.set-error-handler.php" rel="noreferrer">set_error_handler</a> to set a custom exception to log your errors. I\'d personally consider storing them in the database as the default Exception handler\'s backtrace can provide information on what caused it - this of course won\'t be possible if the database handler triggered the exception however.</p>\n<p>You could also use <a href="http://www.php.net/manual/en/function.error-log.php" rel="noreferrer">error_log</a> to log your errors. It has a choice of message destinations including:</p>\n<blockquote>\n<p>Quoted from <a href="http://www.php.net/manual/en/function.error-log.php" rel="noreferrer">error_log</a></p>\n<ol>\n<li>PHP\'s system logger, using the Operating System\'s system logging mechanism or a file, depending on what the error_log  configuration directive is set to. This is the default option.</li>\n<li>Sent by email to the address in the destination  parameter. This is the only message type where the fourth parameter, extra_headers is used.</li>\n<li>Appended to the file destination . A newline is not automatically added to the end of the message  string.</li>\n</ol>\n</blockquote>\n<p><em>Edit: Does markdown have a noparse tag for underscores?</em></p>\n'}, {'answer_id': 20364, 'author': 'Tom Mayfield', 'author_id': 2314, 'author_profile': 'https://Stackoverflow.com/users/2314', 'pm_score': 4, 'selected': True, 'text': '<p>I really like <a href="http://logging.apache.org/log4php/" rel="nofollow noreferrer">log4php</a> for logging, even though it\'s not yet out of the incubator.  I use log4net in just about everything, and have found the style quite natural for me.</p>\n\n<p>With regard to system crashes, you can log the error to multiple destinations (e.g., have appenders whose threshold is CRITICAL or ERROR that only come into play when things go wrong).  I\'m not sure how fail-safe the existing appenders are--if the database is down, how does that appender fail?--but you could quite easily write your own appender that will fail gracefully if it\'s unable to log.</p>\n'}, {'answer_id': 20371, 'author': 'Polsonby', 'author_id': 137, 'author_profile': 'https://Stackoverflow.com/users/137', 'pm_score': 2, 'selected': False, 'text': '<blockquote>\n  <blockquote>\n    <p>Simply writing them to a file won\'t be useful, will it?</p>\n  </blockquote>\n</blockquote>\n\n<p>But of course it is - that\'s a great thing to do, much better than displaying them on the screen. You want to show the user a nice screen which says "Sorry, we goofed. Engineers have been notified. Go back and try again" and ABSOLUTELY NO TECHNICAL DETAIL, because to do so would be a security risk. You can send an email to a shared mailbox and log the exception to file or DB for review later. This would be a best-practice.</p>\n'}, {'answer_id': 20381, 'author': 'crono', 'author_id': 1462, 'author_profile': 'https://Stackoverflow.com/users/1462', 'pm_score': 0, 'selected': False, 'text': '<p>I think it depends a lot of where your error occured. If the DB is down logging it to the DB is no good idea ;)</p>\n\n<p>I use the <a href="http://php.net/syslog" rel="nofollow noreferrer">syslog()</a> function for logging the error, but I have no problems writing it to a file when I\'m on a system which has no syslog-support. You can easily set up your system to send you an email or a jabber message using for example <a href="http://www.logwatch.org/" rel="nofollow noreferrer">logwatch</a> or <a href="http://www.johnandcailin.com/blog/john/how-setup-real-time-email-notification-critical-syslog-events" rel="nofollow noreferrer">the standard syslogd</a>.</p>\n'}, {'answer_id': 64450, 'author': 'Alister Bulman', 'author_id': 6216, 'author_profile': 'https://Stackoverflow.com/users/6216', 'pm_score': 1, 'selected': False, 'text': "<p>I'd write them to a file - and maybe set a monitoring system up to check for changes to the filesize or last-modified date.  Webmin is one easy way, but there are more complete software solutions.</p>\n\n<p>If you know its a one-off error, then emailing a notice can be fine.  However, with a many hits per minute website, do not <em>ever</em> email a notification.  I've seen a website brought down by having hundreds of emails per minute being generated to say that the system could not connect to the database. The fact that it also had a LoadAvg of > 200 because of of the mail server being run for every new message, did not help at all.  In that instance - the best scenario was, by far and away, the watchdog checking for filesizes and connecting to an external service to send an SMS (maybe an IM), or having an external system look on a webpage for an error message (which doesn't have to be visible on screen - it can be in a HTML comment).</p>\n"}, {'answer_id': 67703, 'author': 'Gary Richardson', 'author_id': 2506, 'author_profile': 'https://Stackoverflow.com/users/2506', 'pm_score': 0, 'selected': False, 'text': '<p>I second log4php. I typically have it configured to send things like exceptions to ERROR or CRITITCAL and have them written to syslog. From there, you can have your syslog feed into <a href="http://www.zenoss.com/" rel="nofollow noreferrer">Zenoss</a>, <a href="http://www.nagios.org/" rel="nofollow noreferrer">Nagios</a>, <a href="http://www.splunk.com/" rel="nofollow noreferrer">Splunk</a> or anything else that syslog can talk to.</p>\n'}, {'answer_id': 1753032, 'author': 'Phyxx', 'author_id': 157605, 'author_profile': 'https://Stackoverflow.com/users/157605', 'pm_score': 0, 'selected': False, 'text': '<p>You can also catch and record PHP exceptions using Google Forms. There is a tutorial <a href="http://www.brighthub.com/hubfolio/matthew-casperson/articles/56351.aspx" rel="nofollow noreferrer">here</a> that explains the process.</p>\n'}]
['https://Stackoverflow.com/questions/20322', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1585/']