php-censor/src/PHPCensor/Logging/BuildDBLogHandler.php

61 lines
1.4 KiB
PHP
Raw Normal View History

<?php
/**
* PHPCI - Continuous Integration for PHP
*
* @copyright Copyright 2014, Block 8 Limited.
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
* @link https://www.phptesting.org/
*/
2016-07-19 20:28:11 +02:00
namespace PHPCensor\Logging;
use b8\Store\Factory;
use Monolog\Handler\AbstractProcessingHandler;
2016-07-19 20:28:11 +02:00
use PHPCensor\Model\Build;
2015-02-12 12:42:09 +01:00
use Psr\Log\LogLevel;
/**
* Class BuildDBLogHandler writes the build log to the database.
* @package PHPCensor\Logging
*/
class BuildDBLogHandler extends AbstractProcessingHandler
{
2013-10-26 17:25:34 +02:00
/**
* @var Build
*/
protected $build;
2013-10-26 17:25:34 +02:00
protected $logValue;
/**
* @param Build $build
* @param bool $level
* @param bool $bubble
*/
2014-02-27 15:23:51 +01:00
public function __construct(
2013-10-26 17:25:34 +02:00
Build $build,
$level = LogLevel::INFO,
$bubble = true
) {
parent::__construct($level, $bubble);
$this->build = $build;
// We want to add to any existing saved log information.
$this->logValue = $build->getLog();
}
/**
* Write a log entry to the build log.
* @param array $record
*/
2013-10-26 17:25:34 +02:00
protected function write(array $record)
{
$message = (string)$record['message'];
$message = str_replace($this->build->currentBuildPath, '/', $message);
$this->logValue .= $message . PHP_EOL;
2013-10-26 17:25:34 +02:00
$this->build->setLog($this->logValue);
Factory::getStore('Build')->save($this->build);
2013-10-26 17:25:34 +02:00
}
2014-02-27 15:23:51 +01:00
}