You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

77 line
1.6 KiB

  1. <?php
  2. namespace Kuxin;
  3. /**
  4. * Class Log
  5. *
  6. * @package Kuxin
  7. * @author Pakey <pakey@qq.com>
  8. */
  9. class Log
  10. {
  11. /**
  12. * 日志内容
  13. *
  14. * @var array
  15. */
  16. protected static $log = [];
  17. /**
  18. * 获取日志信息
  19. *
  20. * @param string $type 信息类型
  21. * @return array
  22. */
  23. public static function getLog($type = ''): array
  24. {
  25. return $type ? (self::$log[$type] ?? []) : self::$log;
  26. }
  27. /**
  28. * 记录日志 默认为pt
  29. *
  30. * @param mixed $msg 调试信息
  31. * @param string $type 信息类型
  32. * @return void
  33. */
  34. public static function record($msg, $type = 'kx'):void
  35. {
  36. self::$log[$type][] = "[" . date('Y-m-d H:i:s') . "] " . $msg;;
  37. }
  38. /**
  39. * 清空日志信息
  40. *
  41. * @return void
  42. */
  43. public static function clear():void
  44. {
  45. self::$log = [];
  46. }
  47. /**
  48. * 手动写入指定日志到文件
  49. *
  50. * @param string $content
  51. * @param string $type
  52. */
  53. public static function write(string $content, string $type = 'kx', bool $withTime = true):void
  54. {
  55. DI::Storage('log')->append($type . '_' . date('Ymd') . '.txt', ($withTime ? ("[" . date('Y-m-d H:i:s') . "] ") : "") . $content . PHP_EOL);
  56. }
  57. /**
  58. * 自动写入指定类型日志
  59. */
  60. public static function build():void
  61. {
  62. $logBuild = Config::get('log.buildtype', ['pt', 'debug', 'collect', 'collecterror', 'cron']);
  63. foreach ($logBuild as $type) {
  64. if (isset(self::$log[$type])) {
  65. self::write(implode(PHP_EOL, self::$log[$type]), $type, false);
  66. }
  67. }
  68. }
  69. }