-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathLogManager.php
170 lines (148 loc) · 4.19 KB
/
LogManager.php
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
namespace Biigle\Logging;
use Carbon\Carbon;
use File;
use Illuminate\Log\ParsesLogConfiguration;
use Illuminate\Support\Facades\Redis;
class LogManager
{
use ParsesLogConfiguration;
/**
* Available log levels.
*
* @var array
*/
const LEVELS = [
'debug',
'info',
'notice',
'warning',
'error',
'critical',
'emergency',
];
/**
* The default logging channel.
*
* @var array
*/
protected $channel;
/**
* Create a new instance.
*/
public function __construct()
{
$defaultChannel = config('logging.default');
$this->channel = config("logging.channels.{$defaultChannel}");
}
/**
* Determine whether logs are written to files.
*
* @return boolean
*/
public function isFile()
{
return in_array($this->channel['driver'], ['single', 'daily']);
}
/**
* Get the filenames of the logfiles.
*
* @return array
*/
public function getLogFilenames()
{
$path = File::dirname($this->channel['path']);
$logs = File::glob("{$path}/*.log");
return array_map(function ($path) {
return File::name($path);
}, $logs);
}
/**
* Get the content of a logfile.
*
* @param string $filename
*
* @return string
*/
public function getLogFileContent($filename)
{
$path = File::dirname($this->channel['path']);
return File::get("{$path}/{$filename}.log");
}
/**
* Determine whether logs are written to files.
*
* @return boolean
*/
public function isRedis()
{
return $this->channel['driver'] === 'custom' &&
$this->channel['via'] === CreateRedisLogger::class;
}
/**
* Get the log messages.
*
* @param string $level Minimum log level.
*
* @return \Illuminate\Support\Collection
*/
public function getRedisLogMessages($level = 'debug')
{
$level = $this->level(compact('level'));
$connection = $this->channel['connection'] ?? null;
$client = Redis::connection($connection)->client();
return collect($client->lrange('log', 0, -1))
->map(function ($message) {
return json_decode($message, true);
})
->filter(function ($message) use ($level) {
return $message['level'] >= $level;
});
}
/**
* Get the number of log messages of the last day(s).
*
* @param string $level Minimum log level.
* @param integer $subDays Number of days to go back.
*
* @return int
*/
public function getRecentCount($level = 'debug', $subDays = 1)
{
$days = [];
for ($i = 0; $i <= $subDays; $i++) {
$days[] = Carbon::now()->subDays($i)->toDateString();
}
if ($this->isFile()) {
$filenames = $this->getLogFilenames();
return array_reduce($filenames, function ($carry, $filename) use ($days) {
$content = $this->getLogFileContent($filename);
return array_reduce($days, function ($carry, $day) use ($content) {
return $carry + substr_count($content, "[{$day}");
}, $carry);
}, 0);
} elseif ($this->isRedis()) {
return $this->getRedisLogMessages($level)
->reduce(function ($carry, $message) use ($days) {
return array_reduce($days, function ($carry, $day) use ($message) {
// There are two possible formats in which the time can be
// stored.
if (is_array($message['datetime'])) {
return $carry + substr_count($message['datetime']['date'], $day);
}
return $carry + substr_count($message['datetime'], "{$day}T");
}, $carry);
}, 0);
}
return 0;
}
/**
* Get fallback log channel name.
*
* @return string
*/
protected function getFallbackChannelName()
{
return config('app.env');
}
}