Skip to content

Commit ead7deb

Browse files
committed
Rewrite the logger function to be more generic with its variables
Add the req number to all api related log entries Add the starr instance name to the api related log entries
1 parent 6bb547d commit ead7deb

File tree

13 files changed

+113
-97
lines changed

13 files changed

+113
-97
lines changed

root/app/www/public/api/index.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
$endpoint = rtrim($endpoint, '/');
4444

4545
if (!$apikey) {
46-
logger($logfile, $apikey, null, 401);
46+
logger($logfile, ['req' => $requestCounter, 'proxyCode' => 401]);
4747
apiResponse(401, ['error' => sprintf(APP_API_ERROR, 'no apikey provided')]);
4848
}
4949

@@ -160,14 +160,13 @@
160160
$app = $starr->getStarrInterfaceNameFromId($proxiedApp['starrAppDetails']['starr']);
161161

162162
if (!$proxiedApp) {
163-
logger($logfile, $apikey, $endpoint, 401);
163+
logger($logfile, ['req' => $requestCounter, 'apikey' => $apikey, 'endpoint' => $endpoint, 'proxyCode' => 401]);
164164
apiResponse(401, ['error' => sprintf(APP_API_ERROR, 'provided apikey is not valid or has no access')]);
165165
}
166166

167167
$proxiedAppLogfile = str_replace('access.log', 'access_' . $proxiedApp['proxiedAppDetails']['name'] . '.log', $logfile);
168168

169-
logger($proxiedAppLogfile, '[req ' . $requestCounter . '] incoming request from apikey: ' . truncateMiddle($apikey, 20), null, 999);
170-
logger($proxiedAppLogfile, '[req ' . $requestCounter . '] $starr->getAppFromProxiedKey: name=' . $proxiedApp['proxiedAppDetails']['name'] . ', id=' . $proxiedApp['proxiedAppDetails']['id'], null, 999);
169+
logger($proxiedAppLogfile, ['req' => $requestCounter, 'starr' => $proxiedApp['starrAppDetails']['name'], 'text' => 'apikey: ' . truncateMiddle($apikey, 20) . '; $starr->getAppFromProxiedKey: id=' . $proxiedApp['proxiedAppDetails']['id'] . '; app=' . $proxiedApp['proxiedAppDetails']['name']]);
171170

172171
if (!$endpoint && $_GET['backup']) { //--- Notifiarr corruption checking
173172
$proxyBackup = $starr->downloadBackup($_GET['backup'], $proxiedApp['starrAppDetails']);
@@ -186,8 +185,8 @@
186185
if ($isAllowed) {
187186
$endpoint = $starrEndpoint;
188187
} else {
189-
logger($logfile, $apikey, $endpoint, 401);
190-
logger($proxiedAppLogfile, $apikey, $endpoint, 401);
188+
logger($logfile, ['req' => $requestCounter, 'apikey' => $apikey, 'endpoint' => $endpoint, 'proxyCode' => 401]);
189+
logger($proxiedAppLogfile, ['req' => $requestCounter, 'apikey' => $apikey, 'endpoint' => $endpoint, 'proxyCode' => 401]);
191190
$usageDb->adjustAppUsage($proxiedApp['proxiedAppDetails']['id'], 401);
192191

193192
if ($proxyDb->isNotificationTriggerEnabled('blocked')) {
@@ -204,8 +203,8 @@
204203
}
205204

206205
if (!$accessMethod = $starr->isAllowedEndpointMethod($proxiedApp['access'], $endpoint, $method)) {
207-
logger($logfile, $apikey, $endpoint, 405);
208-
logger($proxiedAppLogfile, $apikey, $endpoint, 405);
206+
logger($logfile, ['req' => $requestCounter, 'apikey' => $apikey, 'endpoint' => $endpoint, 'proxyCode' => 405]);
207+
logger($proxiedAppLogfile, ['req' => $requestCounter, 'apikey' => $apikey, 'endpoint' => $endpoint, 'proxyCode' => 405]);
209208
$usageDb->adjustAppUsage($proxiedApp['proxiedAppDetails']['id'], 405);
210209

211210
if ($proxyDb->isNotificationTriggerEnabled('blocked')) {
@@ -225,8 +224,9 @@
225224
$starrUrl = $proxiedApp['starrAppDetails']['url'] . $originalEndpoint . ($variables ? '?' . http_build_query($variables) : '');
226225
$request = curl($starrUrl, ['X-Api-Key:' . $proxiedApp['starrAppDetails']['apikey']], $method, $json);
227226

228-
logger($logfile, $apikey, $endpoint, 200, $request['code']);
229-
logger($proxiedAppLogfile, $apikey, $originalEndpoint, 200, $request['code'], $request);
227+
logger($logfile, ['req' => $requestCounter, 'apikey' => $apikey, 'endpoint' => $originalEndpoint, 'proxyCode' => 200, 'starrCode' => $request['code']]);
228+
logger($proxiedAppLogfile, ['req' => $requestCounter, 'apikey' => $apikey, 'endpoint' => $originalEndpoint, 'proxyCode' => 200, 'starrCode' => $request['code'], 'starrRequest' => $request]);
229+
230230
$usageDb->adjustAppUsage($proxiedApp['proxiedAppDetails']['id'], $request['code']);
231231

232232
if ($request['code'] <= 299 && str_contains($endpoint, 'mediacover')) { //-- OUTPUT THE REQUESTED IMAGE

root/app/www/public/classes/Database.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,15 @@ public function migrations()
151151
setFile(MIGRATION_FILE, ['started' => date('c')]);
152152

153153
if (filesize(DATABASE_PATH . $this->dbName) == 0) { //-- INITIAL SETUP
154-
logger(SYSTEM_LOG, 'Creating database and applying migration 001_initial_setup');
155-
logger(MIGRATION_LOG, '====================|');
156-
logger(MIGRATION_LOG, '====================| migrations');
157-
logger(MIGRATION_LOG, '====================|');
158-
logger(MIGRATION_LOG, 'migration 001 ->');
154+
logger(SYSTEM_LOG, ['text' => 'Creating database and applying migration 001_initial_setup']);
155+
logger(MIGRATION_LOG, ['text' => '====================|']);
156+
logger(MIGRATION_LOG, ['text' => '====================| migrations']);
157+
logger(MIGRATION_LOG, ['text' => '====================|']);
158+
logger(MIGRATION_LOG, ['text' => 'migration 001 ->']);
159+
logger(MIGRATION_LOG, ['text' => 'migration 001 <-']);
159160
$q = [];
160161
require MIGRATIONS_PATH . '001_initial_setup.php';
161-
logger(MIGRATION_LOG, 'migration 001 <-');
162+
logger(MIGRATION_LOG, ['text' => 'migration 001 <-']);
162163

163164
$neededMigrations = [];
164165
$dir = opendir(MIGRATIONS_PATH);
@@ -173,10 +174,10 @@ public function migrations()
173174
ksort($neededMigrations);
174175

175176
foreach ($neededMigrations as $migrationNumber => $neededMigration) {
176-
logger(MIGRATION_LOG, 'migration ' . $migrationNumber . ' ->');
177+
logger(MIGRATION_LOG, ['text' => 'migration ' . $migrationNumber . ' ->']);
177178
$q = [];
178179
require MIGRATIONS_PATH . $neededMigration;
179-
logger(MIGRATION_LOG, 'migration ' . $migrationNumber . ' <-');
180+
logger(MIGRATION_LOG, ['text' => 'migration ' . $migrationNumber . ' <-']);
180181
}
181182
}
182183
} else { //-- GET CURRENT MIGRATION & CHECK FOR NEEDED MIGRATIONS
@@ -192,16 +193,16 @@ public function migrations()
192193
if ($neededMigrations) {
193194
ksort($neededMigrations);
194195

195-
logger(SYSTEM_LOG, 'Applying migrations: ' . implode(', ', array_keys($neededMigrations)));
196-
logger(MIGRATION_LOG, '====================|');
197-
logger(MIGRATION_LOG, '====================| migrations');
198-
logger(MIGRATION_LOG, '====================|');
196+
logger(SYSTEM_LOG, ['text' => 'Applying migrations: ' . implode(', ', array_keys($neededMigrations))]);
197+
logger(MIGRATION_LOG, ['text' => '====================|']);
198+
logger(MIGRATION_LOG, ['text' => '====================| migrations']);
199+
logger(MIGRATION_LOG, ['text' => '====================|']);
199200

200201
foreach ($neededMigrations as $migrationNumber => $neededMigration) {
201-
logger(MIGRATION_LOG, 'migration ' . $migrationNumber . ' ->');
202+
logger(MIGRATION_LOG, ['text' => 'migration ' . $migrationNumber . ' ->']);
202203
$q = [];
203204
require MIGRATIONS_PATH . $neededMigration;
204-
logger(MIGRATION_LOG, 'migration ' . $migrationNumber . ' <-');
205+
logger(MIGRATION_LOG, ['text' => 'migration ' . $migrationNumber . ' <-']);
205206
}
206207
}
207208
}

root/app/www/public/classes/Notifications.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ public function sendTestNotification($linkId, $name)
5353
$notificationPlatform = $notificationPlatformTable[$notificationLinkData['platform_id']];
5454

5555
$logfile = $this->logpath . $notificationPlatform['platform'] . '.log';
56-
logger($logfile, 'test notification request to ' . $notificationPlatform['platform']);
57-
logger($logfile, 'test=' . $name);
58-
logger($logfile, 'tests=' . json_encode($tests));
59-
logger($logfile, 'test payload=' . json_encode($tests[$name]));
56+
logger($logfile, ['text' => 'test notification request to ' . $notificationPlatform['platform']]);
57+
logger($logfile, ['text' => 'test=' . $name]);
58+
logger($logfile, ['text' => 'tests=' . json_encode($tests)]);
59+
logger($logfile, ['text' => 'test payload=' . json_encode($tests[$name])]);
6060

6161
$result = $this->notify($linkId, $name, $tests[$name], true);
6262

@@ -125,8 +125,8 @@ public function notify($linkId, $trigger, $payload, $test = false)
125125
}
126126

127127
$logfile = $this->logpath . $platformName . '.log';
128-
logger($logfile, 'notification request to ' . $platformName);
129-
logger($logfile, 'notification payload: ' . json_encode($payload));
128+
logger($logfile, ['text' => 'notification request to ' . $platformName]);
129+
logger($logfile, ['text' => 'notification payload: ' . json_encode($payload)]);
130130

131131
switch ($platformId) {
132132
case NotificationPlatforms::NOTIFIARR:

root/app/www/public/classes/traits/Notifications/Notifiarr.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function notifiarr($logfile, $apikey, $payload, $test = false)
1515
$url = 'https://notifiarr.com/api/v1/notification/starrproxy';
1616
$curl = curl($url, $headers, 'POST', json_encode($payload));
1717

18-
logger($logfile, 'notification response:' . json_encode($curl));
18+
logger($logfile, ['text' => 'notification response:' . json_encode($curl)]);
1919

2020
$return = ['code' => 200];
2121

@@ -25,11 +25,11 @@ public function notifiarr($logfile, $apikey, $payload, $test = false)
2525
}
2626

2727
if (!str_equals_any($curl['code'], [200, 201, 400, 401])) {
28-
logger($logfile, 'sending a retry in 5s...');
28+
logger($logfile, ['text' => 'sending a retry in 5s...']);
2929
sleep(5);
3030

3131
$curl = curl($url, $headers, 'POST', json_encode($payload));
32-
logger($logfile, 'notification response:' . json_encode($curl));
32+
logger($logfile, ['text' => 'notification response:' . json_encode($curl)]);
3333

3434
if ($curl['code'] != 200) {
3535
$error = is_array($curl['response']) && $curl['response']['details'] && $curl['response']['details']['response'] ? $curl['response']['details']['response'] : 'Unknown error';

root/app/www/public/classes/traits/Notifications/Telegram.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ public function telegram($logfile, $botToken, $chatId, $payload, $test = false)
2424
$url = sprintf($url, $botToken);
2525
$curl = curl($url, [], 'POST', json_encode($payload));
2626

27-
logger($logfile, 'notification response:' . json_encode($curl), ($curl['code'] != 200 ? 'error' : ''));
27+
logger($logfile, ['text' => 'notification response:' . json_encode($curl), 'notificationCode' => $curl['code']]);
2828

2929
$return = ['code' => 200];
3030
if (!str_equals_any($curl['code'], [200, 201, 400, 401])) {
31-
logger($logfile, 'sending a retry in 5s...');
31+
logger($logfile, ['text' => 'sending a retry in 5s...']);
3232
sleep(5);
3333

3434
$curl = curl($url, [], 'POST', json_encode($payload));
35-
logger($logfile, 'notification response:' . json_encode($curl), ($curl['code'] != 200 ? 'error' : ''));
35+
logger($logfile, ['text' => 'notification response:' . json_encode($curl), 'notificationCode' => $curl['code']]);
3636

3737
if ($curl['code'] != 200) {
3838
$return = ['code' => $curl['code'], 'error' => $curl['response']['description']];

root/app/www/public/functions/file.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99

1010
function getFile($file)
1111
{
12-
logger(SYSTEM_LOG, 'getFile() ' . $file);
12+
logger(SYSTEM_LOG, ['text' => 'getFile() ' . $file]);
1313

1414
if (!$file) {
15-
logger(SYSTEM_LOG, '$file is empty');
15+
logger(SYSTEM_LOG, ['text' => '$file is empty']);
1616
return [];
1717
}
1818

@@ -25,7 +25,7 @@ function getFile($file)
2525

2626
function setFile($file, $contents)
2727
{
28-
logger(SYSTEM_LOG, 'setFile() ' . $file);
28+
logger(SYSTEM_LOG, ['text' => 'setFile() ' . $file]);
2929

3030
if (is_array($contents)) {
3131
$contents = json_encode($contents, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
@@ -40,7 +40,6 @@ function setFile($file, $contents)
4040

4141
function deleteFile($file)
4242
{
43-
logger(SYSTEM_LOG, 'deleteFile() ' . $file);
44-
43+
logger(SYSTEM_LOG, ['text' => 'deleteFile() ' . $file]);
4544
unlink($file);
4645
}

root/app/www/public/functions/logger.php

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,40 +7,56 @@
77
----------------------------------
88
*/
99

10-
function logger($logfile, $apikey = '', $endpoint = '', $proxyCode = 200, $starrCode = 0, $starrRequest = [])
10+
function logger($logfile, $logParameters)
1111
{
12-
if (!$logfile) {
13-
return;
14-
}
12+
global $settingsTable, $LOG_ROTATE_SIZE;
1513

16-
$date = date(sprintf('Y-m-d\TH:i:s%s', substr(microtime(), 1, 8)));
17-
if (str_equals_any($logfile, [MIGRATION_LOG, SYSTEM_LOG]) || str_contains_any($logfile, ['notifications/']) || $proxyCode == 999) {
18-
file_put_contents($logfile, $date . ' ' . $apikey . "\n", FILE_APPEND);
19-
return;
20-
}
14+
$backtrace = debug_backtrace();
15+
$log = [];
16+
$date = date(sprintf('Y-m-d\TH:i:s%s', substr(microtime(), 1, 8)));
17+
$log[] = $date;
2118

22-
$log = $date . ' ua:' . $_SERVER['HTTP_USER_AGENT'];
23-
if ($apikey) {
24-
$log .= '; key:' . truncateMiddle($apikey, 20);
19+
if ($logParameters['req']) {
20+
$log[] = '[' . $logParameters['req'] . ']';
2521
}
26-
if ($endpoint) {
27-
$log .= '; endpoint:' . $endpoint;
22+
23+
if ($logParameters['starr']) {
24+
$log[] = '[' . $logParameters['starr'] . ']';
2825
}
2926

30-
$log .= '; method:' . strtolower($_SERVER['REQUEST_METHOD']);
31-
$log .= '; proxyCode:' . $proxyCode;
27+
if ($logParameters['text']) {
28+
$log[] = $logParameters['text'];
3229

33-
if ($starrCode) {
34-
$log .= '; starrCode:' . $starrCode;
30+
if ($logParameters['notificationCode']) {
31+
$log[] = 'notificationCode:' . $logParameters['notificationCode'] . ';';
32+
}
33+
} else {
34+
$log[] = 'ua:' . $_SERVER['HTTP_USER_AGENT'] . ';';
35+
if ($logParameters['apikey']) {
36+
$log[] = 'key:' . truncateMiddle($logParameters['apikey'], 20) . ';';
37+
}
38+
if ($logParameters['endpoint']) {
39+
$log[] = 'endpoint:' . $logParameters['endpoint'] . ';';
40+
}
3541

36-
if ($starrCode != 200) {
37-
$log .= '; starrResponse:' . json_encode($starrRequest);
42+
$log[] = 'method:' . strtolower($_SERVER['REQUEST_METHOD']) . ';';
43+
$log[] = 'proxyCode:' . $logParameters['proxyCode'] . ';';
44+
45+
if ($logParameters['starrCode']) {
46+
$log[] = 'starrCode:' . $logParameters['starrCode'] . ';';
47+
48+
if ($logParameters['starrCode'] != 200 && $logParameters['starrRequest']) {
49+
$log[] = 'starrResponse:' . json_encode($logParameters['starrRequest']);
50+
}
3851
}
3952
}
4053

41-
file_put_contents($logfile, $log . "\n", FILE_APPEND);
54+
$log[] = 'file:' . $backtrace[0]['file'] . ';';
55+
$log[] = 'line:' . $backtrace[0]['line'] . ';';
56+
57+
file_put_contents($logfile, implode(' ', $log) . "\n", FILE_APPEND);
4258

43-
$rotateSize = LOG_ROTATE_SIZE * pow(1024, 2);
59+
$rotateSize = ($settingsTable['logRotationSize'] ?: $LOG_ROTATE_SIZE) * pow(1024, 2);
4460
if (filesize($logfile) >= $rotateSize) {
4561
$rotated = str_replace('.log', '_' . time() . '.log', $logfile);
4662
rename($logfile, $rotated);
@@ -164,7 +180,7 @@ function getLog($logfile, $page = 1, $app = false)
164180
$error = '<span class="text-danger">[ERROR]</span> ';
165181
}
166182

167-
if (str_contains($line, '[req ')) {
183+
if (str_contains($line, 'getAppFromProxiedKey')) {
168184
$error = '<span class="text-info">[VERIFICATION]</span> ';
169185
}
170186

root/app/www/public/migrations/001_initial_setup.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,14 @@
7272
('migration', '001')";
7373

7474
foreach ($q as $query) {
75-
logger(MIGRATION_LOG, '<span class="text-success">[Q]</span> ' . preg_replace('!\s+!', ' ', $query));
75+
logger(MIGRATION_LOG, ['text' => '<span class="text-success">[Q]</span> ' . preg_replace('!\s+!', ' ', $query)]);
7676

7777
$proxyDb->query($query);
7878

7979
if ($proxyDb->error() != 'not an error') {
80-
logger(MIGRATION_LOG, '<span class="text-info">[R]</span> ' . $proxyDb->error(), 'error');
80+
logger(MIGRATION_LOG, ['text' => '<span class="text-info">[R]</span> ' . $proxyDb->error()]);
8181
} else {
82-
logger(MIGRATION_LOG, '<span class="text-info">[R]</span> query applied!');
82+
logger(MIGRATION_LOG, ['text' => '<span class="text-info">[R]</span> query applied!']);
8383
}
8484
}
8585

@@ -92,13 +92,13 @@
9292
)";
9393

9494
foreach ($q as $query) {
95-
logger(MIGRATION_LOG, '<span class="text-success">[Q]</span> ' . preg_replace('!\s+!', ' ', $query));
95+
logger(MIGRATION_LOG, ['text' => '<span class="text-success">[Q]</span> ' . preg_replace('!\s+!', ' ', $query)]);
9696

9797
$usageDb->query($query);
9898

9999
if ($usageDb->error() != 'not an error') {
100-
logger(MIGRATION_LOG, '<span class="text-info">[R]</span> ' . $usageDb->error(), 'error');
100+
logger(MIGRATION_LOG, ['text' => '<span class="text-info">[R]</span> ' . $usageDb->error()]);
101101
} else {
102-
logger(MIGRATION_LOG, '<span class="text-info">[R]</span> query applied!');
102+
logger(MIGRATION_LOG, ['text' => '<span class="text-info">[R]</span> query applied!']);
103103
}
104104
}

root/app/www/public/migrations/002_settings.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@
3131
VALUES " . implode(', ', $settingRows);
3232

3333
foreach ($q as $query) {
34-
logger(MIGRATION_LOG, '<span class="text-success">[Q]</span> ' . preg_replace('!\s+!', ' ', $query));
34+
logger(MIGRATION_LOG, ['text' => '<span class="text-success">[Q]</span> ' . preg_replace('!\s+!', ' ', $query)]);
3535

3636
$proxyDb->query($query);
3737

3838
if ($proxyDb->error() != 'not an error') {
39-
logger(MIGRATION_LOG, '<span class="text-info">[R]</span> ' . $proxyDb->error(), 'error');
39+
logger(MIGRATION_LOG, ['text' => '<span class="text-info">[R]</span> ' . $proxyDb->error()]);
4040
} else {
41-
logger(MIGRATION_LOG, '<span class="text-info">[R]</span> query applied!');
41+
logger(MIGRATION_LOG, ['text' => '<span class="text-info">[R]</span> query applied!']);
4242
}
4343
}

root/app/www/public/migrations/003_notifications.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020
WHERE id = " . NotificationPlatforms::TELEGRAM;
2121

2222
foreach ($q as $query) {
23-
logger(MIGRATION_LOG, '<span class="text-success">[Q]</span> ' . preg_replace('!\s+!', ' ', $query));
23+
logger(MIGRATION_LOG, ['text' => '<span class="text-success">[Q]</span> ' . preg_replace('!\s+!', ' ', $query)]);
2424

2525
$proxyDb->query($query);
2626

27-
if ($proxyDb->error() != 'not an error') {
28-
logger(MIGRATION_LOG, '<span class="text-info">[R]</span> ' . $proxyDb->error(), 'error');
29-
} else {
30-
logger(MIGRATION_LOG, '<span class="text-info">[R]</span> query applied!');
31-
}
27+
if ($proxyDb->error() != 'not an error') {
28+
logger(MIGRATION_LOG, ['text' => '<span class="text-info">[R]</span> ' . $proxyDb->error()]);
29+
} else {
30+
logger(MIGRATION_LOG, ['text' => '<span class="text-info">[R]</span> query applied!']);
31+
}
3232
}

0 commit comments

Comments
 (0)