forked from ProcessMaker/processmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogger.php
More file actions
76 lines (62 loc) · 1.91 KB
/
Logger.php
File metadata and controls
76 lines (62 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
namespace ProcessMaker\ImportExport;
use Illuminate\Log\Events\MessageLogged;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Storage;
use ProcessMaker\Events\ImportLog;
use ProcessMaker\Jobs\ImportV2;
class Logger
{
public $pid = null;
public $userId = null;
public function __construct($userId = null)
{
$this->pid = getmypid();
$this->userId = $userId;
if ($userId) {
Event::listen(MessageLogged::class, function (MessageLogged $e) {
if ($e->level === 'warning') {
return;
}
$this->logToFile('Log::' . $e->level, $e->message, $e->context);
});
}
}
public function clear()
{
Storage::delete(ImportV2::LOG_PATH);
}
public function log($message, $additionalParams = [])
{
$this->dispatch('log', $message, $additionalParams);
}
public function warn($message)
{
$this->dispatch('warn', $message);
}
public function error($message)
{
$this->dispatch('error', $message);
}
public function exception($e)
{
$this->dispatch('error', get_class($e) . ': ' . $e->getMessage());
}
private function dispatch($type, $message, $additionalParams = [])
{
if (!$this->userId) {
return;
}
ImportLog::dispatch($this->userId, $type, substr($message, 0, 1000), $additionalParams);
$this->logToFile($type, $message, $additionalParams);
}
private function logToFile($type, $message, $additionalParams = [])
{
$params = '';
if (!empty($additionalParams)) {
$params = ' additionalParams: ' . json_encode($additionalParams);
}
$datetime = date('Y-m-d H:i:s');
Storage::append(ImportV2::LOG_PATH, "[$this->pid] [$this->userId] [$datetime] [$type] $message" . $params);
}
}