Add Redis cache configuration and temp logging functionality

This commit is contained in:
alexveebee 2025-04-22 20:22:08 +00:00
parent ec2f55b88f
commit 38d96b80a5
3 changed files with 102 additions and 21 deletions

69
inc/_LOGGER_.php Executable file
View File

@ -0,0 +1,69 @@
<?php
class logger {
private static $log_file = "log.txt";
private static $log_file_path = "/var/www/logs/";
public static function log($message, $includeDate = true) {
// check if directory exists
if (!file_exists(self::$log_file_path)) {
mkdir(self::$log_file_path, 0777, true);
}
// check if file exists
if (!file_exists(self::$log_file_path . "/" . self::$log_file)) {
file_put_contents(self::$log_file_path . "/" . self::$log_file, "");
}
if ($includeDate) {
$date = date('Y-m-d H:i:s');
$ip = $_SERVER['REMOTE_ADDR'];
$log = "$date - $ip \n$message\n-------------------\n";
} else {
$log = "$message\n-------------------\n";
}
file_put_contents(self::$log_file_path . "/" . self::$log_file, $log, FILE_APPEND);
}
public static function arrayToLog($array, $includeDate = true) {
$log = print_r($array, true);
self::log($log, $includeDate);
}
public static function stackTraceLog( $callStack, $includeDate = true ) {
$log = "Stack trace:\n";
try {
// $log .= "Got: " . $callStack[0]['file'] . " : " . $callStack[0]['line'] . "\n";
foreach ($callStack as $key => $value) {
$function = isset($value['function']) ? $value['function'] : "unknown";
$file = isset($value['file']) ? $value['file'] : "unknown";
$line = isset($value['line']) ? $value['line'] : "unknown";
$message = isset($value['args']) ? print_r($value['args'], true) : "unknown";
// if ($key == 0) {
// $log .= " " . $function . " at: " . $file . " : " . $line . "\n";
// $log .= " " . $message[1] . "\n";
// continue;
// }
$log .= " " . $function . " at: " . $file . " : " . $line . "\n";
}
} catch (Exception $e) {
echo '<pre>';
echo "Original stack trace: \n";
echo $e->getMessage();
print_r($callStack->getTrace());
echo '</pre>';
echo '<pre>';
echo 'Current stack trace: \n';
echo $e->getMessage();
print_r($e->getTrace());
echo '</pre>';
die("Unable to parse stack trace");
}
self::log($log, $includeDate);
}
}

View File

@ -12,6 +12,7 @@ defined('TINYBOARD') or exit;
class Cache {
private static function buildCache(): CacheDriver {
global $config;
$isDocker = is_file("/.dockerenv") || is_file("/run/.containerenv");
switch ($config['cache']['enabled']) {
case 'memcached':
@ -20,12 +21,31 @@ class Cache {
$config['cache']['memcached']
);
case 'redis':
$host = $config['cache']['redis'][0] ?? 'localhost';
$port = $config['cache']['redis'][1] ?? 6379;
$password = $config['cache']['redis'][2] ?? '';
$database = $config['cache']['redis'][3] ?? 1;
if ($isDocker) {
$host = getenv('VICHAN_REDIS_HOST') ?: $host;
$port = getenv('VICHAN_REDIS_PORT') ?: $port;
$password = getenv('VICHAN_REDIS_PASSWORD') ?: $password;
$database = getenv('VICHAN_REDIS_DATABASE') ?: $database;
}
// $log->log("Cache info:\n"
// . 'Cache driver: redis' . "\n"
// . '├ Redis host: ' . $host . "\n"
// . '├ Redis port: ' . $port . "\n"
// . '├ Redis password: ' . str_repeat('*', strlen($password)) . "\n"
// . '└ Redis database: ' . $database . "\n"
// );
return new RedisCacheDriver(
$config['cache']['prefix'],
$config['cache']['redis'][0],
$config['cache']['redis'][1],
$config['cache']['redis'][2],
$config['cache']['redis'][3]
$host,
$port,
$password,
$database
);
case 'apcu':
return new ApcuCacheDriver;

View File

@ -143,27 +143,10 @@
// Configure cache
if ($redis_enabled) {
$config['cache']['enabled'] = 'redis';
$config['cache']['redis'] = [
'host' => getenv('VICHAN_REDIS_HOST') ?: 'localhost',
'port' => (int)(getenv('VICHAN_REDIS_PORT') ?: 6379),
'password' => getenv('VICHAN_REDIS_PASSWORD') ?: '',
'database' => 1,
];
} else {
$config['cache']['enabled'] = 'php';
}
// Configure sessions to use Redis if enabled
if ($redis_enabled) {
$config['session']['enabled'] = 'redis';
$config['session']['redis'] = [
'host' => getenv('VICHAN_REDIS_HOST') ?: 'localhost',
'port' => (int)(getenv('VICHAN_REDIS_PORT') ?: 6379),
'password' => getenv('VICHAN_REDIS_PASSWORD') ?: '',
'database' => 1,
];
}
// Cache timeout for cached objects
$config['cache']['timeout'] = 60 * 60 * 48; // 48 hours
@ -1869,6 +1852,15 @@
// Enable public logs? 0: NO, 1: YES, 2: YES, but drop names
$config['public_logs'] = 0;
/*
* ====================
* Docker settings
* ===================
*/
$isDocker = is_file("/.dockerenv") || is_file("/run/.containerenv");
$config['docker'] = $isDocker;
/*
* ====================
* Events (PHP 5.3.0+)