BxDolLogs.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. $sClass = 'BxDolLogs';
  71. if(!empty($aObject['class_name'])) {
  72. $sClass = $aObject['class_name'];
  73. if(!empty($aObject['class_file']))
  74. require_once(BX_DIRECTORY_PATH_ROOT . $aObject['class_file']);
  75. }
  76. $sLogsStorage = (empty($aObject['logs_storage']) ? 'Auto' : $aObject['logs_storage']);
  77. if ('Auto' == $sLogsStorage)
  78. $sLogsStorage = getParam('sys_logs_storage_default');
  79. $sClassLogsStorage = 'BxDolLogsStorage' . $sLogsStorage;
  80. $oLogsStorage = $sClassLogsStorage::getInstance();
  81. $o = new $sClass($aObject, $oLogsStorage);
  82. return ($GLOBALS['bxDolClasses']['BxDolLogs!' . $sObject] = $o);
  83. }
  84. /**
  85. * Get current logs object name
  86. */
  87. public function getObjectName()
  88. {
  89. return $this->_aObject['object'];
  90. }
  91. /**
  92. * Log to the current logs storage object
  93. * @param $mixed string or array to log
  94. * @return true on success or false on error
  95. */
  96. public function add($mixed)
  97. {
  98. return $this->_oLogsStorage->add($this, $mixed);
  99. }
  100. /**
  101. * Get logs from current logs storage
  102. * @param $iLines number of lines from the tail to return
  103. * @return array of strings
  104. */
  105. public function get($iLines = 30)
  106. {
  107. return $this->_oLogsStorage->add($this, $mixed);
  108. }
  109. }
  110. /** @} */