PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/system/codeigniter/libraries/Log.php

https://bitbucket.org/viktorfabry/banditos
PHP | 139 lines | 66 code | 21 blank | 52 comment | 13 complexity | d1f6b683d976b777c72a9a10f0af2a36 MD5 | raw file
Possible License(s): CC-BY-3.0, BSD-3-Clause, MIT
  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. /**
  29. * Logging Class
  30. *
  31. * @package CodeIgniter
  32. * @subpackage Libraries
  33. * @category Logging
  34. * @author EllisLab Dev Team
  35. * @link http://codeigniter.com/user_guide/general/errors.html
  36. */
  37. class CI_Log {
  38. protected $_log_path;
  39. protected $_threshold = 1;
  40. protected $_threshold_max = 0;
  41. protected $_threshold_array = array();
  42. protected $_date_fmt = 'Y-m-d H:i:s';
  43. protected $_enabled = TRUE;
  44. protected $_levels = array('ERROR' => 1, 'DEBUG' => 2, 'INFO' => 3, 'ALL' => 4);
  45. /**
  46. * Constructor
  47. */
  48. public function __construct()
  49. {
  50. $config =& get_config();
  51. $this->_log_path = ($config['log_path'] != '') ? $config['log_path'] : APPPATH.'logs/';
  52. if ( ! is_dir($this->_log_path) OR ! is_really_writable($this->_log_path))
  53. {
  54. $this->_enabled = FALSE;
  55. }
  56. if (is_numeric($config['log_threshold']))
  57. {
  58. $this->_threshold = (int) $config['log_threshold'];
  59. }
  60. elseif (is_array($config['log_threshold']))
  61. {
  62. $this->_threshold = $this->_threshold_max;
  63. $this->_threshold_array = array_flip($config['log_threshold']);
  64. }
  65. if ($config['log_date_format'] != '')
  66. {
  67. $this->_date_fmt = $config['log_date_format'];
  68. }
  69. }
  70. // --------------------------------------------------------------------
  71. /**
  72. * Write Log File
  73. *
  74. * Generally this function will be called using the global log_message() function
  75. *
  76. * @param string the error level
  77. * @param string the error message
  78. * @param bool whether the error is a native PHP error
  79. * @return bool
  80. */
  81. public function write_log($level = 'error', $msg, $php_error = FALSE)
  82. {
  83. if ($this->_enabled === FALSE)
  84. {
  85. return FALSE;
  86. }
  87. $level = strtoupper($level);
  88. if (( ! isset($this->_levels[$level]) OR ($this->_levels[$level] > $this->_threshold))
  89. AND ! isset($this->_threshold_array[$this->_levels[$level]]))
  90. {
  91. return FALSE;
  92. }
  93. $filepath = $this->_log_path.'log-'.date('Y-m-d').'.php';
  94. $message = '';
  95. if ( ! file_exists($filepath))
  96. {
  97. $newfile = TRUE;
  98. $message .= "<"."?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?".">\n\n";
  99. }
  100. if ( ! $fp = @fopen($filepath, FOPEN_WRITE_CREATE))
  101. {
  102. return FALSE;
  103. }
  104. $message .= $level.' '.(($level == 'INFO') ? ' -' : '-').' '.date($this->_date_fmt). ' --> '.$msg."\n";
  105. flock($fp, LOCK_EX);
  106. fwrite($fp, $message);
  107. flock($fp, LOCK_UN);
  108. fclose($fp);
  109. if (isset($newfile) AND $newfile === TRUE)
  110. {
  111. @chmod($filepath, FILE_WRITE_MODE);
  112. }
  113. return TRUE;
  114. }
  115. }
  116. // END Log Class
  117. /* End of file Log.php */
  118. /* Location: ./system/libraries/Log.php */