Test Failed
Push — main ( b6e144...1a2341 )
by Rafael
05:34
created

Logger::__construct()   A

Complexity

Conditions 2
Paths 4

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 4
nop 1
dl 0
loc 14
rs 9.9332
c 0
b 0
f 0
1
<?php
2
/**
3
 * Alxarafe. Development of PHP applications in a flash!
4
 * Copyright (C) 2018-2020 Alxarafe <[email protected]>
5
 */
6
7
namespace Alxarafe\Core\Singletons;
8
9
use DateTimeZone;
10
use Exception;
11
use Monolog\Handler\FirePHPHandler;
12
use Monolog\Handler\StreamHandler;
13
use Monolog\Logger as MonologLogger;
14
15
/**
16
 * Class Logger
17
 *
18
 * @package Alxarafe\Core\Providers
19
 */
20
abstract class Logger
21
{
22
    /**
23
     * The logger.
24
     *
25
     * @var MonologLogger
26
     */
27
    private static MonologLogger $logger;
28
29
    /**
30
     * Logger constructor.
31
     */
32
    public static function load(string $index = 'main')
0 ignored issues
show
Unused Code introduced by
The parameter $index is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

32
    public static function load(/** @scrutinizer ignore-unused */ string $index = 'main')

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
33
    {
34
        self::$logger = new MonologLogger('core_logger');
35
        set_exception_handler(['static', 'exceptionHandler']);
36
        try {
37
            $timeZone = RegionalInfo::$config['timezone'];
38
            self::$logger->setTimezone(
39
                new DateTimeZone($timeZone)
40
            );
41
            self::$logger->pushHandler(new StreamHandler(CONFIGURATION_DIR . '/core.log', MonologLogger::DEBUG));
42
        } catch (Exception $e) {
43
            dump($e);
44
        }
45
        self::$logger->pushHandler(new FirePHPHandler());
46
    }
47
48
    /**
49
     * Catch the exception handler and adds to logger.
50
     *
51
     * @param Exception $e
52
     */
53
    public static function exceptionHandler($e): void
54
    {
55
        FlashMessages::setError($e->getMessage());
56
        dump($e);
57
        self::$logger->error(
58
            'Exception [' . $e->getCode() . ']: ' . $e->getMessage() . PHP_EOL
59
            . $e->getFile() . ':' . $e->getLine() . PHP_EOL
60
            . $e->getTraceAsString()
61
        );
62
    }
63
64
    /**
65
     * Returns the logger.
66
     *
67
     * @return MonologLogger
68
     */
69
    public static function getLogger(): MonologLogger
70
    {
71
        return self::$logger;
72
    }
73
}
74