LowLevelLog.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\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. $logData = date('d.m.Y H:i:s', time()) . ' - Token: ' . $this->logToken . ' - ' . var_export($logDetails, true) . PHP_EOL;
  39. $logFile = $this->getLogFile($this->logType);
  40. file_put_contents($logFile, $logData, FILE_APPEND);
  41. }
  42. public function logToPHPLog($logDetails)
  43. {
  44. }
  45. public function getLogFile($logType = 'error')
  46. {
  47. $logDir = dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR . 'storage' . DIRECTORY_SEPARATOR . 'logs';
  48. $logFile = $logDir . DIRECTORY_SEPARATOR . $logType . '.log';
  49. if (!file_exists($logFile) && !is_writable($logDir))
  50. {
  51. //throw new \Exception('Insufficient permissions for directory: ' . $logDir);
  52. }
  53. if (file_exists($logFile) && !is_writable($logFile))
  54. {
  55. //throw new \Exception('Insufficient permissions for file: ' . $logFile);
  56. }
  57. return $logFile;
  58. }
  59. public function getModuleName()
  60. {
  61. if (!$this->moduleName)
  62. {
  63. $className = trim(self::class, '\\');
  64. if (strpos($className, 'ThurData') === 0)
  65. {
  66. $pt1 = str_replace('ThurData\\', '', $className);
  67. if (strpos($pt1, '\Core\App'))
  68. {
  69. $this->moduleName = substr($pt1, 0, strpos($pt1, '\Core\App'));
  70. }
  71. }
  72. }
  73. return $this->moduleName;
  74. }
  75. }