Log.php 2.7 KB

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