PageRenderTime 64ms CodeModel.GetById 38ms RepoModel.GetById 1ms app.codeStats 0ms

/system/libraries/Log.php

https://bitbucket.org/jeveloper/getbriefed_original
PHP | 117 lines | 54 code | 20 blank | 43 comment | 12 complexity | 4e32b1f56754e7e095c265bfabc1a0b2 MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 4.3.2 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author ExpressionEngine Dev Team
  9. * @copyright Copyright (c) 2008, EllisLab, Inc.
  10. * @license http://codeigniter.com/user_guide/license.html
  11. * @link http://codeigniter.com
  12. * @since Version 1.0
  13. * @filesource
  14. */
  15. // ------------------------------------------------------------------------
  16. /**
  17. * Logging Class
  18. *
  19. * @package CodeIgniter
  20. * @subpackage Libraries
  21. * @category Logging
  22. * @author ExpressionEngine Dev Team
  23. * @link http://codeigniter.com/user_guide/general/errors.html
  24. */
  25. class CI_Log {
  26. var $log_path;
  27. var $_threshold = 1;
  28. var $_date_fmt = 'Y-m-d H:i:s';
  29. var $_enabled = TRUE;
  30. var $_levels = array('ERROR' => '1', 'DEBUG' => '2', 'INFO' => '3', 'ALL' => '4');
  31. /**
  32. * Constructor
  33. *
  34. * @access public
  35. */
  36. function CI_Log()
  37. {
  38. $config =& get_config();
  39. $this->log_path = ($config['log_path'] != '') ? $config['log_path'] : BASEPATH.'logs/';
  40. if ( ! is_dir($this->log_path) OR ! is_really_writable($this->log_path))
  41. {
  42. $this->_enabled = FALSE;
  43. }
  44. if (is_numeric($config['log_threshold']))
  45. {
  46. $this->_threshold = $config['log_threshold'];
  47. }
  48. if ($config['log_date_format'] != '')
  49. {
  50. $this->_date_fmt = $config['log_date_format'];
  51. }
  52. }
  53. // --------------------------------------------------------------------
  54. /**
  55. * Write Log File
  56. *
  57. * Generally this function will be called using the global log_message() function
  58. *
  59. * @access public
  60. * @param string the error level
  61. * @param string the error message
  62. * @param bool whether the error is a native PHP error
  63. * @return bool
  64. */
  65. function write_log($level = 'error', $msg, $php_error = FALSE)
  66. {
  67. if ($this->_enabled === FALSE)
  68. {
  69. return FALSE;
  70. }
  71. $level = strtoupper($level);
  72. if ( ! isset($this->_levels[$level]) OR ($this->_levels[$level] > $this->_threshold))
  73. {
  74. return FALSE;
  75. }
  76. $filepath = $this->log_path.'log-'.date('Y-m-d').EXT;
  77. $message = '';
  78. if ( ! file_exists($filepath))
  79. {
  80. $message .= "<"."?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?".">\n\n";
  81. }
  82. if ( ! $fp = @fopen($filepath, FOPEN_WRITE_CREATE))
  83. {
  84. return FALSE;
  85. }
  86. $message .= $level.' '.(($level == 'INFO') ? ' -' : '-').' '.date($this->_date_fmt). ' --> '.$msg."\n";
  87. flock($fp, LOCK_EX);
  88. fwrite($fp, $message);
  89. flock($fp, LOCK_UN);
  90. fclose($fp);
  91. @chmod($filepath, FILE_WRITE_MODE);
  92. return TRUE;
  93. }
  94. }
  95. // END Log Class
  96. /* End of file Log.php */
  97. /* Location: ./system/libraries/Log.php */