user1615069
user1615069

Reputation: 633

How to and what to log in PHP

Well...I do know it's good to log errors when the website is in production and error reporting is off. But what should I actually log? I can't log all errors, so I should to choose some most errors-probable places in the code and call the function with some message to log this. Should I use try-catch to do this? And is it actually the best way to do this?

Upvotes: 0

Views: 95

Answers (1)

deceze
deceze

Reputation: 522041

You should have several levels of error severity, all of which are handled differently:

  • debug level messages are just for debugging, e.g. very verbosely outputting information about everything that's happening
  • info level messages inform about important events, like "trying to contact web service"
  • notices are not unusual, but noteworthy events
  • warnings are problems which need to be corrected, but do not cause immediate problems
  • errors should be logged in detail and monitored
  • critical errors are unexpected emergencies and should be logged in detail and possibly emailed automatically
  • alerts require immediate attention and should trigger automated SMS notifications
  • emergencies should automatically have the national guard deploy

You intersperse your app with all these different types of errors/events. Notices and up should be logged, errors and up should be logged in detail (dump the system status into a separate file) and for critical errors and up somebody should be automatically notified.

See https://github.com/Seldaek/monolog for a good component which can help you do this.

Upvotes: 5

Related Questions