mirror of
https://github.com/Seldaek/monolog.git
synced 2025-08-07 21:56:31 +02:00
Add caching, group exec calls in one, refs #274
This commit is contained in:
@@ -11,42 +11,54 @@
|
|||||||
|
|
||||||
namespace Monolog\Processor;
|
namespace Monolog\Processor;
|
||||||
|
|
||||||
|
use Monolog\Logger;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Injects Git branch and Git commit SHA in all records
|
* Injects Git branch and Git commit SHA in all records
|
||||||
*
|
*
|
||||||
* @author Nick Otter
|
* @author Nick Otter
|
||||||
|
* @author Jordi Boggiano <j.boggiano@seld.be>
|
||||||
*/
|
*/
|
||||||
class GitProcessor
|
class GitProcessor
|
||||||
{
|
{
|
||||||
|
private $level;
|
||||||
|
private static $cache;
|
||||||
|
|
||||||
|
public function __construct($level = Logger::DEBUG)
|
||||||
|
{
|
||||||
|
$this->level = $level;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $record
|
* @param array $record
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function __invoke(array $record)
|
public function __invoke(array $record)
|
||||||
{
|
{
|
||||||
|
// return if the level is not high enough
|
||||||
|
if ($record['level'] < $this->level) {
|
||||||
|
return $record;
|
||||||
|
}
|
||||||
|
|
||||||
$branch = self::getBranch();
|
$record['extra']['git'] = self::getGitInfo();
|
||||||
$commit = self::getCommit();
|
|
||||||
|
|
||||||
$record['extra']['git']['branch'] = $branch;
|
|
||||||
$record['extra']['git']['commit'] = $commit;
|
|
||||||
|
|
||||||
return $record;
|
return $record;
|
||||||
}
|
}
|
||||||
|
|
||||||
static protected function getBranch() {
|
private static function getGitInfo()
|
||||||
$branches = explode("\n", `git branch`);
|
{
|
||||||
|
if (self::$cache) {
|
||||||
foreach ($branches as $branch) {
|
return self::$cache;
|
||||||
if ($branch[0] == "*") {
|
|
||||||
return substr($branch, 2);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return $branches;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static protected function getCommit() {
|
$branches = `git branch -v --no-abbrev`;
|
||||||
$s = `git rev-parse HEAD`;
|
if (preg_match('{^\* (.+?)\s+([a-f0-9]{40})(?:\s|$)}m', $branches, $matches)) {
|
||||||
return trim($s);
|
return self::$cache = array(
|
||||||
|
'branch' => $matches[1],
|
||||||
|
'commit' => $matches[2],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return self::$cache = array();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -24,8 +24,6 @@ class GitProcessorTest extends TestCase
|
|||||||
$record = $processor($this->getRecord());
|
$record = $processor($this->getRecord());
|
||||||
|
|
||||||
$this->assertArrayHasKey('git', $record['extra']);
|
$this->assertArrayHasKey('git', $record['extra']);
|
||||||
|
|
||||||
$this->assertTrue(!is_array($record['extra']['git']['branch']));
|
$this->assertTrue(!is_array($record['extra']['git']['branch']));
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user