Log.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\common\library;
  13. use think\facade\Log as LogFacade;
  14. class Log
  15. {
  16. // 日志内容
  17. private static $data = [];
  18. /**
  19. * 写入日志
  20. * @param mixed $value
  21. * @param string $type
  22. */
  23. public static function record($value, string $type = 'info')
  24. {
  25. $content = is_string($value) ? $value : print_r($value, true);
  26. LogFacade::record($content, $type);
  27. }
  28. /**
  29. * 写入日志 (使用追加的方式, 索引值是name)
  30. * @param string $name 日志记录名
  31. * @param array $data 记录内容
  32. */
  33. public static function append(string $name, array $data)
  34. {
  35. $merge = array_merge(compact('name'), $data);
  36. if (isset(static::$data[$name])) {
  37. $merge = array_merge(static::$data[$name], $merge);
  38. }
  39. static::$data[$name] = $merge;
  40. }
  41. /**
  42. * 在应用结束时将追加的日志数据写入到文件
  43. */
  44. public static function end()
  45. {
  46. foreach (static::$data as $name => $item) {
  47. static::record(array_merge(['name' => $name], $item));
  48. }
  49. }
  50. }