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.
I was wondering the same not too long ago ;) thnx
Alternatively a StreamHandler can be used: new StreamHandler(“php://stdout”, $level);
Or if preferred you can use php://stderr
Thanks for this post, and 3onyc – thanks for the refinement.
Saved me a lot of searching thanks for the info!
Can anyone please explain me: where would I find a log message sent to STDERR?
For instance, in Symfony, in Monolog configuration, I have:
[code]
monolog:
handlers:
main:
type: fingers_crossed
action_level: error
handler: nested
excluded_http_codes: [404, 405]
nested:
type: stream
path: “php://stderr”
level: debug
[/code]
If I want to check logged messages, where would I look?
Thank you