PageRenderTime 60ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/cubi/openbiz/bin/service/logService.php

http://openbiz-cubi.googlecode.com/
PHP | 439 lines | 272 code | 25 blank | 142 comment | 30 complexity | 2f791b4decfd607b57c0213398e84171 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0
  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 (c) 2005-2011, Rocky Swen
  12. * @license http://www.opensource.org/licenses/bsd-license.php
  13. * @link http://www.phpopenbiz.org/
  14. * @version $Id: logService.php 4042 2011-05-01 06:36:18Z rockys $
  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. private $_daystolive;
  72. /**
  73. * Initialize logService with xml array metadata
  74. *
  75. * @param array $xmlArr
  76. * @return void
  77. */
  78. public function __construct(&$xmlArr)
  79. {
  80. $this->readMetadata($xmlArr);
  81. //Check that logging is enabled
  82. if (is_int($this->_level) == FALSE)
  83. return;
  84. $this->setFormatter();
  85. $this->setExtension();
  86. }
  87. /**
  88. * Read array meta data, and store to meta object
  89. *
  90. * @param array $xmlArr
  91. * @return void
  92. */
  93. protected function readMetadata(&$xmlArr)
  94. {
  95. parent::readMetaData($xmlArr);
  96. $level = strtoupper($xmlArr["PLUGINSERVICE"]["LOG_CONFIG"]["ATTRIBUTES"]["LEVEL"]);
  97. $this->_level = (int) $level;
  98. $this->_format = strtoupper($xmlArr["PLUGINSERVICE"]["LOG_CONFIG"]["ATTRIBUTES"]["FORMAT"]);
  99. $this->_org = strtoupper($xmlArr["PLUGINSERVICE"]["LOG_CONFIG"]["ATTRIBUTES"]["ORG"]);
  100. $this->_daystolive = $xmlArr["PLUGINSERVICE"]["LOG_CONFIG"]["ATTRIBUTES"]["DAYSTOLIVE"]?strtoupper($xmlArr["PLUGINSERVICE"]["LOG_CONFIG"]["ATTRIBUTES"]["DAYSTOLIVE"]):'0';
  101. }
  102. /**
  103. * The primary log function that will take an incoming message and specified paramter and log in the
  104. * appropriate location and format
  105. *
  106. * @param int $level - Follow common BSD error levels
  107. * @param string $package - A way to group log messages
  108. * @param string $message - The actual message to log
  109. * @return boolean -TRUE on success otherwise FALSE
  110. */
  111. public function log($priority, $package, $message)
  112. {
  113. if (DEBUG == 0 && $priority == LOG_DEBUG)
  114. return true;
  115. //Adapt PHP LOG priority
  116. switch ($priority)
  117. {
  118. case LOG_EMERG : // system is unusable
  119. case LOG_ALERT : // action must be taken immediately
  120. case LOG_CRIT : // critical conditions
  121. $level = 2;
  122. break;
  123. case LOG_ERR : // error conditions
  124. $level = 3;
  125. break;
  126. case LOG_WARNING : // warning conditions
  127. $level = 4;
  128. break;
  129. case LOG_NOTICE : // normal, but significant, condition
  130. case LOG_INFO : // informational message
  131. case LOG_DEBUG : // debug-level message
  132. $level = 7;
  133. break;
  134. }
  135. //echo "log levels: ".LOG_EMERG.", ".LOG_ALERT.", ".LOG_CRIT.", ".LOG_ERR.", ".LOG_WARNING.", ".LOG_NOTICE.", ".LOG_INFO.", ".LOG_DEBUG."<br>";
  136. if ($this->_level == FALSE)
  137. return true;
  138. //Get the file path
  139. $this->_level = $level;
  140. $path = $this->_getPath($file_name);
  141. if(!is_file($path))
  142. {
  143. @touch($path);
  144. @chmod($path,0777);
  145. }
  146. if (!is_writable($path))
  147. return false;
  148. $this->prepFile($path);
  149. //Create the log writer object
  150. $writer = new Zend_Log_Writer_Stream($path);
  151. $writer->setFormatter($this->_formatter);
  152. //Instantiate logging object
  153. $logger = new Zend_Log($writer);
  154. //Filter log entries to those allowed by configuration
  155. $filter = new Zend_Log_Filter_Priority($this->_level);
  156. $logger->addFilter($filter);
  157. $logger->setEventItem('back_trace', 'n/a');
  158. $logger->setEventItem('package', $package);
  159. $logger->setEventItem('date', date("m/d/Y"));
  160. $logger->setEventItem('time', date("H:i:s"));
  161. //Write Log Entry
  162. $logger->log($message, $level);
  163. $this->closeFile($path);
  164. return TRUE;
  165. }
  166. /**
  167. * The primary log function that will take an incoming message and specified paramter and log in the
  168. * appropriate location and format
  169. *
  170. * @param int $level - Follow common BSD error levels
  171. * @param string $package - A way to group log messages
  172. * @param string $message - The actual message to log
  173. * @param string $fileName - An optional specific file name where the message should be logged
  174. * @param string $string - An optional list of the Application stack at the time of error
  175. * @return boolean -TRUE on success otherwise FALSE
  176. */
  177. public function logError($level = 7, $package, $message, $fileName = NULL, $backTrace = '')
  178. {
  179. //Adapt PHP Errors
  180. switch ($level)
  181. {
  182. case 8: // E_NOTICE
  183. case 1024: // E_USER_NOTICE
  184. $level = 4;
  185. break;
  186. case 2048: //E_STRICT
  187. case 8192: //E_DEPRICIATED
  188. case 16384: //E_DEPRICIATED
  189. $level = 5;
  190. break;
  191. case 2: //E_WARNING
  192. case 512: //E_USER_WARNING
  193. $level = 3;
  194. break;
  195. case 256:
  196. $level = 2;
  197. break;
  198. default:
  199. $level = 2;
  200. }
  201. if ($this->_level == FALSE)
  202. return true;
  203. //Get the file path
  204. $this->_level = $level;
  205. $path = $this->_getPath($fileName);
  206. if (!is_writable($path))
  207. return false;
  208. $this->prepFile($path);
  209. //Create the log writer object
  210. $writer = new Zend_Log_Writer_Stream($path);
  211. $writer->setFormatter($this->_formatter);
  212. //Instantiate logging object
  213. $logger = new Zend_Log($writer);
  214. //Filter log entries to those allowed by configuration
  215. $filter = new Zend_Log_Filter_Priority($this->_level);
  216. $logger->addFilter($filter);
  217. //Set additional information about the class and function that called the log
  218. // Add custom elements to default log package
  219. $logger->setEventItem('back_trace', $backTrace);
  220. $logger->setEventItem('package', $package);
  221. $logger->setEventItem('date', date("m/d/Y"));
  222. $logger->setEventItem('time', date("H:i:s"));
  223. //Write Log Entry
  224. $logger->log($message, $level);
  225. $this->closeFile($path);
  226. return TRUE;
  227. }
  228. /**
  229. * Set the formatter based on the type of format selected in app.inc
  230. *
  231. * @return void
  232. */
  233. public function setFormatter()
  234. {
  235. switch ($this->_format)
  236. {
  237. case 'HTML':
  238. //HTML Format
  239. $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);
  240. break;
  241. case 'XML':
  242. //XML Format
  243. require_once 'Zend/Log/Formatter/Xml.php';
  244. $this->_formatter = new Zend_Log_Formatter_Xml();
  245. break;
  246. case 'CSV':
  247. default:
  248. //CSV Format
  249. $this->_formatter = new Zend_Log_Formatter_Simple("'%date%','%time%','%priorityName%','%package%','%message%','%back_trace%'" . PHP_EOL);
  250. break;
  251. }
  252. }
  253. /**
  254. * Set the extension based on the type of format selected in app.inc
  255. *
  256. * @return void
  257. */
  258. public function setExtension()
  259. {
  260. switch ($this->_format)
  261. {
  262. case 'HTML':
  263. $this->_extension = '.html';
  264. break;
  265. case 'XML':
  266. $this->_extension = '.xml';
  267. break;
  268. case 'CSV':
  269. default:
  270. $this->_extension = '.log';
  271. break;
  272. }
  273. }
  274. /**
  275. * Either open a new file with the correct string or remove a previous closing string
  276. *
  277. * @return void
  278. */
  279. public function prepFile($path)
  280. {
  281. //Check for existing file and HTML format
  282. if (file_exists($path) and $this->_format == 'HTML')
  283. {
  284. $file = file($path);
  285. array_pop($file);
  286. file_put_contents($path, $file);
  287. } elseif ($this->_format == 'HTML')
  288. {
  289. $html = '<html><head></head><body><table border="1">' . PHP_EOL;
  290. $file = fopen($path, 'a');
  291. fwrite($file, $html);
  292. fclose($file);
  293. } elseif (file_exists($path) and $this->_format == 'XML')
  294. {
  295. $file = file($path);
  296. array_pop($file);
  297. file_put_contents($path, $file);
  298. } elseif ($this->_format == 'XML')
  299. {
  300. $xml = '<?xml version="1.0" standalone="no"?><log>' . PHP_EOL;
  301. $file = fopen($path, 'a');
  302. fwrite($file, $xml);
  303. fclose($file);
  304. }
  305. }
  306. /**
  307. * Close a file with the correct string depending on the configured format
  308. *
  309. * @return void
  310. */
  311. public function closeFile($path)
  312. {
  313. //Close up the file if needed
  314. switch ($this->_format)
  315. {
  316. case 'HTML':
  317. $html = "</table></body></html>";
  318. $file = fopen($path, 'a');
  319. fwrite($file, $html);
  320. fclose($file);
  321. break;
  322. case 'XML':
  323. $xml = "</log>";
  324. $file = fopen($path, 'a');
  325. fwrite($file, $xml);
  326. fclose($file);
  327. break;
  328. default:
  329. break;
  330. }
  331. }
  332. /**
  333. * Get path based on config options
  334. *
  335. * @global BizSystem $g_BizSystem
  336. * @param string $fileName
  337. * @return string log_path - The path where a log entry should be written
  338. */
  339. private function _getPath($fileName = null)
  340. {
  341. $level = $this->_level;
  342. if ($fileName)
  343. return LOG_PATH . '/' . $fileName . $this->_extension;
  344. switch ($this->_org)
  345. {
  346. case 'DATE':
  347. return LOG_PATH . '/' . date("Y_m_d") . $this->_extension;
  348. break;
  349. case 'LEVEL':
  350. $level = $this->_level2filename($level);
  351. return LOG_PATH . '/' . $level . $this->_extension;
  352. break;
  353. case 'LEVEL-DATE':
  354. $level = $this->_level2filename($level);
  355. //delete old log files
  356. if($this->_daystolive>0){
  357. if(is_array(glob(LOG_PATH . '/' . $level .'-*' . $this->_extension))){
  358. foreach (glob(LOG_PATH . '/' . $level .'-*' . $this->_extension) as $filename) {
  359. $mtime = filemtime($filename);
  360. if((time() - $mtime) >= $this->_daystolive*86400 ){
  361. @unlink($filename);
  362. }
  363. }
  364. }
  365. }
  366. return LOG_PATH . '/' . $level .'-'. date("Y_m_d") . $this->_extension;
  367. break;
  368. case 'PROFILE':
  369. $profile = BizSystem::getUserProfile('USERID');
  370. if (! $profile)
  371. $profile = 'Guest';
  372. return LOG_PATH . '/' . $profile . $this->_extension;
  373. break;
  374. default:
  375. ;
  376. break;
  377. }
  378. }
  379. private function _level2filename($level)
  380. {
  381. switch ($this->_level)
  382. {
  383. case 0:
  384. $level = 'EMERG';
  385. break;
  386. case 1:
  387. $level = 'ALERT';
  388. break;
  389. case 2:
  390. $level = 'CRIT';
  391. break;
  392. case 3:
  393. $level = 'ERR';
  394. break;
  395. case 4:
  396. $level = 'WARN';
  397. break;
  398. case 5:
  399. $level = 'NOTICE';
  400. break;
  401. case 6:
  402. $level = 'INFO';
  403. break;
  404. case 7:
  405. $level = 'DEBUG';
  406. break;
  407. default:
  408. ;
  409. break;
  410. }
  411. return $level;
  412. }
  413. }