Completed
Push — master ( 1619f6...bbea89 )
by Carlos
40s
created

Log::hasLogger()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 2
eloc 2
nc 2
nop 0
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
    public static function getLogger()
47
    {
48
        return self::$logger ?: self::$logger = self::createDefaultLogger();
49
    }
50
51
    /**
52
     * Set logger.
53
     *
54
     * @param \Psr\Log\LoggerInterface $logger
55
     */
56
    public static function setLogger(LoggerInterface $logger)
57
    {
58
        self::$logger = $logger;
59
    }
60
61
    /**
62
     * Tests if logger exists
63
     * 
64
     * @return bool
65
     */
66
    public static function hasLogger()
67
    {
68
        return self::$logger ? true : false;
69
    }
70
71
    /**
72
     * Forward call.
73
     *
74
     * @param string $method
75
     * @param array  $args
76
     *
77
     * @return mixed
78
     */
79
    public static function __callStatic($method, $args)
80
    {
81
        return forward_static_call_array([self::getLogger(), $method], $args);
82
    }
83
84
    /**
85
     * Forward call.
86
     *
87
     * @param string $method
88
     * @param array  $args
89
     *
90
     * @return mixed
91
     */
92
    public function __call($method, $args)
93
    {
94
        return call_user_func_array([self::getLogger(), $method], $args);
95
    }
96
97
    /**
98
     * Make a default log instance.
99
     *
100
     * @return \Monolog\Logger
101
     */
102
    private static function createDefaultLogger()
103
    {
104
        $log = new Logger('EasyWeChat');
105
106
        if (defined('PHPUNIT_RUNNING')) {
107
            $log->pushHandler(new NullHandler());
108
        } else {
109
            $log->pushHandler(new ErrorLogHandler());
110
        }
111
112
        return $log;
113
    }
114
}
115