How to use Twitter as an error log

Sure this has been done before, but I had a brainwave today – why not use Twitter as an error log for web apps?

I already have error handling on my functions, so surely this shouldn’t be a difficult addition… turns out it’s not.

First, you’ll need a twitter account. I’d recommend setting one up, then protecting the updates. Keep the username and password, you’ll need them in a minute. I’d then follow the new account you set up, as well as anyone else on your team.

Then, define a function as follows in PHP, preferably in a global include or some such thing.

function tweet_error ($error, $description) {
	$username = 'yourusername';
	$password = 'yourpassword';
	$status = "#$error - $description";
 
	$update_url = 'http://www.twitter.com/statuses/update.xml'; // http://identi.ca/api/statuses/update.xml will use identi.ca instead.
 
	$curl = curl_init();
	curl_setopt($curl, CURLOPT_URL, "$update_url");
	curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2);
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($curl, CURLOPT_POST, 1);
	curl_setopt($curl, CURLOPT_POSTFIELDS, "status=$status");
	curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
 
	$result = curl_exec($curl);
	$resultArray = curl_getinfo($curl);
 
	curl_close($curl);
 
	return ($resultArray['http_code'] == 200);
}

My function here takes two parameters, the error code as well as a description. You could generalise this and rejig it a bit, as well as perhaps changing the $status variable to look different.

For instance,

$status = "d username #$error - $description";

or

$status = "@username #$error - $description";

to send it to you. You could even define different usernames for different types of errors, if you are in a team.

In your code, then call it like this:

tweet_error ("404", $_SERVER[SCRIPT_URL]);

to log a 404, as well as the page it came from, or:

tweet_error ("DB-Connect", "Failed");

In your database connection script, or even:

tweet_error ("Possible spam", "From user XXXX");

in your comment/email form handling.

Hope this comes in useful!

  • Share/Bookmark

Random Posts

This entry was posted in PHP, tips. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.
  • AFAIK, Twitter API have the limit of calls per hour. Are they big enough?
  • I'm only using it for mission critical errors, so hopefully!
  • johannilsson
    Nice idea, Just did this logger using zf components. http://gist.github.com/82394
  • did anybody test it. i believe a direct message "d" can´t be send with this script
  • There is another API method for directs, though I found it worked fine using a d .
  • Tim
    It's a nice idea, but I don't see the benefit over other notification methods, such as email to a distribution list. Email is just as instantaneous, can be sent to any number of people, is more controlled and can get more detailed feedback. Why would we want to use Twitter, apart from the novelty?
  • me
    how useful can a 140 character error really be?
  • Sorry, didn't notice the other comments.

    Even if the thing is private, it is still on server I don't control ...
    Thanks, but thanks no
  • Sounds dangerous to me. Is such a twitter feed private? Although they shouldn't error logs are prone to leeking sensitive information. It's bad enough if those show up in log files or e-mails, but I don't want to find them on twitter ... at least not from my applications ;-)
  • hehe cool :)
  • I was just told that you can declare a twitter feed as private, so just forget my comment ;)

    Nice idea definitely
  • Yeah - you'd want the twitter account to be private for good reason. Been using this for a few days now, and it's really good - going to set it up as a matter of course from now on.
  • I don't really know if it's that good if the whole world can read your error log?
blog comments powered by Disqus