Is there a recommended way to log (failed) cron actions from your plugin? For example, I have a plugin that synchronizes with an external service every hour. I want to log how much was changed, but also when the synchronization failed. What would you recommend here? A new database table? The Log Deprecated Notices plugin does this with a custom post type, but this might be too much overhead? I believe WordPress does not come with a standard logging package?
Leave a Reply
You must be logged in to post a comment.
Use a file to write the events to. There are several drawbacks here;
Use a database table. Create your own database table and log the events there. None of the drawbacks listed above apply. It doesn’t add a lot of complexity. Automate the creation and deletion of the table using a plugin hook and you have a reasonably reliable log system I think.
Send an e-mail. Depending on the relevance of the logs, you could always opt for this method. I figure you’d probably want to send an e-mail in case of a failure anyway.
Use the OS logger. When you use the syslog function in PHP, you can write events to the system logger (syslog on Unix, Event Log on Windows NT). Configuring a user defined syslog complicates deployment.
Finally; you should consider if you want to log the normal case and exceptions, or only exceptions. Jeff Atwood did a good writeup about this.
Hope this helps!
Use a logging function like this, so that it writes your log out to debug.log by configuring your wp-config.php with the following which I found here:
This should work for debugging/development purposes at least (I’m not sure how great it would be for production logging, but it works for development.)
I agree, putting this data in
wp_posts
could potentially make the table grow quickly to an unmanageable size. It’s fine to create a table. I would go the extra mile and give someone a way to cleanup the log table in the event they stop using their plugin.I’m not afraid of plugins that create tables, but maybe that’s because I’ve seen WordPress databases with 8,000,000
wp_term_relationships
records and 300,000 posts and I know how unpleasant that experience can be.dbDelta()
andregister_activation_hook()
will be your friends, here. See Creating Tables with Plugins.