Laravel comes with a built in error and exception handling functions to create the log files.Laravel is also integrated with the Monolog logging library, which support variety of log handlers.Monolog sends logs to files,sockets,inboxes,databases and various web services.
Configuring Custom Logging (File and DB logging)
- Configure the Logging mode in the .env file.
AUDIT_TRAIL=DB [DB - Store the logs in the database, FILE -Store the logs in the file(/storage/logs/Audit_Trail_<YYY-MM-DD>)]
- Create the model class. Issue the following command on the terminal.
php artisan make:model model/log --migration
Laravel will create the log.class in the app/model folder and also create the migration file in the database/migrations folder
- Make the following changes in the migration file.
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateLogsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('logs', function (Blueprint $table) {
$table->increments('id');
$table->string('userid',50);
$table->string('action',250);
$table->text('message');
$table->string('ipaddress',50);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('logs');
}
}
|
---|
- Run the following command
php artisan migrate
- Create DBHandler.php file to store the logs into the database and paste the following code (App/Utilities folder)<?phpnamespace App\Utilities;use Monolog\Logger;use Monolog\Handler\AbstractProcessingHandler;use App\model\log;
class DBHandler extends AbstractProcessingHandler{public function __construct($level = Logger::DEBUG,$bubble = true){parent::__construct($level, $bubble);}
protected function write(array $record){$objLog = new log();$objLog->userid = '1'; //to be changed$objLog->action = $record['context']['path'];$objLog->message = $record['formatted'];$objLog->ipaddress = $record['context']['ip'];$objLog->save();}
} - Create AuditTrail.php file to handle the log request (App/Utilities folder)
<?php
namespace App\Utilities;
use Monolog\Logger;
use App\Utilities\PDOHandler;
use App\Utilities\DBHandler;
use Monolog\Handler\StreamHandler;
use Illuminate\Support\Facades\Request;
class AuditTrail{
public function logData($action,$message){
$ipAddress = Request::ip();
$browser = Request::header('User-Agent');
$path = Request::path();
$url = Request::fullUrl();
$message = (!empty($message))?$message:null;
$action = (!empty($action))?$action:null;
$log = new Logger('Audit Log');
if(!empty(env('AUDIT_TRAIL')) && env('AUDIT_TRAIL') == 'FILE'){
$fileName = 'Audit_Trail_'.date('Y-m-d');
$log->pushHandler(new StreamHandler(storage_path().'/logs/'.$fileName.'.log', Logger::DEBUG));
$log->addInfo($action,array('message'=>$message,'ip'=>$ipAddress,'browser'=>$browser,'path'=>$path,'url'=>$url));
}
else if(!empty(env('AUDIT_TRAIL')) && env('AUDIT_TRAIL') == 'DB'){
$log->pushHandler(new DBHandler());
$log->addInfo($action,array('message'=>$message,'ip'=>$ipAddress,'browser'=>$browser,'path'=>$path,'url'=>$url));
}
}
}
|
---|
How to log the action
- Create Controller class. Issue the following command
php artisan make:controller MyController (Create MyController.php file in App/Http/Controllers folder)
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Utilities\AuditTrail;
class MyController extends Controller
{
public function __construct(AuditTrail $AuditTrail){
$this->objAuditTrail = $AuditTrail;
}
public function Profile(){
$this->objAuditTrail->logData(array('action'=>'My Controller : Profile Action','message'=>'Profile data inserted successfully'));
}
}
|
---|
No comments:
Post a Comment