Passed
Push — master ( 8feb3f...9dab76 )
by Biao
04:03
created

LogTrait::callWithCatchException()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Hhxsv5\LaravelS\Swoole\Traits;
4
5
trait LogTrait
6
{
7
    public function logException(\Exception $e)
8
    {
9
        $this->log(
10
            sprintf(
11
                'Uncaught exception \'%s\': [%d]%s called in %s:%d%s%s',
12
                get_class($e),
13
                $e->getCode(),
14
                $e->getMessage(),
15
                $e->getFile(),
16
                $e->getLine(),
17
                PHP_EOL,
18
                $e->getTraceAsString()
19
            ),
20
            'ERROR'
21
        );
22
    }
23
24
    public function log($msg, $type = 'INFO')
25
    {
26
        echo sprintf('[%s] [%s] LaravelS: %s', date('Y-m-d H:i:s'), $type, $msg), PHP_EOL;
27
    }
28
29
    public function callWithCatchException(callable $callback)
30
    {
31
        try {
32
            return $callback();
33
        } catch (\Exception $e) {
34
            $this->logException($e);
35
            return false;
36
        }
37
    }
38
}