thinkphp6⾃定义⽇志驱动,增加显⽰全部请求信息
关键是两个,1是修改配置⽂件,指定⾃定义的驱动。2是⾃定义驱动的save⽅法直接上代码
config/log.
app\drive\log\Tp6Log.php
<?php // +----------------------------------------------------------------------// | ⽇志设置// +----------------------------------------------------------------------return [ // 默认⽇志记录通道 'default' => env ('log.channel', 'file'), // ⽇志记录级别 'level' => [], // ⽇志类型记录的通道 ['error'=>'email',...] 'type_channel' => [], // 关闭全局⽇志写⼊ 'close' => false , // 全局⽇志处理 ⽀持闭包 'processor' => null , //ThinkPHP 对系统的⽇志按照级别来分类记录,按照PSR-3⽇志规范,⽇志的级别从低到⾼依次为: debug, info, notice, warning, error, critical, alert, emerg
// ⽇志通道列表 'channels' => [ 'file' => [ // ⽇志记录⽅式 //'type' => 'File', 'type' => '\app\driver\log\Tp6Log', // ⽇志保存⽬录 'path' => '', // 单
⽂件⽇志写⼊ 'single' => false , // 独⽴⽇志级别 'apart_level' => ['error', 'sql','user','fun','pay','refund','order','cancelBadOrder','ar','job'], // 最⼤⽇志⽂件数量 'max_files' => 0, // 使⽤JSON 格式记录 'json' => false , // ⽂件⼤⼩ 'file_size' => 1024*1024*2, // ⽇志处理 'processor' => null , // 关闭通道⽇志写⼊ 'close' => false , // ⽇志输出格式化 'format' => '[%s][%s] %s', // 是否实时写⼊ 'realtime_write' => true , ], // 其它⽇志通道配置 ],];1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36phpjson格式化输出
37
38
39
40
41
42
43
44
45
46
47
48
49<?php declare (strict_types =1);
1
2
declare (strict_types =1);namespace app \driver \log ;use DateTime ;use DateTimeZone ;use Exc
eption ;use think \App ;use think \contract \LogHandlerInterface ;use think \facade \Request ;/** * 本地化调试输出到⽂件 */class Tp6Log implements LogHandlerInterface { /** * 配置参数 * @var array */ protected $config = [ 'time_format' => 'c', 'single' => false , 'file_size' => 2097152, 'path' => '', 'apart_level' => [], 'max_files' => 0, 'json' => false , 'json_options' => JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES , 'format' => '[%s][%s] %s', ]; protected $app ; // 实例化并传⼊参数 public function __construct (App $app , $config = []) { $this ->app = $app ; if (is_array ($config )) { $this ->config = array_merge ($this ->config , $config ); } if (empty ($this ->config ['format'])) { $this ->config ['format'] = '[%s][%s] %s'; } if (empty ($this ->config ['path'])) { $this ->config ['path'] = $app ->getRuntimePath () . 'log'; } if (substr ($this ->config ['path'], -1) != DIRECTORY_SEPARATOR ) { $this ->config ['path'] .= DIRECTORY_SEPARATOR ; } } /** * ⽇志写⼊接⼝ * @access public * @param array $log ⽇志信息 * @return bool */ public function save (array $log ): bool { $destination = $this ->getMasterLogFile ();
2
3
4
5
6
7
8
9
10111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
$path = dirname ($destination ); !is_dir ($path ) && mkdir ($path , 0755, true ); $info = []; // ⽇志信息封装 $time = DateTime ::createFromFormat ('0.u00 U', microtime ())->setTimezone (new DateTimeZone (date_default_timezone_get ()))->format ($this $request = Request ::instance (); //新增 $requestInfo = [ 'ip' => $request ->ip (), 'method' => $request ->method (), 'host' => $request ->host (), 'uri' => $request ->url () ]; if (isset ($log ['sql'][0]) && strpos ('CONNECT',$log ['sql'][0])){ } if (!$this ->config ['json']) { $debugInfo = [ 'param' => '[ PARAM ] ' . var_export ($request ->param (), true ), 'header' => '[ HEADER ] ' . var_export ($request ->header (), true ) ]; foreach ($d
ebugInfo as $row ) { array_unshift ($info , $row ); } // 增加额外的调试信息 $runtime = round (microtime (true ) - $this ->app ->getBeginTime (), 10); $reqs = $runtime > 0 ? number_format (1 / $runtime , 2) : '∞'; $memory_use = number_format ((memory_get_usage () - $this ->app ->getBeginMem ()) / 1024, 2); $time_str = '[运⾏时间:' . number_format ($runtime , 6) . 's] [吞吐率:' . $reqs . 'req/s]'; $memory_str = ' [内存消耗:' . $memory_use . 'kb]'; $file_load = ' [⽂件加载:' . count (get_included_files ()) . ']'; array_unshift ($info , $time_str . $memory_str . $file_load ); array_unshift ($info , "---------------------------------------------------------------\r\n[{$time }] {$requestInfo ['ip']} {$requestInfo ['method']} {$requestInfo } foreach ($log as $type => $val ) { $message = []; foreach ($val as $msg ) { if (!is_string ($msg )) { $msg = var_export ($msg , true ); } $message [] = $this ->config ['json'] ? json_encode (['time' => $time , 'type' => $type , 'msg' => $msg ], $this ->config ['json_options']) : sprintf ($this ->config ['format'], $time , $type , $msg ); } if (true === $this ->config ['apart_level'] || in_array ($type , $this ->config ['apart_level'])) { //这⼀句很关键,可以给mysql 或者其他独⽴的⽇志,也加上请求和时间等信息 array_unshift ($message , "---------------------------------------------------------------\r\n[{$time }] {$requestInfo ['ip']} {$requestInfo ['method']} {$requestInfo
/
/ 独⽴记录的⽇志级别 $filename = $this ->getApartLevelFile ($path , $type ); $this ->write ($message , $filename ); continue ; }676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
$info [$type ] = $message ; } if ($info ) { return $this ->write ($info , $destination ); } return true ; } /** * 获取主⽇志⽂件名 * @access public * @return string */ protected function getMasterLogFile (): string { if ($this ->config ['max_files']) { $files = glob ($this ->config ['path'] . '*.log'); try { if (count ($files ) > $this ->config ['max_files']) { unlink ($files [0]); } } catch (Exception $e ) { // } } if ($this ->config ['single']) { $name = is_string ($this ->config ['single']) ? $this ->config ['single'] : 'single'; $destination = $this ->config ['path'] . $name . '.log'; } else { if ($this ->config ['max_files']) { $filename = date ('Ymd') . '.log'; } else { $filename = date ('Ym') . DIRECTORY_SEPARATOR . date ('d') . '.log'; } $destination = $this ->config ['path'] . $filename ; } return $destination ; } /** * 获取独⽴⽇志⽂件名 * @access public * @param string $path ⽇志⽬录 * @param string $type ⽇志类型 * @return string */ protected function getApartLevelFile (string $path , string $type ): string { if ($this ->config ['single']) { $name = is_string ($this ->config ['single']) ? $this -
>config ['single'] : 'single'; $name .= '_' . $type ; } elseif ($this ->config ['max_files']) { $name = date ('Ymd') . '_' . $type ; } else { $name = date ('d') . '_' . $type ; }
132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
效果如下mysql
return $path . DIRECTORY_SEPARATOR . $name . '.log'; } /** * ⽇志写⼊ * @access protected * @param array $message ⽇志信息 * @param string $destination ⽇志⽂件 * @return bool */ protected function write (array $message , string $destination ): bool { // 检测⽇志⽂件⼤⼩,超过配置⼤⼩则备份⽇志⽂件重新⽣成 $this ->checkLogSize ($destination ); $info = []; foreach ($message as $type => $msg ) { $info [$type ] = is_array ($msg ) ? implode (PHP_EOL , $msg ) : $msg ; } $message = implode (PHP_EOL , $info ) . PHP_EOL ; return error_log ($message , 3, $destination ); } /** * 检查⽇志⽂件⼤⼩并⾃动⽣成备份⽂件 * @access protected * @param string $destination ⽇志⽂件 * @return void */ protected function checkLogSize (string $destination ): void { if (is_file ($destination ) && floor ($this ->config ['file_size']) <= filesize ($destination )) { try { rename ($destinatio
n , dirname ($destination ) . DIRECTORY_SEPARATOR . time () . '-' . basename ($destination )); } catch (Exception $e ) { // } } }}
197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243---------------------------------------------------------------[2021-11-11T09:05:00+08:00] 127.0.0.1 POST localhost:4100/api/Dictionary/getDictData [2021-11-11T09:05:00+08:00][sql] CONNECT:[ UseTime:0.083536s ] mysql:host=47.99.78.253;port=3306;dbname=ad_qingclouds_c;charset=utf8---------------------------------------------------------------[2021-11-11T09:05:01+08:00] 127.0.0.1 POST localhost:4100/api/Dictionary/getDictData [2021-11-11T09:05:01+08:00][sql] SHOW FULL COLUMNS FROM `user` [ RunTime:0.056446s ]---------------------------------------------------------------[2021-11-11T09:05:01+08:00] 127.0.0.1 POST localhost:4100/api/Dictionary/getDictData [2021-11-11T09:05:01+08:00][sql] SELECT * FROM `user` WHERE `access_token` = '5bdb8c308b77089bb68754ea37717dd2' LIMIT 1 [ RunTime:0.0557---------------------------------------------------------------[2021-11-11T09:05:01+08:00] 127.0.0.1 POST localhost:4100/api/Dictionary/getDictData [2021-11-11T09:05:01+08:00][sql] SHOW FULL COLUMNS FROM `system_config` [ RunTime:0.060319s ]1
2
3
4
5
6
7
8
9
10111213
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论