Laravel 重写日志,让日志更优雅

重写了日志名目

插手trace,一次请求的独一标识

插手error级别信息推送,事例中利用企业微信群助手

让我们可以更实时、更优雅、更利便追踪日志信息

有助于初学者相识Laravel框架

1。将文件 AppTool.php、Logger.php、LogServiceProvider.php复制到 app/Providers文件夹下,将文件BaseCommand.php复制到App\Console下

2 。在config/app.php→providers中插手

'providers' => [ …… // 注册日志 App\Providers\LogServiceProvider::class …… ];

3。在项目中利用如下方法挪用

// php-fpm方法挪用 日志路径 /opt/logs/xxx.log /opt/logs/xxx.error \Log::info("info"); \Log::debug("debug"); \Log::error("error"); // 在cli方法挪用 日志路径 /opt/clogs/xxx.log /opt/clogs/xxx.error app('cLog')->info("info"); app('cLog')->debug("debug"); app('cLog')->error("error");

4。在日志级别为error时,会执行推送,才干例中回收企业微信群推送

/** * 推送错误信息 * @param $message */ public function pushErrorMessage($message) { $content = "app:". static::getAppName() ." src: ". static::getRequestSource() ." trace:". self::getTrace() ." url:". static::$uri_info ." error: ". $message ." time:". date("Y-m-d H:i:s"); // 测试群 $url = "xxxxxxxxxxxx"; $result = app('\GuzzleHttp\Client')->request('POST', $url, [ \GuzzleHttp\RequestOptions::JSON=>[ "msgtype"=> "text", "text"=> [ "content" => $content ] ] ]); $body = \GuzzleHttp\json_decode($result->getBody()->getContents(), true); }

5 。日志内容

留意事项:

修改如下代码差异版本bind部门会有所差异,详细按照\Illuminate\Foundation\Application::registerCoreContainerAliases中log信息修改。
如laravel6.x中为'log' => [\Illuminate\Log\LogManager::class, \Psr\Log\LoggerInterface::class],,

修改方法就如下方代码

…… // 注入全局容器 $app->instance('Log', $logger); $app->bind('Psr\Log\LoggerInterface', function (Application $app) { return $app['log']->getLogger(); }); $app->bind('\Illuminate\Log\LogManager', function (Application $app) { return $app['log']; }); ……

有关console中利用时,发起重写\Illuminate\Console\Command::info、\Illuminate\Console\Command::line、\Illuminate\Console\Command::error,然后所有console担任BaseCommand

demo代码块:

use App\Console\BaseCommand; class Demo extends BaseCommand { protected $signature = 'command:demo'; protected $description = 'demo'; public function __construct() { parent::__construct(); } public function handle() { $this->info('this is info!'); $this->line('this is line!'); $this->error('this is error!!!'); } }

demo 呼吁行输出:

到此这篇关于Laravel 重写日志,让日志更优雅的文章就先容到这了,更多相关Laravel 重写日志内容请搜索剧本之家以前的文章或继承欣赏下面的相关文章但愿各人今后多多支持剧本之家!

您大概感乐趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wsjxgy.html