-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDockerMonologHandler.php
More file actions
91 lines (78 loc) · 1.87 KB
/
DockerMonologHandler.php
File metadata and controls
91 lines (78 loc) · 1.87 KB
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
82
83
84
85
86
87
88
89
90
<?php
namespace DockerMonologHandler;
use Monolog\Formatter\LineFormatter;
use Monolog\Formatter\FormatterInterface;
use Monolog\Handler\AbstractProcessingHandler;
use Monolog\Logger;
/**
* Logs to a Docker container output.
*
* usage example:
*
* $log = new Logger('application');
* $docker = new DockerHandler();
* $log->pushHandler($docker);
*
* @author Ilya Isaev <me@ilyaisaev.com>
*/
class DockerMonologHandler extends AbstractProcessingHandler
{
/**
* @var resource
*/
private $resource;
/**
* @var string
*/
private $command;
/**
* Constructor
*
* @param int $processId PID, this should be 1
* @param int $fileDescriptor Accept 1: stdout, or 2: stderr
* @param string|int $level Log level
* @param bool $bubble
* @param FormatterInterface|null $formatter
*/
public function __construct(
int $processId = 1,
int $fileDescriptor = 2,
$level = Logger::DEBUG,
bool $bubble = true,
FormatterInterface $formatter = null
)
{
$this->command = sprintf('cat - >> /proc/%d/fd/%d', $processId, $fileDescriptor);
parent::__construct($level, $bubble);
if (null !== $formatter) {
$this->setFormatter($formatter);
}
}
/**
* {@inheritdoc}
*/
public function close()
{
if (is_resource($this->resource)) {
pclose($this->resource);
}
parent::close();
}
/**
* {@inheritdoc}
*/
protected function write(array $record)
{
if (!is_resource($this->resource)) {
$this->resource = popen($this->command, 'w');
}
fwrite($this->resource, (string)$record['formatted']);
}
/**
* {@inheritDoc}
*/
protected function getDefaultFormatter()
{
return new LineFormatter();
}
}