BxDolLogs.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. /**
  10. * This class unifies the usage of logs.
  11. *
  12. * Add record to sys_objects_logs table, like you are doing this for Comments or Voting objects:
  13. * - object: your logs object name, usually it is in the following format - vendor prefix, underscore, module prefix;
  14. * - module: module name
  15. * - logs_storage: logs storage, supported storages:
  16. * - `Auto`: log to the storage currently configured in settings
  17. * - `Folder`: files in `logs` folder
  18. * - `PHPLog`: php log
  19. * - `STDErr`: standard error output, usually written to web server error log
  20. * - title: translatable title
  21. * - active: 0 or 1
  22. * - class_name: user defined class name which is derived from BxDolLogs.
  23. * - class_file: the location of the user defined class, leave it empty if class is located in system folders.
  24. *
  25. * @section example Example of usage
  26. *
  27. * Log data to the logs object
  28. *
  29. * @code
  30. * if ($o = BxDolLogs::getObjectInstance('my_module_logs_object'))
  31. * $o->add($s);
  32. * @endcode
  33. *
  34. * or
  35. *
  36. * @code
  37. * bx_log('my_module_logs_object', $s);
  38. * @endcode
  39. *
  40. */
  41. class BxDolLogs extends BxDolFactory implements iBxDolFactoryObject
  42. {
  43. protected $_oDb;
  44. protected $_sObject;
  45. protected $_aObject;
  46. protected $_oLogsStorage;
  47. /**
  48. * Constructor
  49. */
  50. protected function __construct($aObject, $oLogsStorage)
  51. {
  52. parent::__construct();
  53. $this->_aObject = $aObject;
  54. $this->_sObject = $aObject['object'];
  55. $this->_oLogsStorage = $oLogsStorage;
  56. $this->_oDb = new BxDolLogsQuery($this->_aObject);
  57. }
  58. /**
  59. * Get logs object instance by object name
  60. * @param $sObject object name
  61. * @return object instance or false on error
  62. */
  63. public static function getObjectInstance($sObject)
  64. {
  65. if(isset($GLOBALS['bxDolClasses']['BxDolLogs!' . $sObject]))
  66. return $GLOBALS['bxDolClasses']['BxDolLogs!' . $sObject];
  67. $aObject = BxDolLogsQuery::getLogsObject($sObject);
  68. if($aObject && is_array($aObject) && !$aObject['active'])
  69. return false;
  70. if(!$aObject) {
  71. $aObject = array(
  72. 'object' => $sObject,
  73. 'module' => 'system',
  74. 'logs_storage' => getParam('sys_logs_storage_default'),
  75. );
  76. }
  77. $sClass = 'BxDolLogs';
  78. if(!empty($aObject['class_name'])) {
  79. $sClass = $aObject['class_name'];
  80. if(!empty($aObject['class_file']))
  81. require_once(BX_DIRECTORY_PATH_ROOT . $aObject['class_file']);
  82. }
  83. $sLogsStorage = (empty($aObject['logs_storage']) ? 'Auto' : $aObject['logs_storage']);
  84. if ('Auto' == $sLogsStorage)
  85. $sLogsStorage = getParam('sys_logs_storage_default');
  86. $sClassLogsStorage = 'BxDolLogsStorage' . $sLogsStorage;
  87. $oLogsStorage = $sClassLogsStorage::getInstance();
  88. $o = new $sClass($aObject, $oLogsStorage);
  89. return ($GLOBALS['bxDolClasses']['BxDolLogs!' . $sObject] = $o);
  90. }
  91. /**
  92. * Get current logs object name
  93. */
  94. public function getObjectName()
  95. {
  96. return $this->_aObject['object'];
  97. }
  98. /**
  99. * Log to the current logs storage object
  100. * @param $mixed string or array to log
  101. * @return true on success or false on error
  102. */
  103. public function add($mixed)
  104. {
  105. return $this->_oLogsStorage->add($this, $mixed);
  106. }
  107. /**
  108. * Get logs from current logs storage
  109. * @param $iLines number of lines from the tail to return
  110. * @param $sFilter filter lines by keyword
  111. * @return array of strings or false on error
  112. */
  113. public function get($iLines = 30, $sFilter = '')
  114. {
  115. if (!$this->_oLogsStorage->isGetAvail())
  116. return false;
  117. return $this->_oLogsStorage->get($this, $iLines, $sFilter);
  118. }
  119. /**
  120. * Check if filtering supported in `get` method
  121. */
  122. public function isFilterAvail()
  123. {
  124. return $this->_oLogsStorage->isFilterAvail();
  125. }
  126. /**
  127. * Check if `get` method available
  128. */
  129. public function isGetAvail()
  130. {
  131. return $this->_oLogsStorage->isGetAvail();
  132. }
  133. }
  134. /** @} */