Test Failed
Push — release/5.x ( 3aca7b...4817e3 )
by
unknown
08:19 queued 02:06
created

CreateStdLoggerTrait   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 45
rs 10
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A createStdLogger() 0 15 2
1
<?php declare(strict_types=1);
2
/**
3
 * @copyright Zicht Online <https://zicht.nl>
4
 */
5
6
namespace Zicht\Bundle\FrameworkExtraBundle\Command;
7
8
use Monolog\Handler\FilterHandler;
0 ignored issues
show
Bug introduced by
The type Monolog\Handler\FilterHandler was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Monolog\Handler\StreamHandler;
0 ignored issues
show
Bug introduced by
The type Monolog\Handler\StreamHandler was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Monolog\Logger;
0 ignored issues
show
Bug introduced by
The type Monolog\Logger was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Psr\Log\LoggerInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
trait CreateStdLoggerTrait
15
{
16
    /**
17
     * Returns a logger configured to use STDOUT and STDERR depending on $input and $output settings
18
     *
19
     * Use this method to construct a logger that sends log messages to stdout or stderr.  This
20
     * is especially useful for commands that are executed using crontab in combination with the
21
     * linux tool cronic.
22
     *
23
     * Usage:
24
     * ```php
25
     * class ExampleCommand extends AbstractCommand
26
     * {
27
     *     use CreateStdLoggerTrait;
28
     *
29
     *     protected function execute(InputInterface $input, OutputInterface $output)
30
     *     {
31
     *         $logger = $this->createStdLogger($output, 'console');
32
     *         $logger->debug(...);      // logs to stdout when -v or --verbose
33
     *         $logger->info(...);       // logs to stdout
34
     *         $logger->notice(...);     // logs to stdout
35
     *         $logger->warning(...);    // logs to stdout
36
     *         $logger->error(...);      // logs to stderr
37
     *         $logger->critical(...);   // logs to stderr even when -q (quiet)
38
     *         $logger->alert(...);      // logs to stderr even when -q (quiet)
39
     *         $logger->emergency(...);  // logs to stderr even when -q (quiet)
40
     *     }
41
     * }
42
     * ```
43
     */
44
    protected function createStdLogger(OutputInterface $output, string $name): LoggerInterface
45
    {
46
        $levelMap = [
47
            OutputInterface::VERBOSITY_QUIET => Logger::CRITICAL,
48
            OutputInterface::VERBOSITY_NORMAL => Logger::INFO,
49
            OutputInterface::VERBOSITY_VERBOSE => Logger::DEBUG,
50
            OutputInterface::VERBOSITY_VERY_VERBOSE => Logger::DEBUG,
51
            OutputInterface::VERBOSITY_DEBUG => Logger::DEBUG,
52
        ];
53
        $level = $levelMap[$output->getVerbosity()] ?: Logger::ERROR;
54
55
        $logger = new Logger($name);
56
        $logger->pushHandler(new FilterHandler(new StreamHandler(STDOUT, $level), Logger::DEBUG, Logger::ERROR - 1));
57
        $logger->pushHandler(new FilterHandler(new StreamHandler(STDERR, $level), Logger::ERROR, Logger::EMERGENCY));
58
        return $logger;
59
    }
60
}
61