Register Plugin
<?php
use Bartlett\Reflect;
use Bartlett\Reflect\Plugin\Log\LogPlugin;
// Optional plugin configuration
$opt = array();
$reflect = new Reflect;
$reflect->addSubscriber( new LogPlugin($logger, $opt) );
Where $logger
is an instance of object that must implement interface
Psr\Log\LoggerInterface
(PSR-3).
And $opt
is an array to configure what events and its details you would like to have.
Event | Log Level | Message Template (with/without placeholders) |
---|---|---|
reflect.progess | Psr3\Log\Level\LogLevel::INFO | Parsing file "{file}" in progress. |
reflect.success | Psr3\Log\Level\LogLevel::INFO | AST built. |
reflect.cache | Psr3\Log\Level\LogLevel::INFO | AST built by a previous request. |
reflect.error | Psr3\Log\Level\LogLevel::ERROR | Parser has detected an error on file "{file}". "{error}". |
reflect.complete | Psr3\Log\Level\LogLevel::NOTICE | Parsing data source "{source}" completed. |
For example, if you want to deactivate logging on reflect.success
event, then give the following options :
<?php
$opt = array(
'reflect.success' => false,
);
Or, if you don’t want to have contextual data sent to logger :
<?php
$opt = array(
'reflect.success' => array(
'level' => LogLevel::INFO,
'template' => 'AST built.',
'context' => false,
),
);
Using your private logger
Use your own logger, that must be compatible PSR-3.
<?php
use Bartlett\Reflect;
use Bartlett\Reflect\Plugin\Log\LogPlugin;
use Psr\Log\AbstractLogger;
class YourLogger extends AbstractLogger
{
private $channel;
public function __construct($name = 'YourLoggerChannel')
{
$this->channel = $name;
}
public function log($level, $message, array $context = array())
{
error_log(
sprintf(
'%s.%s: %s',
$this->channel,
strtoupper($level),
$this->interpolate($message, $context)
)
);
}
protected function interpolate($message, array $context = array())
{
// build a replacement array with braces around the context keys
$replace = array();
foreach ($context as $key => $val) {
$replace['{' . $key . '}'] = $val;
}
// interpolate replacement values into the message and return
return strtr($message, $replace);
}
}
// Create the main logger
$logger = new YourLogger('Reflect');
// Optional plugin configuration
$opt = array();
$reflect = new Reflect;
$reflect->addSubscriber( new LogPlugin($logger, $opt) );
Using Monolog
Use one of the most famous logging solution compatible PSR-3.
<?php
use Bartlett\Reflect;
use Bartlett\Reflect\Plugin\Log\LogPlugin;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
// Create some handlers
$stream = new StreamHandler('/var/logs/phpreflect.log');
// Create the main logger
$logger = new Logger('Reflect');
$logger->pushHandler($stream);
// Optional plugin configuration
$opt = array();
$reflect = new Reflect;
$reflect->addSubscriber( new LogPlugin($logger, $opt) );
If you want to use Monolog with Reflect on CLI mode,
then you should use a wrapper like this.
<?php
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
class YourLogger extends Logger
{
public function __construct($name = 'YourLoggerChannel')
{
$stream = new StreamHandler('/var/logs/phpreflect.log');
parent::__construct($name, array($stream));
}
}