MonologFactory   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 91
rs 10
c 0
b 0
f 0
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getStreamHandler() 0 10 2
A getQueuedJobLogger() 0 6 1
A getLoggerFor() 0 5 1
A getJobHandler() 0 5 1
A getOutputLogger() 0 15 2
A getFormatter() 0 10 2
1
<?php
2
3
namespace SilverStripe\FullTextSearch\Utils\Logging;
4
5
use Monolog\Formatter\FormatterInterface;
6
use Monolog\Formatter\LineFormatter;
7
use Monolog\Handler\HandlerInterface;
8
use Monolog\Handler\StreamHandler;
9
use Monolog\Logger;
10
use SilverStripe\Control\Director;
11
use SilverStripe\Core\Injector\Injector;
12
use SilverStripe\FullTextSearch\Utils\Logging\QueuedJobLogHandler;
13
14
/**
15
 * Provides logging based on monolog
16
 */
17
class MonologFactory implements SearchLogFactory
18
{
19
    public function getOutputLogger($name, $verbose)
20
    {
21
        $logger = $this->getLoggerFor($name);
22
        $formatter = $this->getFormatter();
23
24
        // Notice handling
25
        if ($verbose) {
26
            $messageHandler = $this->getStreamHandler($formatter, 'php://stdout', Logger::INFO);
27
            $logger->pushHandler($messageHandler);
28
        }
29
30
        // Error handling. buble is false so that errors aren't logged twice
31
        $errorHandler = $this->getStreamHandler($formatter, 'php://stderr', Logger::ERROR, false);
32
        $logger->pushHandler($errorHandler);
33
        return $logger;
34
    }
35
36
    public function getQueuedJobLogger($job)
37
    {
38
        $logger = $this->getLoggerFor(get_class($job));
39
        $handler = $this->getJobHandler($job);
40
        $logger->pushHandler($handler);
41
        return $logger;
42
    }
43
44
    /**
45
     * Generate a handler for the given stream
46
     *
47
     * @param FormatterInterface $formatter
48
     * @param string $stream Name of preferred stream
49
     * @param int $level
50
     * @param bool $bubble
51
     * @return HandlerInterface
52
     */
53
    protected function getStreamHandler(FormatterInterface $formatter, $stream, $level = Logger::DEBUG, $bubble = true)
54
    {
55
        // Unless cli, force output to php://output
56
        $stream = Director::is_cli() ? $stream : 'php://output';
57
        $handler = Injector::inst()->createWithArgs(
58
            StreamHandler::class,
59
            array($stream, $level, $bubble)
60
        );
61
        $handler->setFormatter($formatter);
62
        return $handler;
63
    }
64
65
    /**
66
     * Gets a formatter for standard output
67
     *
68
     * @return FormatterInterface
69
     */
70
    protected function getFormatter()
71
    {
72
        // Get formatter
73
        $format = LineFormatter::SIMPLE_FORMAT;
74
        if (!Director::is_cli()) {
75
            $format = "<p>$format</p>";
76
        }
77
        return Injector::inst()->createWithArgs(
78
            LineFormatter::class,
79
            array($format)
80
        );
81
    }
82
83
    /**
84
     * Get a logger for a named class
85
     *
86
     * @param string $name
87
     * @return Logger
88
     */
89
    protected function getLoggerFor($name)
90
    {
91
        return Injector::inst()->createWithArgs(
92
            Logger::class,
93
            array(strtolower($name))
94
        );
95
    }
96
97
    /**
98
     * Generate handler for a job object
99
     *
100
     * @param QueuedJob $job
101
     * @return HandlerInterface
102
     */
103
    protected function getJobHandler($job)
104
    {
105
        return Injector::inst()->createWithArgs(
106
            QueuedJobLogHandler::class,
107
            array($job, Logger::INFO)
108
        );
109
    }
110
}
111