BxDolLog.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCore UNA Core
  7. * @{
  8. */
  9. class BxDolLog extends BxDolFactory implements iBxDolSingleton
  10. {
  11. protected $_sFile;
  12. protected $_sName;
  13. protected function __construct()
  14. {
  15. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  16. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  17. parent::__construct();
  18. }
  19. /**
  20. * Prevent cloning the instance
  21. */
  22. public function __clone()
  23. {
  24. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  25. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  26. }
  27. /**
  28. * Get singleton instance of the class
  29. */
  30. public static function getInstance()
  31. {
  32. if (!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  33. $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolLog();
  34. $GLOBALS['bxDolClasses'][__CLASS__]->reset();
  35. return $GLOBALS['bxDolClasses'][__CLASS__];
  36. }
  37. /**
  38. * Reset custom log file and log name
  39. * to be empty to ready for future usage.
  40. */
  41. public function reset()
  42. {
  43. $this->_sFile = '';
  44. $this->_sName = '';
  45. }
  46. /**
  47. * Set custom log file. The file should have necessary permissions.
  48. * @param string $sFile - full path to custom log file.
  49. */
  50. public function setFile($sFile)
  51. {
  52. $this->_sFile = $sFile;
  53. }
  54. /**
  55. * Set log name. Log file will be created\updated in standard log directory.
  56. * @param string $sName - log file name without path and extension.
  57. */
  58. public function setName($sName)
  59. {
  60. $this->_sName = $sName;
  61. }
  62. /**
  63. * Writes content to a log file. Can get any number of arguments to be written
  64. * in one write session. Each argument can be a string, an array or an object.
  65. */
  66. public function write()
  67. {
  68. if(func_num_args() == 0)
  69. return;
  70. try {
  71. $sFile = '';
  72. if(!empty($this->_sFile))
  73. $sFile = $this->_sFile;
  74. else
  75. $sFile = BX_DIRECTORY_PATH_LOGS . '/' . $this->_sName . '.log';
  76. if(empty($sFile))
  77. return;
  78. file_put_contents($sFile, date('m-d H:i:s') . ":\n", FILE_APPEND);
  79. $aArgs = func_get_args();
  80. foreach($aArgs as $mixedArg) {
  81. if(is_array($mixedArg))
  82. $mixedArg = var_export($mixedArg, true);
  83. else if(is_object($mixedArg))
  84. $mixedArg = json_encode($mixedArg);
  85. file_put_contents($sFile, $mixedArg . "\n", FILE_APPEND);
  86. }
  87. }
  88. catch (Exception $oException) {
  89. echo 'Error: ' . $oException->getMessage();
  90. }
  91. }
  92. }
  93. /** @} */