jbroadway/analog

Send log to multiple separate handlers (mongo & slack)?

tseven opened this issue · 1 comments

It appears I can send all ERRORS to one handler (Slack) and all DEBUG to Mongodb using the multi handler, but not to both simultaneously.

Ultimately, I'm looking for the ability to choose which handler is used during the log call.

In other logging libraries you can simply instantiate two versions of class, one for each destination like:

$mongo_logger = new Logger('mongo', 'db', 'info');
$slack_logger = new Logger('slack', 'api', 'info');

$mongo_logger->log('error', 'this is an error');
$slack_logger->log('error', 'this is an error');

When I try to do the same in Analog, all the log messages use the last handler set:

require 'vendor/autoload.php';
$_logger_mongo = new \Analog\Logger();
$_logger_mongo->handler(Analog\Handler\Mongo::init(
  MONGODB_HOST,
  MONGODB_LOG_DB_NAME,
  MONGODB_LOG_COLLECTION_NAME
));
$_logger_slack = new \Analog\Logger();
$_logger_slack->handler(Analog\Handler\Slackbot::init('team', 'token', 'channel'));

$_logger_slack->debug(['message' => 'Debug info for slack', 'data' => [1, 4, 6]]);
$_logger_mongo->debug(['message' => 'Debug info for mongo', 'data' => [1, 4, 6]]);

Both messages are sent to slack.

What am I doing wrong?

I'm using "dev-master" inside my composer.json

Thanks!

lux commented

Apologies for only seeing this now. I'm pretty sure that you can use an array for each level to log to multiple handlers at once. There's an example in the Multi handler's comments here:

https://github.com/jbroadway/analog/blob/master/lib/Analog/Handler/Multi.php

Hope that helps!