Completed
Push — master ( 10cdf7...b78470 )
by Biao
03:54 queued 42s
created

LogTrait   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 46
dl 0
loc 90
rs 10
c 0
b 0
f 0
wmc 16

9 Methods

Rating   Name   Duplication   Size   Complexity  
B log() 0 26 7
A logException() 0 14 1
A warning() 0 3 1
A trace() 0 3 1
A error() 0 3 1
A callWithCatchException() 0 7 2
A info() 0 3 1
A setOutputStyle() 0 3 1
A getOutputStyle() 0 3 1
1
<?php
2
3
namespace Hhxsv5\LaravelS\Swoole\Traits;
4
5
use Hhxsv5\LaravelS\LaravelS;
6
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
7
use Symfony\Component\Console\Style\OutputStyle;
8
9
trait LogTrait
10
{
11
    /**
12
     * @var OutputStyle $outputStyle
13
     */
14
    protected static $outputStyle;
15
16
    public static function setOutputStyle(OutputStyle $outputStyle)
17
    {
18
        static::$outputStyle = $outputStyle;
19
    }
20
21
    public static function getOutputStyle()
22
    {
23
        return static::$outputStyle;
24
    }
25
26
    public function logException(\Exception $e)
27
    {
28
        $this->log(
29
            sprintf(
30
                'Uncaught exception \'%s\': [%d]%s called in %s:%d%s%s',
31
                get_class($e),
32
                $e->getCode(),
33
                $e->getMessage(),
34
                $e->getFile(),
35
                $e->getLine(),
36
                PHP_EOL,
37
                $e->getTraceAsString()
38
            ),
39
            'ERROR'
40
        );
41
    }
42
43
    public function log($msg, $type = 'INFO')
44
    {
45
        $outputStyle = LaravelS::getOutputStyle();
46
        $msg = sprintf('[%s] [%s] %s', date('Y-m-d H:i:s'), $type, $msg);
47
        if ($outputStyle) {
0 ignored issues
show
introduced by
$outputStyle is of type Symfony\Component\Console\Style\OutputStyle, thus it always evaluated to true.
Loading history...
48
            switch (strtoupper($type)) {
49
                case 'INFO':
50
                    $outputStyle->writeln("<info>$msg</info>");
51
                    break;
52
                case 'WARNING':
53
                    if (!$outputStyle->getFormatter()->hasStyle('warning')) {
54
                        $style = new OutputFormatterStyle('yellow');
55
                        $outputStyle->getFormatter()->setStyle('warning', $style);
56
                    }
57
                    $outputStyle->writeln("<warning>$msg</warning>");
58
                    break;
59
                case 'ERROR':
60
                    $outputStyle->writeln("<error>$msg</error>");
61
                    break;
62
                case 'TRACE':
63
                default:
64
                    $outputStyle->writeln($msg);
65
                    break;
66
            }
67
        } else {
68
            echo $msg, PHP_EOL;
69
        }
70
    }
71
72
    public function trace($msg)
73
    {
74
        $this->log($msg, 'TRACE');
75
    }
76
77
    public function info($msg)
78
    {
79
        $this->log($msg, 'INFO');
80
    }
81
82
    public function warning($msg)
83
    {
84
        $this->log($msg, 'WARNING');
85
    }
86
87
    public function error($msg)
88
    {
89
        $this->log($msg, 'ERROR');
90
    }
91
92
    public function callWithCatchException(callable $callback, array $args = [])
93
    {
94
        try {
95
            return call_user_func_array($callback, $args);
96
        } catch (\Exception $e) {
97
            $this->logException($e);
98
            return false;
99
        }
100
    }
101
}