BxDolLogs.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. * It's also possible to not add record to sys_objects_logs table,
  26. * then it will use default settings for logging.
  27. *
  28. * @section example Example of usage
  29. *
  30. * Log data to the logs object
  31. *
  32. * @code
  33. * if ($o = BxDolLogs::getObjectInstance('my_module_logs_object'))
  34. * $o->add($s);
  35. * @endcode
  36. *
  37. * or
  38. *
  39. * @code
  40. * bx_log('my_module_logs_object', $s);
  41. * @endcode
  42. *
  43. */
  44. class BxDolLogs extends BxDolFactory implements iBxDolFactoryObject
  45. {
  46. protected $_oDb;
  47. protected $_sObject;
  48. protected $_aObject;
  49. protected $_oLogsStorage;
  50. /**
  51. * Constructor
  52. */
  53. protected function __construct($aObject, $oLogsStorage)
  54. {
  55. parent::__construct();
  56. $this->_aObject = $aObject;
  57. $this->_sObject = $aObject['object'];
  58. $this->_oLogsStorage = $oLogsStorage;
  59. $this->_oDb = new BxDolLogsQuery($this->_aObject);
  60. }
  61. /**
  62. * Get logs object instance by object name
  63. * @param $sObject object name
  64. * @return object instance or false on error
  65. */
  66. public static function getObjectInstance($sObject)
  67. {
  68. if(isset($GLOBALS['bxDolClasses']['BxDolLogs!' . $sObject]))
  69. return $GLOBALS['bxDolClasses']['BxDolLogs!' . $sObject];
  70. $aObject = BxDolLogsQuery::getLogsObject($sObject);
  71. if($aObject && is_array($aObject) && !$aObject['active'])
  72. return false;
  73. if(!$aObject) {
  74. $aObject = array(
  75. 'object' => $sObject,
  76. 'module' => 'system',
  77. 'logs_storage' => getParam('sys_logs_storage_default') ?: 'Folder',
  78. );
  79. }
  80. $sClass = 'BxDolLogs';
  81. if(!empty($aObject['class_name'])) {
  82. $sClass = $aObject['class_name'];
  83. if(!empty($aObject['class_file']))
  84. require_once(BX_DIRECTORY_PATH_ROOT . $aObject['class_file']);
  85. }
  86. $sLogsStorage = (empty($aObject['logs_storage']) ? 'Auto' : $aObject['logs_storage']);
  87. if ('Auto' == $sLogsStorage)
  88. $sLogsStorage = getParam('sys_logs_storage_default') ?: 'Folder';
  89. $sClassLogsStorage = 'BxDolLogsStorage' . $sLogsStorage;
  90. $oLogsStorage = $sClassLogsStorage::getInstance();
  91. $o = new $sClass($aObject, $oLogsStorage);
  92. return ($GLOBALS['bxDolClasses']['BxDolLogs!' . $sObject] = $o);
  93. }
  94. static public function getLevel2NameMap()
  95. {
  96. return [
  97. BX_LOG_ERR => 'ERROR',
  98. BX_LOG_WARN => 'WARNING',
  99. BX_LOG_INFO => 'INFO',
  100. BX_LOG_DEBUG => 'DEBUG',
  101. ];
  102. }
  103. static public function getName2LevelsMap()
  104. {
  105. return [
  106. 'ERROR' => BX_LOG_ERR,
  107. 'WARNING' => BX_LOG_WARN | BX_LOG_ERR,
  108. 'INFO' => BX_LOG_INFO | BX_LOG_WARN | BX_LOG_ERR,
  109. 'DEBUG' => BX_LOG_DEBUG | BX_LOG_INFO | BX_LOG_WARN | BX_LOG_ERR,
  110. ];
  111. }
  112. /**
  113. * Get current logs object name
  114. */
  115. public function getObjectName()
  116. {
  117. return $this->_aObject['object'];
  118. }
  119. /**
  120. * Log to the current logs storage object
  121. * @param $mixed string or array to log
  122. * @return true on success or false on error
  123. */
  124. public function add($mixed, $iLevel = BX_LOG_DEBUG)
  125. {
  126. $a = $this->getName2LevelsMap();
  127. $sLogLevel = getParam('sys_logs_level');
  128. if (isset($a[$sLogLevel]) && ($a[$sLogLevel] & $iLevel))
  129. return $this->_oLogsStorage->add($this, $mixed, $iLevel);
  130. return true;
  131. }
  132. /**
  133. * Get logs from current logs storage
  134. * @param $iLines number of lines from the tail to return
  135. * @param $sFilter filter lines by keyword
  136. * @return array of strings or false on error
  137. */
  138. public function get($iLines = 30, $sFilter = '')
  139. {
  140. if (!$this->_oLogsStorage->isGetAvail())
  141. return false;
  142. return $this->_oLogsStorage->get($this, $iLines, $sFilter);
  143. }
  144. /**
  145. * Check if filtering supported in `get` method
  146. */
  147. public function isFilterAvail()
  148. {
  149. return $this->_oLogsStorage->isFilterAvail();
  150. }
  151. /**
  152. * Check if `get` method available
  153. */
  154. public function isGetAvail()
  155. {
  156. return $this->_oLogsStorage->isGetAvail();
  157. }
  158. }
  159. /** @} */