Logger.Class.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace JinDouYun\Controller\Common;
  3. error_reporting(0);
  4. /**
  5. * demo Logger::php_error_log(E_USER_ERROR,'查询企业列表失败',__CLASS__,__LINE__,$objDEnterprise->error());
  6. * 错误记录
  7. * Class Logger
  8. * @package JinDouYun\Controller\Common
  9. */
  10. class Logger
  11. {
  12. /**
  13. * @var string 外部php生成日志路基
  14. */
  15. private static $customFileLogDir = '/www/wwwroot/logs/';
  16. /**
  17. * 写入php错误日志
  18. *
  19. * @param string $errno 错误编号
  20. * @param string $errmsg 错误信息
  21. * @param string $filename 错误文件 __CLASS__
  22. * @param string $linenum 错误行数 __LINE__
  23. * @param mixed $vars 错误参数
  24. * @param boolean|string $custom 使用非容器内php时这个参数传true
  25. *
  26. */
  27. public static function logs($errno, $errmsg, $filename, $linenum, $vars, $custom = false)
  28. {
  29. if (!defined('E_STRICT')) define('E_STRICT', 2048);
  30. $errortype = array(
  31. E_ERROR => 'Error',
  32. E_WARNING => 'Warning',
  33. E_PARSE => 'Parsing Error',
  34. E_NOTICE => 'Notice',
  35. E_CORE_ERROR => 'Core Error',
  36. E_CORE_WARNING => 'Core Warning',
  37. E_COMPILE_ERROR => 'Compile Error',
  38. E_COMPILE_WARNING => 'Compile Warning',
  39. E_USER_ERROR => 'User Error',
  40. E_USER_WARNING => 'User Warning',
  41. E_USER_NOTICE => 'User Notice',
  42. E_STRICT => 'Runtime Notice'
  43. );
  44. $user_errors = array(E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE);
  45. $filename = str_replace("\\", '/', $filename);
  46. $dt = date('Y-m-d H:i:s');
  47. $err = "#####" . $dt . "\n";
  48. $err .= "```" . "\n";
  49. $err .= "datetime :" . $dt . "\n";
  50. $err .= "errornum :" . $errno . "\n";
  51. $err .= "errormsg :" . $errortype[$errno] . "\n";
  52. $err .= "errortype :" . $errmsg . "\n";
  53. $err .= "scriptname :" . $filename . "\n";
  54. if (in_array($errno, $user_errors)) {
  55. //$err .= "scriptname :" . wddx_serialize_value($vars, "Variables") . "\n";
  56. $err .= "scriptname :" . serialize($vars) . "\n";
  57. }
  58. $err .= "scriptlinenum:" . $linenum . "\n";
  59. $err .= "```\n";
  60. if ($custom === false){
  61. $logDir = LOGS_DIR . 'SysError' . DS;
  62. }else{
  63. $logDir = self::$customFileLogDir . 'SysError' . DS;
  64. }
  65. $logFile = $logDir . date('m-d') . '.md';
  66. if (!is_dir($logDir)) {
  67. @mkdir($logDir);
  68. }
  69. @error_log($err, 3, $logFile);
  70. @chmod($logFile, 0777);
  71. }
  72. }