1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
namespace Hhxsv5\LaravelS\Illuminate; |
4
|
|
|
|
5
|
|
|
use Hhxsv5\LaravelS\LaravelS; |
6
|
|
|
use Symfony\Component\Console\Formatter\OutputFormatterStyle; |
7
|
|
|
|
8
|
|
|
trait LogTrait |
|
|
|
|
9
|
|
|
{ |
10
|
|
|
public function logException(\Exception $e) |
|
|
|
|
11
|
|
|
{ |
12
|
|
|
$this->log( |
13
|
|
|
sprintf( |
14
|
|
|
'Uncaught exception \'%s\': [%d]%s called in %s:%d%s%s', |
15
|
|
|
get_class($e), |
16
|
|
|
$e->getCode(), |
17
|
|
|
$e->getMessage(), |
18
|
|
|
$e->getFile(), |
19
|
|
|
$e->getLine(), |
20
|
|
|
PHP_EOL, |
21
|
|
|
$e->getTraceAsString() |
22
|
|
|
), |
23
|
|
|
'ERROR' |
24
|
|
|
); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function log($msg, $type = 'INFO') |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
$outputStyle = LaravelS::getOutputStyle(); |
30
|
|
|
$msg = sprintf('[%s] [%s] %s', date('Y-m-d H:i:s'), $type, $msg); |
31
|
|
|
if ($outputStyle) { |
|
|
|
|
32
|
|
|
switch (strtoupper($type)) { |
33
|
|
|
case 'INFO': |
|
|
|
|
34
|
|
|
$outputStyle->writeln("<info>{$msg}</info>"); |
35
|
|
|
break; |
36
|
|
|
case 'WARNING': |
|
|
|
|
37
|
|
|
if (!$outputStyle->getFormatter()->hasStyle('warning')) { |
|
|
|
|
38
|
|
|
$style = new OutputFormatterStyle('yellow'); |
39
|
|
|
$outputStyle->getFormatter()->setStyle('warning', $style); |
40
|
|
|
} |
|
|
|
|
41
|
|
|
$outputStyle->writeln("<warning>{$msg}</warning>"); |
42
|
|
|
break; |
43
|
|
|
case 'ERROR': |
|
|
|
|
44
|
|
|
$outputStyle->writeln("<error>{$msg}</error>"); |
45
|
|
|
break; |
46
|
|
|
case 'TRACE': |
|
|
|
|
47
|
|
|
default: |
|
|
|
|
48
|
|
|
$outputStyle->writeln($msg); |
49
|
|
|
break; |
50
|
|
|
} |
51
|
|
|
} else { |
52
|
|
|
echo $msg, PHP_EOL; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function trace($msg) |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
$this->log($msg, 'TRACE'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function info($msg) |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
$this->log($msg, 'INFO'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function warning($msg) |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
$this->log($msg, 'WARNING'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function error($msg) |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
$this->log($msg, 'ERROR'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function callWithCatchException(callable $callback, array $args = [], $tries = 1) |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
$try = 0; |
79
|
|
|
do { |
80
|
|
|
$try++; |
81
|
|
|
try { |
82
|
|
|
return call_user_func_array($callback, $args); |
83
|
|
|
} catch (\Exception $e) { |
84
|
|
|
$this->logException($e); |
85
|
|
|
} |
86
|
|
|
} while ($try < $tries); |
87
|
|
|
return null; |
88
|
|
|
} |
89
|
|
|
} |