Completed
Push — master ( c7d4dc...13d232 )
by Carlos
32:44 queued 24:34
created

Log::setLogger()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of the overtrue/wechat.
5
 *
6
 * (c) overtrue <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
/**
13
 * Log.php.
14
 *
15
 * This file is part of the wechat-components.
16
 *
17
 * (c) overtrue <[email protected]>
18
 *
19
 * This source file is subject to the MIT license that is bundled
20
 * with this source code in the file LICENSE.
21
 */
22
namespace EasyWeChat\Support;
23
24
use Monolog\Handler\ErrorLogHandler;
25
use Monolog\Handler\NullHandler;
26
use Monolog\Logger;
27
use Psr\Log\LoggerInterface;
28
29
/**
30
 * Class Log.
31
 */
32
class Log
33
{
34
    /**
35
     * Logger instance.
36
     *
37
     * @var \Psr\Log\LoggerInterface
38
     */
39
    protected static $logger;
40
41
    /**
42
     * Return the logger instance.
43
     *
44
     * @return \Psr\Log\LoggerInterface
45
     */
46 20
    public static function getLogger()
47
    {
48 20
        return self::$logger ?: self::$logger = self::createDefaultLogger();
49
    }
50
51
    /**
52
     * Set logger.
53
     *
54
     * @param \Psr\Log\LoggerInterface $logger
55
     */
56 3
    public static function setLogger(LoggerInterface $logger)
57
    {
58 3
        self::$logger = $logger;
59 3
    }
60
61
    /**
62
     * Forward call.
63
     *
64
     * @param string $method
65
     * @param array  $args
66
     *
67
     * @return mixed
68
     */
69 20
    public static function __callStatic($method, $args)
70
    {
71 20
        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 1
    private static function createDefaultLogger()
93
    {
94 1
        $log = new Logger('EasyWeChat');
95
96 1
        if (defined('PHPUNIT_RUNNING')) {
97 1
            $log->pushHandler(new NullHandler());    
98 1
        } else {
99
            $log->pushHandler(new ErrorLogHandler());
100
        }
101
102 1
        return $log;
103
    }
104
}
105