Log   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 83
rs 10
c 0
b 0
f 0
wmc 9
lcom 0
cbo 3

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getLogger() 0 4 2
A setLogger() 0 4 1
A hasLogger() 0 4 2
A __callStatic() 0 4 1
A __call() 0 4 1
A createDefaultLogger() 0 12 2
1
<?php
2
3
namespace EntWeChat\Support;
4
5
use Monolog\Handler\ErrorLogHandler;
6
use Monolog\Handler\NullHandler;
7
use Monolog\Logger;
8
use Psr\Log\LoggerInterface;
9
10
/**
11
 * Class Log.
12
 *
13
 * @method static debug($message, $context = null)
14
 * @method static info($message, $context = null)
15
 * @method static notice($message, $context = null)
16
 * @method static warning($message, $context = null)
17
 * @method static error($message, $context = null)
18
 * @method static critical($message, $context = null)
19
 * @method static alert($message, $context = null)
20
 * @method static emergency($message, $context = null)
21
 */
22
class Log
23
{
24
    /**
25
     * Logger instance.
26
     *
27
     * @var \Psr\Log\LoggerInterface
28
     */
29
    protected static $logger;
30
31
    /**
32
     * Return the logger instance.
33
     *
34
     * @return \Psr\Log\LoggerInterface
35
     */
36
    public static function getLogger()
37
    {
38
        return self::$logger ?: self::$logger = self::createDefaultLogger();
39
    }
40
41
    /**
42
     * Set logger.
43
     *
44
     * @param \Psr\Log\LoggerInterface $logger
45
     */
46
    public static function setLogger(LoggerInterface $logger)
47
    {
48
        self::$logger = $logger;
49
    }
50
51
    /**
52
     * Tests if logger exists.
53
     *
54
     * @return bool
55
     */
56
    public static function hasLogger()
57
    {
58
        return self::$logger ? true : false;
59
    }
60
61
    /**
62
     * Forward call.
63
     *
64
     * @param string $method
65
     * @param array  $args
66
     *
67
     * @return mixed
68
     */
69
    public static function __callStatic($method, $args)
70
    {
71
        return forward_static_call_array([self::getLogger(), $method], $args);
72
    }
73
74
    /**
75
     * Forward call.
76
     *
77
     * @param string $method
78
     * @param array  $args
79
     *
80
     * @return mixed
81
     */
82
    public function __call($method, $args)
83
    {
84
        return call_user_func_array([self::getLogger(), $method], $args);
85
    }
86
87
    /**
88
     * Make a default log instance.
89
     *
90
     * @return \Monolog\Logger
91
     */
92
    private static function createDefaultLogger()
93
    {
94
        $log = new Logger('EntWeChat');
95
96
        if (defined('PHPUNIT_RUNNING')) {
97
            $log->pushHandler(new NullHandler());
98
        } else {
99
            $log->pushHandler(new ErrorLogHandler());
100
        }
101
102
        return $log;
103
    }
104
}
105