Skip to content

Commit

Permalink
Server Logging override
Browse files Browse the repository at this point in the history
If on the mothership, dump logs to central location.
  • Loading branch information
VaMaster committed Feb 2, 2019
1 parent 292cf94 commit fe202ca
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 45 deletions.
5 changes: 3 additions & 2 deletions php/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"kryptonit3/sonarr": "^1.0",
"guzzlehttp/guzzle": "^6.2",
"symfony/polyfill-mbstring": "^1.2",
"kryptonit3/couchpotato": "1.0.0"
}
"kryptonit3/couchpotato": "1.0.0",
"ext-json": "*"
}
}
89 changes: 46 additions & 43 deletions php/util.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require_once dirname(__FILE__) . '/vendor/autoload.php';
require_once dirname(__FILE__) . '/JsonXmlElement.php';
require_once dirname(__FILE__) . '/multiCurl.php';
if (file_exists('/var/www/util/serverUtil.php')) require_once '/var/www/util/serverUtil.php';

function array_diff_assoc_recursive($array1, $array2)
{
Expand Down Expand Up @@ -2509,49 +2510,51 @@ function translateControl($string, $searchArray) {
return $string;
}

function write_log($text, $level = false, $caller = false, $force=false, $skip=false) {
$log = file_build_path(dirname(__FILE__), '..', 'logs', "Phlex.log.php");
$pp = false;
if ($force && isset($_GET['pollPlayer'])) {
$pp = true;
unset($_GET['pollPlayer']);
}
if (!file_exists($log)) {
touch($log);
chmod($log, 0666);
$authString = "; <?php die('Access denied'); ?>".PHP_EOL;
file_put_contents($log,$authString);
}
if (filesize($log) > 10485760) {
$oldLog = file_build_path(dirname(__FILE__),"..",'logs',"Phlex.log.php.old");
if (file_exists($oldLog)) unlink($oldLog);
rename($log, $oldLog);
touch($log);
chmod($log, 0666);
$authString = "; <?php die('Access denied'); ?>".PHP_EOL;
file_put_contents($log,$authString);
}

$aux = microtime(true);
$now = DateTime::createFromFormat('U.u', $aux);
if (is_bool($now)) $now = DateTime::createFromFormat('U.u', $aux += 0.001);
$date = $now->format("m-d-Y H:i:s.u");
$level = $level ? $level : "DEBUG";
$user = $_SESSION['plexUserName'] ?? false;
$user = $user ? "[$user] " : "";
$caller = $caller ? getCaller($caller) : getCaller();
if (!$skip) $text = protectMessage(($text));

if ((isset($_GET['pollPlayer']) || isset($_GET['passive'])) || ($text === "") || !file_exists($log)) return;

$line = "[$date] [$level] ".$user."[$caller] - $text".PHP_EOL;

if ($pp) $_SESSION['pollPlayer'] = true;
if (!is_writable($log)) return;
if (!$handle = fopen($log, 'a+')) return;
if (fwrite($handle, $line) === FALSE) return;

fclose($handle);
if (!function_exists('write_log')) {
function write_log($text, $level = false, $caller = false, $force = false, $skip = false) {
$log = file_build_path(dirname(__FILE__), '..', 'logs', "Phlex.log.php");
$pp = false;
if ($force && isset($_GET['pollPlayer'])) {
$pp = true;
unset($_GET['pollPlayer']);
}
if (!file_exists($log)) {
touch($log);
chmod($log, 0666);
$authString = "; <?php die('Access denied'); ?>" . PHP_EOL;
file_put_contents($log, $authString);
}
if (filesize($log) > 10485760) {
$oldLog = file_build_path(dirname(__FILE__), "..", 'logs', "Phlex.log.php.old");
if (file_exists($oldLog)) unlink($oldLog);
rename($log, $oldLog);
touch($log);
chmod($log, 0666);
$authString = "; <?php die('Access denied'); ?>" . PHP_EOL;
file_put_contents($log, $authString);
}

$aux = microtime(true);
$now = DateTime::createFromFormat('U.u', $aux);
if (is_bool($now)) $now = DateTime::createFromFormat('U.u', $aux += 0.001);
$date = $now->format("m-d-Y H:i:s.u");
$level = $level ? $level : "DEBUG";
$user = $_SESSION['plexUserName'] ?? false;
$user = $user ? "[$user] " : "";
$caller = $caller ? getCaller($caller) : getCaller();
if (!$skip) $text = protectMessage(($text));

if ((isset($_GET['pollPlayer']) || isset($_GET['passive'])) || ($text === "") || !file_exists($log)) return;

$line = "[$date] [$level] " . $user . "[$caller] - $text" . PHP_EOL;

if ($pp) $_SESSION['pollPlayer'] = true;
if (!is_writable($log)) return;
if (!$handle = fopen($log, 'a+')) return;
if (fwrite($handle, $line) === FALSE) return;

fclose($handle);
}
}

function writeSession($key, $value, $unset = false) {
Expand Down
2 changes: 2 additions & 0 deletions php/webApp.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ function scriptDefaults() {
ini_set('max_execution_time', 300);
error_reporting(E_ERROR);
$errorLogPath = file_build_path(dirname(__FILE__),'..', 'logs', 'Phlex_error.log.php');
$host = gethostname();
if (file_exists("/var/s3bucket/$host.log")) $errorLogPath = "/var/s3bucket/$host.log";
ini_set("error_log", $errorLogPath);
date_default_timezone_set((date_default_timezone_get() ? date_default_timezone_get() : "America/Chicago"));
}
Expand Down

0 comments on commit fe202ca

Please sign in to comment.