laravel 打印sql运行记录

简介:

app/Providers/AppServiceProvider.php

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
 
namespace  App\Providers;
 
use  Blade;
use  Illuminate\Support\ServiceProvider;
use  Illuminate\Support\Facades\DB;
use  Illuminate\Contracts\Logging\Log;
 
class  AppServiceProvider  extends  ServiceProvider
{
     /**
      * Bootstrap any application services.
      *
      * @return void
      */
     public  function  boot()
     {
         Blade::directive( 'currency' function  ( $x ) {
             return  "<?php if (round((float)$x, 2)) { echo number_format(round((float)$x, 2), 2); } ?>" ;
         });
 
         Blade::directive( 'currency_with_zero' function  ( $x ) {
             return  "<?php echo number_format(round((float)$x, 2), 2); ?>" ;
         });
 
         Blade::directive( 'selector' function  ( $x ) {
             return  "<?php echo 'ac-' . strtr($x, '.', '-'); ?>" ;
         });
         DB::listen(
 
             function  ( $sql ) {
                 // $sql is an object with the properties:
                 //  sql: The query
                 //  bindings: the sql query variables
                 //  time: The execution time for the query
                 //  connectionName: The name of the connection
 
                 // To save the executed queries to file:
                 // Process the sql and the bindings:
                 foreach  ( $sql ->bindings  as  $i  =>  $binding ) {
                     if  ( $binding  instanceof  \DateTime) {
                         $sql ->bindings[ $i ] =  $binding ->format( '\'Y-m-d H:i:s\'' );
                     else  {
                         if  ( is_string ( $binding )) {
                             $sql ->bindings[ $i ] =  "'{$binding}'" ;
                         }
                     }
                 }
                 // Insert bindings into query
                 $query  str_replace ( array ( '%' '?' ),  array ( '%%' '%s' ),  $sql ->sql);
                 $query  = vsprintf( $query $sql ->bindings);
 
                 \Log::debug( $query );
                 \Log::debug( $sql ->time .  ' ms'  . PHP_EOL . PHP_EOL);
             }
         );
     }
 
     /**
      * Register any application services.
      *
      * @return void
      */
     public  function  register()
     {
         //
     }
}









本文转自 hgditren 51CTO博客,原文链接:http://blog.51cto.com/phpme/1958744,如需转载请自行联系原作者
目录
相关文章
|
6月前
|
SQL 数据库
PowerDesigner导出SQL脚本运行注释出现乱码问题
PowerDesigner导出SQL脚本运行注释出现乱码问题
153 0
|
SQL Java 数据库连接
springboot-maven项目+jpa 运行过程中执行resources下sql脚本文件-ClassPathResource和ScriptUtils.executeSqlScript的使用
springboot-maven项目+jpa 运行过程中执行resources下sql脚本文件-ClassPathResource和ScriptUtils.executeSqlScript的使用
503 0
springboot-maven项目+jpa 运行过程中执行resources下sql脚本文件-ClassPathResource和ScriptUtils.executeSqlScript的使用
|
4天前
|
SQL 资源调度 Oracle
Flink CDC产品常见问题之sql运行中查看日志任务失败如何解决
Flink CDC(Change Data Capture)是一个基于Apache Flink的实时数据变更捕获库,用于实现数据库的实时同步和变更流的处理;在本汇总中,我们组织了关于Flink CDC产品在实践中用户经常提出的问题及其解答,目的是辅助用户更好地理解和应用这一技术,优化实时数据处理流程。
|
4天前
|
SQL 存储 数据处理
Flink SQL 问题之提交程序运行报错如何解决
Flink SQL报错通常指在使用Apache Flink的SQL接口执行数据处理任务时遇到的问题;本合集将收集常见的Flink SQL报错情况及其解决方法,帮助用户迅速恢复数据处理流程。
72 3
|
6月前
|
SQL 分布式计算 调度
在MaxCompute中,你可以通过SQL语句来查询和导出实例的运行状态和时间等信息
在MaxCompute中,你可以通过SQL语句来查询和导出实例的运行状态和时间等信息
63 5
|
9月前
|
SQL 关系型数据库 MySQL
Navicat如何运行SQL脚本文件
一朋友初入门,今天导入sql文件时出错,我就简单记录了一下
1347 1
|
11月前
|
SQL 数据库
在运行数据库脚本的时候报sql语法错,按理来说数据库脚本既然被导出来了那应该是不会有什么语法错误的
在运行数据库脚本的时候报sql语法错,按理来说数据库脚本既然被导出来了那应该是不会有什么语法错误的
83 0
|
SQL 关系型数据库 MySQL
【大数据系列之MySQL】(十):使用Navicat运行本地sql文件
【大数据系列之MySQL】(十):使用Navicat运行本地sql文件
326 0
【大数据系列之MySQL】(十):使用Navicat运行本地sql文件
|
SQL 监控 druid
SpringBoot 如何统计、监控 SQL运行情况?
SpringBoot 如何统计、监控 SQL运行情况?
SpringBoot 如何统计、监控 SQL运行情况?
|
SQL XML Java
【Log日志】打印mybatis中sql日志并存放到指定文件中
【Log日志】打印mybatis中sql日志并存放到指定文件中
815 0
【Log日志】打印mybatis中sql日志并存放到指定文件中