• 企业400电话
  • 微网小程序
  • AI电话机器人
  • 电商代运营
  • 全 部 栏 目

    企业400电话 网络优化推广 AI电话机器人 呼叫中心 网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    Laravel 重写日志,让日志更优雅

    更改目的:

    1。将文件 AppTool.phpLogger.phpLogServiceProvider.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::registerCoreContainerAliaseslog信息修改。
    如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 重写日志内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    您可能感兴趣的文章:
    • Laravel的加密解密与哈希实例讲解
    • Laravel 数据库加密及数据库表前缀配置方法
    • laravel 5.3中自定义加密服务的方案详解
    • Laravel中10个有用的用法小结
    • Laravel中的where高级使用方法实例讲解
    • laravel使用组件实现微信网页授权登入
    • Laravel统一封装接口返回状态实例讲解
    • 浅谈Laravel中如何对大文件进行加密
    上一篇:laravel使用组件实现微信网页授权登入
    下一篇:Laravel中的where高级使用方法实例讲解
  • 相关文章
  • 

    © 2016-2020 巨人网络通讯

    时间:9:00-21:00 (节假日不休)

    地址:江苏信息产业基地11号楼四层

    《增值电信业务经营许可证》 苏B2-20120278

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