<?php use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Queue; /* |-------------------------------------------------------------------------- | Database Logging |-------------------------------------------------------------------------- | 1) Add "require app_path().'/errors.php';" to the end of app/start/global.php | 2) Uncomment the "Application Error Logger" section in app/start/global.php | 3) Migrate "create_log_table", or create the table manualy */ Log::listen(function ($level, $message, $context) { // Save the php sapi and date, because the closure needs to be serialized $apiName = php_sapi_name(); $date = new \DateTime(); Queue::push(function () use($level, $message, $context, $apiName, $date) { DB::insert("INSERT INTO log (php_sapi_name, level, message, context, created_at) VALUES (?, ?, ?, ?, ?)", array($apiName, $level, $message, json_encode($context), $date)); }); });
/** * Boot the service provider. * * This method is called after all other service providers have * been registered, meaning you have access to all other services * that have been registered by the framework. * * @return void */ public function boot(DispatcherContract $events) { parent::boot($events); // Publish the database migrations $this->publishes([__DIR__ . '/../database/migrations' => $this->app->databasePath() . '/migrations'], 'migrations'); $this->publishes([__DIR__ . '/../config' => config_path()], 'config'); // Log needs a closure as a listener Log::listen(function ($level, $message, $context) { // Throw out debug messages if we are not in debug mode if ($level == 'debug' && \Config::get('app.debug') != true) { return; } // Fetch the currently logged in user $username = ApplogHelper::currentUserName(); // Get the list of client IP addresses $clientIp = ApplogHelper::getClientIps(); // Split the log message to see how it is formatted. $logdata = explode(':', $message, 6); if (count($logdata) == 6) { list($classname, $traitname, $filename, $linenumber, $functionname, $message) = $logdata; } else { list($classname, $traitname, $filename, $linenumber, $functionname, $message) = ['', '', '', '', '', $message]; } // Store the log entry. try { Applog::create(['type' => $level, 'classname' => $classname, 'traitname' => $traitname, 'filename' => $filename, 'linenumber' => $linenumber, 'functionname' => $functionname, 'message' => $message, 'details' => json_encode($context), 'ipaddr' => $clientIp, 'created_by' => $username, 'updated_by' => $username]); } catch (\Exception $e) { // Do nothing } }); }