Nessuna descrizione

Log.php 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 log($name, $content, $extra, $level=Logger::ERROR)
  9. {
  10. $logger = new Logger($name);
  11. # 日志文件位置及文件名
  12. $logFilePath = storage_path('qyWechat/' . 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->addInfo($content, $extra);
  19. break;
  20. case Logger::ERROR:
  21. $logger->addError($content, $extra);
  22. break;
  23. default:
  24. $logger->addInfo($content, $extra);
  25. break;
  26. }
  27. } catch (\Exception $e) {
  28. $exceptionLogPath = storage_path('logs/exception/monolog.log');
  29. file_put_contents($exceptionLogPath, date('Y-m-d H:i:s') . " " . $e->getMessage() . "[". json_encode($extra, JSON_UNESCAPED_UNICODE) ."【".$name."】]". "\r\n", FILE_APPEND);
  30. }
  31. }
  32. /**
  33. * 信息日志
  34. * @param $title
  35. * @param $content
  36. * @param string $file
  37. */
  38. public static function logInfo($content, $extra=array(), $prefix='info')
  39. {
  40. self::log($prefix, $content, $extra, Logger::INFO);
  41. }
  42. /**
  43. * 错误日志
  44. * @param $title
  45. * @param $content
  46. * @param string $file
  47. */
  48. public static function logError($content, $extra=array(), $prefix='error')
  49. {
  50. self::log($prefix, $content, $extra, Logger::ERROR);
  51. }
  52. }