polish the api route, response, health, cache, and metrics
This commit is contained in:
28
app/helpers/logger.php
Normal file
28
app/helpers/logger.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
// Simple file logger
|
||||
function log_start() {
|
||||
$id = bin2hex(random_bytes(4));
|
||||
$GLOBALS['req_start'] = microtime(true);
|
||||
header("X-Request-Id: $id");
|
||||
return $id;
|
||||
}
|
||||
function log_end($id, $status=200) {
|
||||
$ms = round((microtime(true)-$GLOBALS['req_start'])*1000);
|
||||
$ip = $_SERVER['REMOTE_ADDR'] ?? '-';
|
||||
$path = $_SERVER['REQUEST_URI'] ?? '-';
|
||||
$plan = $_SERVER['HTTP_X_DEWEMOJI_PLAN'] ?? ($GLOBALS['bucket_plan'] ?? '-');
|
||||
$bucket = $GLOBALS['bucket_id'] ?? '-';
|
||||
$ua = $_SERVER['HTTP_USER_AGENT'] ?? '-';
|
||||
$ref = $_SERVER['HTTP_REFERER'] ?? '-';
|
||||
$errorMsg = '';
|
||||
if ($status >= 400) {
|
||||
$errorMsg = $GLOBALS['json_error_msg'] ?? '';
|
||||
}
|
||||
$line = sprintf("[%s] %s %s %s plan=%s bucket=%s %d %dms ua=\"%s\" ref=\"%s\"%s\n",
|
||||
gmdate('c'), $id, $ip, $path, $plan, $bucket, $status, $ms, $ua, $ref,
|
||||
$errorMsg ? " error=\"".addslashes($errorMsg)."\"" : ''
|
||||
);
|
||||
$logFile = __DIR__.'/../../storage/logs/access.log';
|
||||
@mkdir(dirname($logFile), 0775, true);
|
||||
error_log($line, 3, $logFile);
|
||||
}
|
||||
Reference in New Issue
Block a user