LowLevelLog.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace ModulesGarden\Servers\ProxmoxVps\Core\App;
  3. class LowLevelLog
  4. {
  5. protected $logType = null;
  6. protected $logToken = null;
  7. protected $logTime = null;
  8. protected $moduleName = null;
  9. public function __construct($logType, $logToken, $logTime)
  10. {
  11. $this->logType = $logType;
  12. $this->logToken = $logToken;
  13. $this->logTime = $logTime;
  14. }
  15. public function makeLogs($logDetails)
  16. {
  17. if ($this->logType === 'error')
  18. {
  19. $this->logToDb($logDetails);
  20. }
  21. $this->logToFile($logDetails);
  22. if ($this->logType === 'error')
  23. {
  24. $this->logToPHPLog($logDetails);
  25. }
  26. }
  27. public function logToDb($logDetails = [])
  28. {
  29. \logModuleCall(
  30. $this->getModuleName() . ' Error',
  31. 'Token: ' . $this->logToken,
  32. ['time' => $this->logTime],
  33. var_export($logDetails, true)
  34. );
  35. }
  36. public function logToFile($logDetails)
  37. {
  38. return;
  39. $logData = date('d.m.Y H:i:s', time()) . ' - Token: ' . $this->logToken . ' - ' . var_export($logDetails, true) . PHP_EOL;
  40. $logFile = $this->getLogFile($this->logType);
  41. file_put_contents($logFile, $logData, FILE_APPEND);
  42. }
  43. public function logToPHPLog($logDetails)
  44. {
  45. }
  46. public function getLogFile($logType = 'error')
  47. {
  48. $logDir = dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR . 'storage' . DIRECTORY_SEPARATOR . 'logs';
  49. $logFile = $logDir . DIRECTORY_SEPARATOR . $logType . '.log';
  50. if (!file_exists($logFile) && !is_writable($logDir))
  51. {
  52. //throw new \Exception('Insufficient permissions for directory: ' . $logDir);
  53. }
  54. if (file_exists($logFile) && !is_writable($logFile))
  55. {
  56. //throw new \Exception('Insufficient permissions for file: ' . $logFile);
  57. }
  58. return $logFile;
  59. }
  60. public function getModuleName()
  61. {
  62. if (!$this->moduleName)
  63. {
  64. $className = trim(self::class, '\\');
  65. if (strpos($className, 'ModulesGarden') === 0)
  66. {
  67. $pt1 = str_replace('ModulesGarden\\', '', $className);
  68. if (strpos($pt1, '\Core\App'))
  69. {
  70. $this->moduleName = substr($pt1, 0, strpos($pt1, '\Core\App'));
  71. }
  72. }
  73. }
  74. return $this->moduleName;
  75. }
  76. }