PageRenderTime 43ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/openbiz/bin/service/logService.php

https://github.com/asipto/siremis
PHP | 406 lines | 241 code | 24 blank | 141 comment | 24 complexity | d4a58e28462fc0b51138772155bf3610 MD5 | raw file
  1. <?php
  2. /**
  3. * PHPOpenBiz Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. *
  10. * @package openbiz.bin.service
  11. * @copyright Copyright &copy; 2005-2009, Rocky Swen
  12. * @license http://www.opensource.org/licenses/bsd-license.php
  13. * @link http://www.phpopenbiz.org/
  14. * @version $Id$
  15. */
  16. require_once 'Zend/Log.php';
  17. require_once 'Zend/Log/Writer/Stream.php';
  18. /**
  19. * ioService class is the plug-in service of handling file import/export
  20. *
  21. * @package openbiz.bin.service
  22. * @author Rocky Swen
  23. * @copyright Copyright (c) 2003-2009, Rocky Swen
  24. * @access public
  25. */
  26. class logService extends MetaObject
  27. {
  28. /**
  29. * Log Levels are the highest level of logging to record.
  30. * By default, will log all entries up to the selected level
  31. * If this is set to NULL, logging is disabled
  32. * EMERG = 0; // Emergency: system is unusable
  33. * ALERT = 1; // Alert: action must be taken immediately
  34. * CRIT = 2; // Critical: critical conditions
  35. * ERR = 3; // Error: error conditions
  36. * WARN = 4; // Warning: warning conditions
  37. * NOTICE = 5; // Notice: normal but significant condition
  38. * INFO = 6; // Informational: informational messages
  39. * DEBUG = 7; // Debug: debug messages
  40. *
  41. * @var int
  42. */
  43. private $_level;
  44. /**
  45. * Logging format options include CSV, XML and HTML
  46. *
  47. * @var string
  48. */
  49. private $_format;
  50. /**
  51. * Organize files by in different ways:
  52. * PROFILE - pased on a profile userID
  53. * LEVEL - Store log files groups by their level
  54. * DATE - write log entries into a different file for each day
  55. *
  56. * @var string
  57. */
  58. private $_org;
  59. /**
  60. * Holds the formatter for a log object. This object dictates how a log file is logged.
  61. *
  62. * @var object
  63. */
  64. private $_formatter;
  65. /**
  66. * Holds the file extension set based on the preferred format
  67. *
  68. * @var string
  69. */
  70. private $_extension = '.csv';
  71. /**
  72. * Initialize logService with xml array metadata
  73. *
  74. * @param array $xmlArr
  75. * @return void
  76. */
  77. public function __construct(&$xmlArr)
  78. {
  79. $this->readMetadata($xmlArr);
  80. //Check that logging is enabled
  81. if (is_int($this->_level) == FALSE)
  82. return;
  83. $this->setFormatter();
  84. $this->setExtension();
  85. }
  86. /**
  87. * Read array meta data, and store to meta object
  88. *
  89. * @param array $xmlArr
  90. * @return void
  91. */
  92. protected function readMetadata(&$xmlArr)
  93. {
  94. parent::readMetaData($xmlArr);
  95. $level = strtoupper($xmlArr["PLUGINSERVICE"]["LOG_CONFIG"]["ATTRIBUTES"]["LEVEL"]);
  96. $this->_level = (int) $level;
  97. $this->_format = strtoupper($xmlArr["PLUGINSERVICE"]["LOG_CONFIG"]["ATTRIBUTES"]["FORMAT"]);
  98. $this->_org = strtoupper($xmlArr["PLUGINSERVICE"]["LOG_CONFIG"]["ATTRIBUTES"]["ORG"]);
  99. }
  100. /**
  101. * The primary log function that will take an incoming message and specified paramter and log in the
  102. * appropriate location and format
  103. *
  104. * @param int $level - Follow common BSD error levels
  105. * @param string $package - A way to group log messages
  106. * @param string $message - The actual message to log
  107. * @return boolean -TRUE on success otherwise FALSE
  108. */
  109. public function log($priority, $package, $message)
  110. {
  111. if (DEBUG == 0 && $priority == LOG_DEBUG)
  112. return true;
  113. //Adapt PHP LOG priority
  114. switch ($priority)
  115. {
  116. case LOG_EMERG : // system is unusable
  117. case LOG_ALERT : // action must be taken immediately
  118. case LOG_CRIT : // critical conditions
  119. $level = 2;
  120. break;
  121. case LOG_ERR : // error conditions
  122. $level = 3;
  123. break;
  124. case LOG_WARNING : // warning conditions
  125. $level = 4;
  126. break;
  127. case LOG_NOTICE : // normal, but significant, condition
  128. case LOG_INFO : // informational message
  129. case LOG_DEBUG : // debug-level message
  130. $level = 7;
  131. break;
  132. }
  133. //echo "log levels: ".LOG_EMERG.", ".LOG_ALERT.", ".LOG_CRIT.", ".LOG_ERR.", ".LOG_WARNING.", ".LOG_NOTICE.", ".LOG_INFO.", ".LOG_DEBUG."<br>";
  134. if ($this->_level == FALSE)
  135. return true;
  136. //Get the file path
  137. $this->_level = $level;
  138. $path = $this->_getPath($file_name);
  139. $this->prepFile($path);
  140. //Create the log writer object
  141. $writer = new Zend_Log_Writer_Stream($path);
  142. $writer->setFormatter($this->_formatter);
  143. //Instantiate logging object
  144. $logger = new Zend_Log($writer);
  145. //Filter log entries to those allowed by configuration
  146. $filter = new Zend_Log_Filter_Priority($this->_level);
  147. $logger->addFilter($filter);
  148. $logger->setEventItem('back_trace', 'n/a');
  149. $logger->setEventItem('package', $package);
  150. $logger->setEventItem('date', date("m/d/Y"));
  151. $logger->setEventItem('time', date("H:i:s"));
  152. //Write Log Entry
  153. $logger->log($message, $level);
  154. $this->closeFile($path);
  155. return TRUE;
  156. }
  157. /**
  158. * The primary log function that will take an incoming message and specified paramter and log in the
  159. * appropriate location and format
  160. *
  161. * @param int $level - Follow common BSD error levels
  162. * @param string $package - A way to group log messages
  163. * @param string $message - The actual message to log
  164. * @param string $fileName - An optional specific file name where the message should be logged
  165. * @param string $string - An optional list of the Application stack at the time of error
  166. * @return boolean -TRUE on success otherwise FALSE
  167. */
  168. public function logError($level = 7, $package, $message, $fileName = NULL, $backTrace = '')
  169. {
  170. //Adapt PHP Errors
  171. switch ($level)
  172. {
  173. case 8: // E_NOTICE
  174. case 1024: // E_USER_NOTICE
  175. $level = 4;
  176. break;
  177. case 2048: //E_STRICT
  178. case 8192: //E_DEPRICIATED
  179. case 16384: //E_DEPRICIATED
  180. $level = 5;
  181. break;
  182. case 2: //E_WARNING
  183. case 512: //E_USER_WARNING
  184. $level = 3;
  185. break;
  186. case 256:
  187. $level = 2;
  188. break;
  189. default:
  190. $level = 2;
  191. }
  192. if ($this->_level == FALSE)
  193. return true;
  194. //Get the file path
  195. $this->_level = $level;
  196. $path = $this->_getPath($fileName);
  197. $this->prepFile($path);
  198. //Create the log writer object
  199. $writer = new Zend_Log_Writer_Stream($path);
  200. $writer->setFormatter($this->_formatter);
  201. //Instantiate logging object
  202. $logger = new Zend_Log($writer);
  203. //Filter log entries to those allowed by configuration
  204. $filter = new Zend_Log_Filter_Priority($this->_level);
  205. $logger->addFilter($filter);
  206. //Set additional information about the class and function that called the log
  207. // Add custom elements to default log package
  208. $logger->setEventItem('back_trace', $backTrace);
  209. $logger->setEventItem('package', $package);
  210. $logger->setEventItem('date', date("m/d/Y"));
  211. $logger->setEventItem('time', date("H:i:s"));
  212. //Write Log Entry
  213. $logger->log($message, $level);
  214. $this->closeFile($path);
  215. return TRUE;
  216. }
  217. /**
  218. * Set the formatter based on the type of format selected in app.inc
  219. *
  220. * @return void
  221. */
  222. public function setFormatter()
  223. {
  224. switch ($this->_format)
  225. {
  226. case 'HTML':
  227. //HTML Format
  228. $this->_formatter = new Zend_Log_Formatter_Simple('<tr><td>%date%</td> <td>%time%</td> <td>%priorityName%</td> <td>%package%</td> <td>%message%</td> <td>%back_trace%</td> </tr>' . PHP_EOL);
  229. break;
  230. case 'XML':
  231. //XML Format
  232. require_once 'Zend/Log/Formatter/Xml.php';
  233. $this->_formatter = new Zend_Log_Formatter_Xml();
  234. break;
  235. case 'CSV':
  236. default:
  237. //CSV Format
  238. $this->_formatter = new Zend_Log_Formatter_Simple("'%date%','%time%','%priorityName%','%package%','%message%','%back_trace%'" . PHP_EOL);
  239. break;
  240. }
  241. }
  242. /**
  243. * Set the extension based on the type of format selected in app.inc
  244. *
  245. * @return void
  246. */
  247. public function setExtension()
  248. {
  249. switch ($this->_format)
  250. {
  251. case 'HTML':
  252. $this->_extension = '.html';
  253. break;
  254. case 'XML':
  255. $this->_extension = '.xml';
  256. break;
  257. case 'CSV':
  258. default:
  259. $this->_extension = '.log';
  260. break;
  261. }
  262. }
  263. /**
  264. * Either open a new file with the correct string or remove a previous closing string
  265. *
  266. * @return void
  267. */
  268. public function prepFile($path)
  269. {
  270. //Check for existing file and HTML format
  271. if (file_exists($path) and $this->_format == 'HTML')
  272. {
  273. $file = file($path);
  274. array_pop($file);
  275. file_put_contents($path, $file);
  276. } elseif ($this->_format == 'HTML')
  277. {
  278. $html = '<html><head></head><body><table border="1">' . PHP_EOL;
  279. $file = fopen($path, 'a');
  280. fwrite($file, $html);
  281. fclose($file);
  282. } elseif (file_exists($path) and $this->_format == 'XML')
  283. {
  284. $file = file($path);
  285. array_pop($file);
  286. file_put_contents($path, $file);
  287. } elseif ($this->_format == 'XML')
  288. {
  289. $xml = '<?xml version="1.0" standalone="no"?><log>' . PHP_EOL;
  290. $file = fopen($path, 'a');
  291. fwrite($file, $xml);
  292. fclose($file);
  293. }
  294. }
  295. /**
  296. * Close a file with the correct string depending on the configured format
  297. *
  298. * @return void
  299. */
  300. public function closeFile($path)
  301. {
  302. //Close up the file if needed
  303. switch ($this->_format)
  304. {
  305. case 'HTML':
  306. $html = "</table></body></html>";
  307. $file = fopen($path, 'a');
  308. fwrite($file, $html);
  309. fclose($file);
  310. break;
  311. case 'XML':
  312. $xml = "</log>";
  313. $file = fopen($path, 'a');
  314. fwrite($file, $xml);
  315. fclose($file);
  316. break;
  317. default:
  318. break;
  319. }
  320. }
  321. /**
  322. * Get path based on config options
  323. *
  324. * @global BizSystem $g_BizSystem
  325. * @param string $fileName
  326. * @return string log_path - The path where a log entry should be written
  327. */
  328. private function _getPath($fileName = null)
  329. {
  330. if ($fileName)
  331. return LOG_PATH . '/' . $fileName . $this->_extension;
  332. switch ($this->_org)
  333. {
  334. case 'DATE':
  335. return LOG_PATH . '/' . date("Y_d_m") . $this->_extension;
  336. break;
  337. case 'LEVEL':
  338. switch ($this->_level)
  339. {
  340. case 0:
  341. $level = 'EMERG';
  342. break;
  343. case 1:
  344. $level = 'ALERT';
  345. break;
  346. case 2:
  347. $level = 'CRIT';
  348. break;
  349. case 3:
  350. $level = 'ERR';
  351. break;
  352. case 4:
  353. $level = 'WARN';
  354. break;
  355. case 5:
  356. $level = 'NOTICE';
  357. break;
  358. case 6:
  359. $level = 'INFO';
  360. break;
  361. case 7:
  362. $level = 'DEBUG';
  363. break;
  364. default:
  365. ;
  366. break;
  367. }
  368. return LOG_PATH . '/' . $level . $this->_extension;
  369. break;
  370. case 'PROFILE':
  371. $profile = BizSystem::getUserProfile('USERID');
  372. if (! $profile)
  373. $profile = 'Guest';
  374. return LOG_PATH . '/' . $profile . $this->_extension;
  375. break;
  376. default:
  377. ;
  378. break;
  379. }
  380. }
  381. }