Logging to Stdout with Monolog

My worker scripts have really basic logging (as in, they echo when something happens, and I can see those in the supervisord logs). Which is kind of okay, but I wanted to at least add timestamps in to them, and maybe send emails when something REALLY bad happened.

I’m a huge fan of Monolog so I grabbed that, but it wasn’t immediately obvious which of the many and varied options I would need for this fairly simple addition. It turns out that the right thing to use is the ErrorLogHandler, so now my code looks like this:

<?php

require "vendor/autoload.php";

$logger = new \Monolog\Logger("log");
$logger->pushHandler(new \Monolog\Handler\ErrorLogHandler());

$logger->addInfo("Something happened");

And the output is neatly in lines like this:

[2014-06-05 17:00:47] log.INFO: Something happened [] []

You can do a lot of different things with Monolog, but since I had to look up this simplest of all options, I thought I’d share.

Beanstalk, Pheanstalk and Priorities

I’ve got an application that uses Beanstalkd to queue up messages, and some PHP worker scripts that grab messages from the queue and process them. Messages get added by the web application, but can also be added by cron – and when I add a bunch of messages via cron, I don’t want to swamp what the web application is doing! Those cron-added jobs are mostly pretty low priority, generating reports, sending weekly update emails, that kind of thing. Beanstalkd has a concept of priority, so I can create lower priority jobs by using code like this:

<?php

define("LOW_PRIORITY", 2048);  // default is 1024 

$queue =  new Pheanstalk_Pheanstalk($config&#91;'beanstalkd'&#93;&#91;'host'&#93; . ":" . $config&#91;'beanstalkd'&#93;&#91;'port'&#93;);

$queue->useTube("scorem")->put(json_encode(array("action" => "my_important_task")), LOW_PRIORITY);

This will add the job to the queue, but anything with a higher priority value (where 1 is the highest priority!) will take precendence. This way I can add as many non-urgent jobs as I want to to the queue without impacting my website performance. My setup has multiple workers and also I tend to write a script that puts loads of tiny jobs on the queue rather than putting one monster task on there. I find this approach a bit more fault-tolerant and also means that incoming tasks can get a chance to get serviced rather than waiting for some crazy huge thing to finish.

I had real issues finding information about the priority settings for beanstalkd and PHP, so hopefully if anyone is looking for it, they will find this post :)