/** * Generic audit logger. * * This is set up as a listener for the after save event by the * bootAuditable method. * * Store a message in the logs table in the database. * Use a raw query for this to prevent looping. * * Just for the time being we are logging direct to a table rather * than going through the Monolog / Laravel logger API. * * @param Model $target * @return void */ public static function eventAuditLogger($target) { // exists flag is set on an existing record. if ($target->exists) { $message = 'UPDATE'; } else { $message = 'INSERT'; } // Fetch the currently logged in user $username = ApplogHelper::currentUserName(); // Get the list of client IP addresses $clientIp = ApplogHelper::getClientIps(); // Also get any addresses in _SERVER["HTTP_X_FORWARDED_FOR"] $forwardAddress = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : ''; if (empty($clientIp)) { $clientIp = $forwardAddress; } elseif (!empty($forwardAddress)) { $clientIp = $clientIp . ',' . $forwardAddress; } // Store the audit log. try { DB::table('applogs')->insert(['type' => 'audit', 'modelname' => get_class($target), 'foreign_id' => $target->id, 'classname' => __CLASS__, 'traitname' => __TRAIT__, 'functionname' => __FUNCTION__, 'filename' => __FILE__, 'linenumber' => __LINE__, 'message' => $message, 'details' => $message . ' in ' . $target->getTable() . ' table.', 'ipaddr' => $clientIp, 'created_by' => $username, 'updated_by' => $username, 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s')]); } catch (\Exception $e) { // Do nothing } }
/** * 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 } }); }
/** * Log a message to the application log. * * Can be called directly or from the Laravel log listener. * * @param string $level * @param string $message * @param mixed $context * @return void */ public static function log($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 { $error = static::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]); // Send an email out for error messages -- example code below. /* if ($level == 'error') { MandrillMailerRepository::sendError(env('MAIL_TO_ADDRESS'), env('MAIL_TO_NAME'), $error); } */ } catch (\Exception $e) { // Do nothing if the log fails -- possibly fatal database error } }