guilhem / js-log
一个在JavaScript控制台调试PHP的库,以便在调试时不会影响前端
v0.7
2023-02-27 13:12 UTC
This package is not auto-updated.
Last update: 2024-09-24 14:57:32 UTC
README
一个易于使用的PHP库,可在js控制台显示日志。调试PHP而不破坏前端
安装
composer require guilhem/js-log
开始使用
JsLog是一个极简的库,允许你在代码中添加调试信息。
- 在代码中导入该包
require('vendor/autoload.php'); use JsLog\JsLog; // Generate an instance like this $jslog = JsLog::getInstance();
- 使用它
// Use it from the instance $jslog->log('Your log'); // Or use it without instance JsLog::getInstance()->msg('Success message !'); // Render error $jslog->err('Error triggered...');
- 自定义渲染器
Jslog允许你通过自定义函数使用CSS渲染你的消息
// Render a custom message, with customm CSS $jslog->custom( "[CUSTOM DEBUG]\n", "color:orange", "Whouhou it worked\n", "color:red;border:1px solid red;", "Whouhou it worked\n", "color:darkgrey;font-weight:800;", "..Debug stop..\n", "color:blue;font-weight:bold" );
- 查看消息
在您的网页浏览器中查看JavaScript控制台
- 何时发送调试信息?
如果你在构造时传递了True参数,Jslog将只在输出缓冲开始时发送调试信息。否则,消息将被放入一个FIFO数组。消息队列将在下一个调用日志函数或脚本结束时刷新。否则,它将在调用时直接发送调试信息
示例
以下是以下脚本的输出结果
<?php require('vendor/autoload.php'); use JsLog\JsLog; // By adding True jsLog will wait until headers has been sent (relying on ob if activated else `headers_sent` function) // By default, it is called with none, and will log as soon as called :D JsLog::getInstance(True)->log('Hey from javascript console before headers was sent'); echo "<!DOCTYPE html><html><head></head><body>"; $jslog = JsLog::getInstance(); $jslog->msg("A message from PHP display in js console"); $jslog->err("Error here: \"" . $_SERVER['SCRIPT_NAME'] . '"'); $jslog->custom( "[CUSTOM DEBUG]\n", "color:orange", "Whouhou it worked\n", "color:red;border:1px solid red;", "Whouhou it worked\n", "color:darkgrey;font-weight:800;", "..Debug stop..\n", "color:blue;font-weight:bold" ); echo "</body></html>";
在JavaScript控制台内部