/
BuildDBLogHandler.php
81 lines (69 loc) · 1.7 KB
/
BuildDBLogHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
namespace PHPCensor\Logging;
use PHPCensor\Store\Factory;
use Monolog\Handler\AbstractProcessingHandler;
use PHPCensor\Model\Build;
use Psr\Log\LogLevel;
/**
* Class BuildDBLogHandler writes the build log to the database.
*/
class BuildDBLogHandler extends AbstractProcessingHandler
{
/**
* @var Build
*/
protected $build;
protected $logValue;
/**
* @var int last flush timestamp
*/
protected $flush_timestamp = 0;
/**
* @var int flush delay, seconds
*/
protected $flush_delay = 1;
/**
* @param Build $build
* @param bool $level
* @param bool $bubble
*/
public function __construct(
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();
}
/**
* Destructor
*/
public function __destruct()
{
$this->flushData();
}
/**
* Flush buffered data
*/
protected function flushData()
{
$this->build->setLog($this->logValue);
Factory::getStore('Build')->save($this->build);
$this->flush_timestamp = time();
}
/**
* Write a log entry to the build log.
* @param array $record
*/
protected function write(array $record)
{
$message = (string)$record['message'];
$message = str_replace($this->build->getBuildPath(), './', $message);
$this->logValue .= $message . PHP_EOL;
if ($this->flush_timestamp < (time() - $this->flush_delay)) {
$this->flushData();
}
}
}