forked from Syonix/monolog-viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.php
144 lines (126 loc) · 5.07 KB
/
api.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
<?php
use Symfony\Component\HttpFoundation\Request;
use League\Flysystem\Adapter\Local;
$api = $app['controllers_factory'];
$api->get('/config', function (Silex\Application $app) {
return $app->json(array(
'debug' => $app['config']['debug'],
'timezone' => $app['config']['timezone'],
'date_format' => $app['config']['date_format'],
'display_logger' => $app['config']['display_logger'],
'default_limit' => $app['config']['default_limit'],
'reverse_line_order' => $app['config']['reverse_line_order'],
));
});
$api->get('/logs', function (Silex\Application $app, Request $request) {
$viewer = new Syonix\LogViewer\LogViewer($app['config']['logs']);
$clients = $viewer->getClients();
$returnLogs = (bool) $request->query->get('logs', false);
$return = [];
foreach ($clients as $client) {
$element = array(
'name' => $client->getName(),
'slug' => $client->getSlug(),
'url' => BASE_URL.'/api/logs/'.$client->getSlug()
);
if ($returnLogs) {
foreach ($client->getLogs() as $log) {
$element['logs'][] = array(
'name' => $log->getName(),
'slug' => $log->getSlug(),
'url' => BASE_URL.'/api/logs/'.$client->getSlug().'/'.$log->getSlug()
);
}
}
$return[] = $element;
}
$response = array(
'clients' => $return
);
return $app->json($response);
});
$api->get('/cache/clear', function (Silex\Application $app) {
$cache = new \Syonix\LogViewer\Cache(new Local(APP_PATH.'/cache'));
$cache->emptyCache();
return $app->json(array(
'message' => 'success'
));
});
$api->get('/logs/{clientSlug}', function (Silex\Application $app, $clientSlug) {
$viewer = new Syonix\LogViewer\LogViewer($app['config']['logs']);
$client = $viewer->getClient($clientSlug);
if(null === $client) {
$error = array('message' => 'The client was not found.');
return $app->json($error, 404);
}
$logs = $client->getLogs();
$return = [];
foreach($logs as $log) {
$return [] = array(
'name' => $log->getName(),
'slug' => $log->getSlug(),
'url' => BASE_URL.'/api/logs/'.$client->getSlug().'/'.$log->getSlug()
);
}
$response = array(
'name' => $client->getName(),
'slug' => $client->getSlug(),
'logs' => $return
);
return $app->json($response);
});
$api->get('/logs/{clientSlug}/{logSlug}', function (Silex\Application $app, Request $request, $clientSlug, $logSlug) {
$defaultLimit = (intval($app['config']['default_limit']) > 0) ? intval($app['config']['default_limit']) : 100;
$limit = intval($request->query->get('limit', $defaultLimit));
$offset = intval($request->query->get('offset', 0));
$filter = [];
$filter['logger'] = $request->query->get('logger');
if($filter['logger'] == "") $filter['logger'] = null;
$filter['level'] = intval($request->query->get('level', 0));
if(!($filter['level'] > 0)) $filter['level'] = null;
$filter['text'] = $request->query->get('text');
if($filter['text'] == "") $filter['text'] = null;
if($filter['logger'] === null && $filter['level'] === null && $filter['text'] === null) {
$filter = null;
}
$viewer = new Syonix\LogViewer\LogViewer($app['config']['logs']);
$client = $viewer->getClient($clientSlug);
if(null === $client) {
$error = array('message' => 'The client was not found.');
return $app->json($error, 404);
}
$log = $client->getLog($logSlug);
if(null === $log) {
$error = array('message' => 'The log file was not found.');
return $app->json($error, 404);
}
$adapter = new \League\Flysystem\Adapter\Local(APP_PATH.'/cache');
$cache = new \Syonix\LogViewer\Cache($adapter, $app['config']['cache_expire'], $app['config']['reverse_line_order']);
$log = $cache->get($log);
$logUrl = BASE_URL.'/api/logs/'.$client->getSlug().'/'.$log->getSlug();
$totalLines = $log->countLines($filter);
$prevPageUrl = $offset > 0 ? ($offset-$limit < 0 ? $logUrl.'?limit='.$limit.'&offset=0' : $logUrl.'?limit='.$limit.'&offset='.($offset-$limit)) : null;
$nextPageUrl = $offset+$limit < $totalLines ? $logUrl.'?limit='.$limit.'&offset='.($offset+$limit) : null;
if($filter !== null) {
foreach($filter as $k => $f) {
if($prevPageUrl !== null) $prevPageUrl .= '&'.$k.'='.$f;
if($nextPageUrl !== null) $nextPageUrl .= '&'.$k.'='.$f;
}
}
$response = array(
'name' => $log->getName(),
'client' => array(
'name' => $client->getName(),
'slug' => $client->getSlug()
),
'lines' => $log->getLines($limit, $offset),
'total_lines' => $totalLines,
'offset' => $offset,
'limit' => $limit,
'loggers' => $log->getLoggers()->toArray(),
'prev_page_url' => $prevPageUrl,
'next_page_url' => $nextPageUrl
);
return $app->json($response);
});
return $api;