PageRenderTime 52ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/system/libraries/Log.php

https://bitbucket.org/jorgenio/codeigniter
PHP | 180 lines | 66 code | 27 blank | 87 comment | 12 complexity | 098d11af7ad83f12aa0fa9d4af6e7763 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.2.4 or newer
  6. *
  7. * NOTICE OF LICENSE
  8. *
  9. * Licensed under the Open Software License version 3.0
  10. *
  11. * This source file is subject to the Open Software License (OSL 3.0) that is
  12. * bundled with this package in the files license.txt / license.rst. It is
  13. * also available through the world wide web at this URL:
  14. * http://opensource.org/licenses/OSL-3.0
  15. * If you did not receive a copy of the license and are unable to obtain it
  16. * through the world wide web, please send an email to
  17. * licensing@ellislab.com so we can send you a copy immediately.
  18. *
  19. * @package CodeIgniter
  20. * @author EllisLab Dev Team
  21. * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
  22. * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
  23. * @link http://codeigniter.com
  24. * @since Version 1.0
  25. * @filesource
  26. */
  27. /**
  28. * Logging Class
  29. *
  30. * @package CodeIgniter
  31. * @subpackage Libraries
  32. * @category Logging
  33. * @author EllisLab Dev Team
  34. * @link http://codeigniter.com/user_guide/general/errors.html
  35. */
  36. class CI_Log {
  37. /**
  38. * Path to save log files
  39. *
  40. * @var string
  41. */
  42. protected $_log_path;
  43. /**
  44. * Level of logging
  45. *
  46. * @var int
  47. */
  48. protected $_threshold = 1;
  49. /**
  50. * Highest level of logging
  51. *
  52. * @var int
  53. */
  54. protected $_threshold_max = 0;
  55. /**
  56. * Array of threshold levels to log
  57. *
  58. * @var array
  59. */
  60. protected $_threshold_array = array();
  61. /**
  62. * Format of timestamp for log files
  63. *
  64. * @var string
  65. */
  66. protected $_date_fmt = 'Y-m-d H:i:s';
  67. /**
  68. * Whether or not the logger can write to the log files
  69. *
  70. * @var bool
  71. */
  72. protected $_enabled = TRUE;
  73. /**
  74. * Predefined logging levels
  75. *
  76. * @var array
  77. */
  78. protected $_levels = array('ERROR' => 1, 'DEBUG' => 2, 'INFO' => 3, 'ALL' => 4);
  79. /**
  80. * Initialize Logging class
  81. *
  82. * @return void
  83. */
  84. public function __construct()
  85. {
  86. $config =& get_config();
  87. $this->_log_path = ($config['log_path'] !== '') ? $config['log_path'] : APPPATH.'logs/';
  88. if ( ! is_dir($this->_log_path) OR ! is_really_writable($this->_log_path))
  89. {
  90. $this->_enabled = FALSE;
  91. }
  92. if (is_numeric($config['log_threshold']))
  93. {
  94. $this->_threshold = (int) $config['log_threshold'];
  95. }
  96. elseif (is_array($config['log_threshold']))
  97. {
  98. $this->_threshold = $this->_threshold_max;
  99. $this->_threshold_array = array_flip($config['log_threshold']);
  100. }
  101. if ($config['log_date_format'] !== '')
  102. {
  103. $this->_date_fmt = $config['log_date_format'];
  104. }
  105. }
  106. // --------------------------------------------------------------------
  107. /**
  108. * Write Log File
  109. *
  110. * Generally this function will be called using the global log_message() function
  111. *
  112. * @param string the error level
  113. * @param string the error message
  114. * @param bool whether the error is a native PHP error
  115. * @return bool
  116. */
  117. public function write_log($level = 'error', $msg, $php_error = FALSE)
  118. {
  119. if ($this->_enabled === FALSE)
  120. {
  121. return FALSE;
  122. }
  123. $level = strtoupper($level);
  124. if (( ! isset($this->_levels[$level]) OR ($this->_levels[$level] > $this->_threshold))
  125. && ! isset($this->_threshold_array[$this->_levels[$level]]))
  126. {
  127. return FALSE;
  128. }
  129. $filepath = $this->_log_path.'log-'.date('Y-m-d').'.php';
  130. $message = '';
  131. if ( ! file_exists($filepath))
  132. {
  133. $newfile = TRUE;
  134. $message .= '<'."?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?".">\n\n";
  135. }
  136. if ( ! $fp = @fopen($filepath, FOPEN_WRITE_CREATE))
  137. {
  138. return FALSE;
  139. }
  140. $message .= $level.' '.($level === 'INFO' ? ' -' : '-').' '.date($this->_date_fmt).' --> '.$msg."\n";
  141. flock($fp, LOCK_EX);
  142. fwrite($fp, $message);
  143. flock($fp, LOCK_UN);
  144. fclose($fp);
  145. if (isset($newfile) && $newfile === TRUE)
  146. {
  147. @chmod($filepath, FILE_WRITE_MODE);
  148. }
  149. return TRUE;
  150. }
  151. }
  152. /* End of file Log.php */
  153. /* Location: ./system/libraries/Log.php */