Add use of laravel log

        When starting to learn laravel, we generally need to record the operation log of the system, so we read how to record the log, and hereby share:

 

1) Laravel creates daily log files for the application by default in the storage/logs directory. You can write information to the log like this:

 

First, write at the controller layer such as : use Log;

In each of your methods write: 

Log::info('This is some useful information.');

Then go back to the directory /storage/logs/laravel-{date of the day}.log to see if it has been saved to the log.

 

2) Add the sql log information of the execution system running

AppServiceProvider.php 

First, it is necessary to introduce

use DB;

use Monolog\Logger;

use Monolog\Handler\StreamHandler;

 

Then modify the boot() method and add the following code:

 

 DB::listen(function($sql, $bindings, $time) {

           // echo 'SQL statement execution: '.$sql.', parameter: '.json_encode($bindings).', time-consuming: '.$time.'ms';

           $log = new Logger('sql_log');

           $logDir = storage_path('logs/sql');    //storage\logs\sql

             if (!is_dir($logDir)) {

                  mkdir($logDir, 0777, true);

            }

           /* $param = '[';

           foreach($bindings as $x=>$x_value) {

               $param = $param.$x_value.',';

            }

           $param.']';*/

           $log->pushHandler(new StreamHandler($logDir . '/' . date('Y-m-d') . '.log', Logger::DEBUG));

           $log->addInfo('SQL statement execution: '.$sql.', parameter: '.'['.join(", ",$bindings).']'.', time-consuming: '.$time. 'ms');

        });

At this time, the SQL statements running in the system are recorded, and there is no need to manually write and write files.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326263231&siteId=291194637