Log.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 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. /**
  15. * 系统日志工具类
  16. * Class helper
  17. * @package app\common\library
  18. */
  19. class Log
  20. {
  21. // 日志内容
  22. private static array $data = [];
  23. /**
  24. * 写入日志
  25. * @param mixed $value
  26. * @param string $type
  27. */
  28. public static function record($value, string $type = 'info')
  29. {
  30. $content = is_string($value) ? $value : print_r($value, true);
  31. LogFacade::record($content, $type);
  32. }
  33. /**
  34. * 记录错误日志
  35. * @param $value
  36. * @return void
  37. */
  38. public static function error($value)
  39. {
  40. static::record($value, 'error');
  41. }
  42. /**
  43. * 写入日志 (使用追加的方式, 索引值是name)
  44. * @param string $name 日志记录名
  45. * @param array $data 记录内容
  46. */
  47. public static function append(string $name, array $data)
  48. {
  49. $merge = array_merge(compact('name'), $data);
  50. if (isset(static::$data[$name])) {
  51. $merge = array_merge(static::$data[$name], $merge);
  52. }
  53. static::$data[$name] = $merge;
  54. }
  55. /**
  56. * 在应用结束时将追加的日志数据写入到文件
  57. */
  58. public static function end()
  59. {
  60. foreach (static::$data as $name => $item) {
  61. static::record(array_merge(['name' => $name], $item));
  62. }
  63. }
  64. }