2013-06-25 17:06:37 +00:00
|
|
|
<?php
|
2018-05-25 07:26:54 +00:00
|
|
|
|
2013-06-25 17:06:37 +00:00
|
|
|
/**
|
|
|
|
* Resque default logger PSR-3 compliant
|
|
|
|
*
|
2018-05-25 07:26:54 +00:00
|
|
|
* @package Resque/Stat
|
|
|
|
* @author Chris Boulton <chris@bigcommerce.com>
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php
|
2013-06-25 17:06:37 +00:00
|
|
|
*/
|
2018-05-25 07:26:54 +00:00
|
|
|
|
|
|
|
class Resque_Log extends Psr\Log\AbstractLogger
|
2013-06-25 17:06:37 +00:00
|
|
|
{
|
2018-05-29 10:20:56 +00:00
|
|
|
public $logLevel;
|
2018-05-25 07:26:54 +00:00
|
|
|
|
2018-05-29 10:20:56 +00:00
|
|
|
public function __construct($logLevel = "warning")
|
2018-05-25 07:26:54 +00:00
|
|
|
{
|
2018-05-29 10:20:56 +00:00
|
|
|
$this->logLevel = strtolower($logLevel);
|
2018-05-25 07:26:54 +00:00
|
|
|
}
|
2013-06-25 17:06:37 +00:00
|
|
|
|
2018-05-25 07:26:54 +00:00
|
|
|
/**
|
|
|
|
* Logs with an arbitrary level.
|
|
|
|
*
|
|
|
|
* @param mixed $level PSR-3 log level constant, or equivalent string
|
|
|
|
* @param string $message Message to log, may contain a { placeholder }
|
|
|
|
* @param array $context Variables to replace { placeholder }
|
|
|
|
* @return null
|
|
|
|
*/
|
2018-05-29 10:20:56 +00:00
|
|
|
public function log($level, $message, array $context = [])
|
2018-05-25 07:26:54 +00:00
|
|
|
{
|
2018-05-29 10:20:56 +00:00
|
|
|
$logLevels = ["emergency", "alert", "critical", "error", "warning", "notice", "info", "debug"];
|
2018-06-22 08:13:16 +00:00
|
|
|
/**
|
|
|
|
* Only log things with a higher rating than the current log level.
|
|
|
|
* e.g If set as 'alert' will only alert for 'emergency' and 'alert' logs.
|
|
|
|
*/
|
2018-05-29 10:20:56 +00:00
|
|
|
if (array_search($level, $logLevels) <= array_search($this->logLevel, $logLevels)) {
|
2018-05-25 07:26:54 +00:00
|
|
|
fwrite(
|
|
|
|
STDOUT,
|
2018-05-29 10:20:56 +00:00
|
|
|
'[' . $level . '][' . strftime('%T %Y-%m-%d') . '] ' . $this->interpolate($message, $context) . PHP_EOL
|
2018-05-25 07:26:54 +00:00
|
|
|
);
|
|
|
|
}
|
2018-05-29 10:20:56 +00:00
|
|
|
return;
|
2013-06-25 17:06:37 +00:00
|
|
|
|
2018-05-25 07:26:54 +00:00
|
|
|
}
|
2013-06-25 17:06:37 +00:00
|
|
|
|
2018-05-25 07:26:54 +00:00
|
|
|
/**
|
|
|
|
* Fill placeholders with the provided context
|
|
|
|
* @author Jordi Boggiano j.boggiano@seld.be
|
|
|
|
*
|
|
|
|
* @param string $message Message to be logged
|
|
|
|
* @param array $context Array of variables to use in message
|
|
|
|
* @return string
|
|
|
|
*/
|
2018-05-29 10:20:56 +00:00
|
|
|
public function interpolate($message, array $context = [])
|
2018-05-25 07:26:54 +00:00
|
|
|
{
|
|
|
|
// build a replacement array with braces around the context keys
|
2018-05-29 10:20:56 +00:00
|
|
|
$replace = [];
|
2018-05-25 07:26:54 +00:00
|
|
|
foreach ($context as $key => $val) {
|
|
|
|
$replace['{' . $key . '}'] = $val;
|
|
|
|
}
|
2013-06-25 17:06:37 +00:00
|
|
|
|
2018-05-25 07:26:54 +00:00
|
|
|
// interpolate replacement values into the message and return
|
|
|
|
return strtr($message, $replace);
|
|
|
|
}
|
2013-06-25 17:06:37 +00:00
|
|
|
}
|