新版订单消耗系统

Log.php 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace App;
  3. use Monolog\Logger;
  4. use Monolog\Handler\StreamHandler;
  5. use Monolog\Processor\WebProcessor;
  6. class Log
  7. {
  8. public static function errorLog($params,$returnValue,$interfaceName,$flag,$headers=[],$responseTime=NULL){
  9. $data['time']=date("Y-m-d H:i:s",time());
  10. $data["interfaceName"]=$interfaceName;
  11. $data['responseTime'] = $responseTime;
  12. $data["params"]=$params;
  13. $data["headers"]=$headers;
  14. $data["returnValue"]=$returnValue;
  15. if($flag){
  16. $tmpDir="success/";
  17. }else{
  18. $tmpDir="error/";
  19. }
  20. $name=date("Y-m-d",time()).".txt";
  21. error_log(json_encode($data, JSON_UNESCAPED_UNICODE)."\n",3,"/log/order_consumption_system_new/".$tmpDir.$name);
  22. }
  23. public static function scriptLog($params,$err_msg,$flag){
  24. $data['time']=date("Y-m-d H:i:s",time());
  25. $data["params"]=$params;
  26. $data["err_msg"]=$err_msg;
  27. if($flag){
  28. $tmpDir="script_success/";
  29. }else{
  30. $tmpDir="script_error/";
  31. }
  32. $name=date("Y-m-d",time()).".txt";
  33. error_log(json_encode($data, JSON_UNESCAPED_UNICODE)."\n",3,"/log/order_consumption_system_new/".$tmpDir.$name);
  34. }
  35. public static function importLog($params, $type) {
  36. $data['time'] = date("Y-m-d H:i:s",time());
  37. $data['params'] = $params;
  38. $data['type'] = $type;
  39. $tmpDir="import/";
  40. $name=date("Y-m-d",time()).".txt";
  41. error_log(json_encode($data, JSON_UNESCAPED_UNICODE)."\n",3,"/log/order_consumption_system_new/".$tmpDir.$name);
  42. }
  43. /**
  44. * 信息日志
  45. * @param $title
  46. * @param $content
  47. * @param string $file
  48. */
  49. public static function logInfo($content, $extra=array(), $prefix='info')
  50. {
  51. self::log($prefix, $content, $extra, Logger::INFO);
  52. }
  53. /**
  54. * 错误日志
  55. * @param $title
  56. * @param $content
  57. * @param string $file
  58. */
  59. public static function logError($content, $extra=array(), $prefix='error')
  60. {
  61. self::log($prefix, $content, $extra, Logger::ERROR);
  62. }
  63. public static function log($name, $content, $extra, $level=Logger::ERROR)
  64. {
  65. $logger = new Logger($name);
  66. # 日志文件位置及文件名
  67. $logFilePath = storage_path('xhb/' . date('m-d') . '/' .$name .'.log');
  68. try {
  69. $logger->pushHandler(new StreamHandler($logFilePath), $level);
  70. $logger->pushProcessor(new WebProcessor());
  71. switch($level) {
  72. case Logger::INFO:
  73. $logger->addInfo($content, $extra);
  74. break;
  75. case Logger::ERROR:
  76. $logger->addError($content, $extra);
  77. break;
  78. default:
  79. $logger->addInfo($content, $extra);
  80. break;
  81. }
  82. } catch (\Exception $e) {
  83. $exceptionLogPath = storage_path('logs/exception/monolog.log');
  84. file_put_contents($exceptionLogPath, date('Y-m-d H:i:s') . " " . $e->getMessage() . "[". json_encode($extra, JSON_UNESCAPED_UNICODE) ."【".$name."】]". "\r\n", FILE_APPEND);
  85. }
  86. }
  87. }