优惠券小程序

Log.php 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace App;
  3. use Monolog\Handler\StreamHandler;
  4. use Monolog\Logger;
  5. use Monolog\Processor\WebProcessor;
  6. class Log
  7. {
  8. private static function _save($name, $content, $extra, $level = Logger::INFO)
  9. {
  10. $logger = new Logger($name);
  11. # 日志文件位置及文件名
  12. $logFilePath = storage_path('yhq/' . date('m-d') . '/' .$name .'.log');
  13. try {
  14. $logger->pushHandler(new StreamHandler($logFilePath), $level);
  15. $logger->pushProcessor(new WebProcessor());
  16. switch($level) {
  17. case Logger::INFO:
  18. $logger->info($content, $extra);
  19. break;
  20. case Logger::ERROR:
  21. $logger->error($content, $extra);
  22. break;
  23. case Logger::DEBUG:
  24. $logger->debug($content, $extra);
  25. break;
  26. default:
  27. $logger->warning('日志类型传递有误,暂时存在三种类型:', ['info', 'error', 'debug']);
  28. }
  29. } catch (\Exception $e) {
  30. $exceptionLogPath = storage_path('logs/exception.log');
  31. file_put_contents($exceptionLogPath, date('Y-m-d H:i:s') . " " . $e->getMessage() . "[". json_encode($extra, JSON_UNESCAPED_UNICODE) ."【".$name."】]". "\r\n", FILE_APPEND);
  32. }
  33. }
  34. /**
  35. * 信息日志
  36. * @param $title
  37. * @param $content
  38. * @param string $file
  39. */
  40. public static function info($content, $extra = [], $prefix = 'info')
  41. {
  42. self::_save($prefix, $content, $extra, Logger::INFO);
  43. }
  44. /**
  45. * 错误日志
  46. * @param $title
  47. * @param $content
  48. * @param string $file
  49. */
  50. public static function error($content, $extra = [], $prefix = 'error')
  51. {
  52. self::_save($prefix, $content, $extra, Logger::ERROR);
  53. }
  54. /**
  55. * 调试日志
  56. * @param $content
  57. * @param array $extra
  58. * @param string $prefix
  59. */
  60. public static function debug($content, $extra = [], $prefix = 'debug')
  61. {
  62. self::_save($prefix, $content, $extra, Logger::DEBUG);
  63. }
  64. }