Log.class.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. // +--------------------------------------------------------------------------
  3. // | Senthot [ DEVELOPED BY ME ]
  4. // +--------------------------------------------------------------------------
  5. // | Copyright (c) 2005-2013 http://www.senthot.com All rights reserved.
  6. // | License ( http://www.apache.org/licenses/LICENSE-2.0 )
  7. // | Author: ms134n ( [email protected] )
  8. // +--------------------------------------------------------------------------
  9. /**
  10. * Log processing class
  11. * @category Sen
  12. * @package Sen
  13. * @subpackage Core
  14. * @author ms134n <[email protected]>
  15. */
  16. class Log {
  17. // Log Level From top to bottom, from low to high
  18. const EMERG = 'EMERG'; // Fatal Error: Crash the system can not be used
  19. const ALERT = 'ALERT'; // Cautionary error: Errors must be immediately modified
  20. const CRIT = 'CRIT'; // Threshold Error: Error exceeds the threshold , for example, 24 hours a day and 25 hours of this input
  21. const ERR = 'ERR'; // General Error: Generic error
  22. const WARN = 'WARN'; // Warning Error: Need to issue a warning error
  23. const NOTICE = 'NOTIC'; // Notification: Program can run but not perfect error
  24. const INFO = 'INFO'; // Information: Program output
  25. const DEBUG = 'DEBUG'; // Debugging: Debugging information
  26. const SQL = 'SQL'; // SQL: SQL Statement Note Only valid in debug mode is on
  27. // Log mode
  28. const SYSTEM = 0;
  29. const MAIL = 1;
  30. const FILE = 3;
  31. const SAPI = 4;
  32. // Log information
  33. static $log = array();
  34. // Date Format
  35. static $format = '[ c ]';
  36. /**
  37. * Logging And will not set the level of filtering
  38. * @static
  39. * @access public
  40. * @param string $message Log information
  41. * @param string $level Log Level
  42. * @param boolean $record Whether to force record
  43. * @return void
  44. */
  45. static function record($message,$level=self::ERR,$record=false) {
  46. if($record || false !== strpos(C('LOG_LEVEL'),$level)) {
  47. self::$log[] = "{$level}: {$message}\r\n";
  48. }
  49. }
  50. /**
  51. * Log Save
  52. * @static
  53. * @access public
  54. * @param integer $type Log mode
  55. * @param string $destination Written to the target
  56. * @param string $extra Additional parameters
  57. * @return void
  58. */
  59. static function save($type='',$destination='',$extra='') {
  60. if(empty(self::$log)) return ;
  61. $type = $type?$type:C('LOG_TYPE');
  62. if(self::FILE == $type) { // Papers logging information
  63. if(empty($destination))
  64. $destination = LOG_PATH.date('y_m_d').'.log';
  65. //Test log file size exceeds the configured size of the backup log files to regenerate
  66. if(is_file($destination) && floor(C('LOG_FILE_SIZE')) <= filesize($destination) )
  67. rename($destination,dirname($destination).'/'.time().'-'.basename($destination));
  68. }else{
  69. $destination = $destination?$destination:C('LOG_DEST');
  70. $extra = $extra?$extra:C('LOG_EXTRA');
  71. }
  72. $now = date(self::$format);
  73. error_log($now.' '.get_client_ip().' '.$_SERVER['REQUEST_URI']."\r\n".implode('',self::$log)."\r\n", $type,$destination ,$extra);
  74. // After saving the log cache emptied
  75. self::$log = array();
  76. //clearstatcache();
  77. }
  78. /**
  79. * Log directly into
  80. * @static
  81. * @access public
  82. * @param string $message Log information
  83. * @param string $level Log Level
  84. * @param integer $type Log mode
  85. * @param string $destination Written to the target
  86. * @param string $extra Additional parameters
  87. * @return void
  88. */
  89. static function write($message,$level=self::ERR,$type='',$destination='',$extra='') {
  90. $now = date(self::$format);
  91. $type = $type?$type:C('LOG_TYPE');
  92. if(self::FILE == $type) { // Logging papers
  93. if(empty($destination))
  94. $destination = LOG_PATH.date('y_m_d').'.log';
  95. //Test log file size exceeds the configured size of the backup log files to regenerate
  96. if(is_file($destination) && floor(C('LOG_FILE_SIZE')) <= filesize($destination) )
  97. rename($destination,dirname($destination).'/'.time().'-'.basename($destination));
  98. }else{
  99. $destination = $destination?$destination:C('LOG_DEST');
  100. $extra = $extra?$extra:C('LOG_EXTRA');
  101. }
  102. error_log("{$now} {$level}: {$message}\r\n", $type,$destination,$extra );
  103. //clearstatcache();
  104. }
  105. }