PageRenderTime 52ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/system/libraries/Log.php

https://bitbucket.org/muzamilqadir/buildium
PHP | 114 lines | 54 code | 20 blank | 40 comment | 12 complexity | f2c66f78fdabb0208d2ece1fe5649050 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 5.1.6 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author ExpressionEngine Dev Team
  9. * @copyright Copyright (c) 2008 - 2011, 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. protected $_log_path;
  27. protected $_threshold = 1;
  28. protected $_date_fmt = 'Y-m-d H:i:s';
  29. protected $_enabled = TRUE;
  30. protected $_levels = array('ERROR' => '1', 'DEBUG' => '2', 'INFO' => '3', 'ALL' => '4');
  31. /**
  32. * Constructor
  33. */
  34. public function __construct()
  35. {
  36. $config =& get_config();
  37. $this->_log_path = ($config['log_path'] != '') ? $config['log_path'] : APPPATH.'logs/';
  38. if ( ! is_dir($this->_log_path) OR ! is_really_writable($this->_log_path))
  39. {
  40. $this->_enabled = FALSE;
  41. }
  42. if (is_numeric($config['log_threshold']))
  43. {
  44. $this->_threshold = $config['log_threshold'];
  45. }
  46. if ($config['log_date_format'] != '')
  47. {
  48. $this->_date_fmt = $config['log_date_format'];
  49. }
  50. }
  51. // --------------------------------------------------------------------
  52. /**
  53. * Write Log File
  54. *
  55. * Generally this function will be called using the global log_message() function
  56. *
  57. * @param string the error level
  58. * @param string the error message
  59. * @param bool whether the error is a native PHP error
  60. * @return bool
  61. */
  62. public function write_log($level = 'error', $msg, $php_error = FALSE)
  63. {
  64. if ($this->_enabled === FALSE)
  65. {
  66. return FALSE;
  67. }
  68. $level = strtoupper($level);
  69. if ( ! isset($this->_levels[$level]) OR ($this->_levels[$level] > $this->_threshold))
  70. {
  71. return FALSE;
  72. }
  73. $filepath = $this->_log_path.'log-'.date('Y-m-d').'.php';
  74. $message = '';
  75. if ( ! file_exists($filepath))
  76. {
  77. $message .= "<"."?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?".">\n\n";
  78. }
  79. if ( ! $fp = @fopen($filepath, FOPEN_WRITE_CREATE))
  80. {
  81. return FALSE;
  82. }
  83. $message .= $level.' '.(($level == 'INFO') ? ' -' : '-').' '.date($this->_date_fmt). ' --> '.$msg."\n";
  84. flock($fp, LOCK_EX);
  85. fwrite($fp, $message);
  86. flock($fp, LOCK_UN);
  87. fclose($fp);
  88. @chmod($filepath, FILE_WRITE_MODE);
  89. return TRUE;
  90. }
  91. }
  92. // END Log Class
  93. /* End of file Log.php */
  94. /* Location: ./system/libraries/Log.php */