sinarajabpour1998/log-manager
Laravel log manager with UI components and migrations to store custom activity logs and system error logs in database tables. Define log types in config, use LogFacade to generate logs, and hook into Exception Handler to capture errors.
Installation
Run composer require sinarajabpour1998/log-manager to add the package to your Laravel project.
Publish Assets and Run Migrations Execute these commands to set up the database tables and Blade components:
php artisan vendor:publish --tag=log-manager
php artisan migrate
Add Log Menu to Sidebar
Include the Blade component in your layout (e.g., resources/views/layouts/app.blade.php):
<x-log-menu />
Configure Log Types
Define custom log types in config/log-manager.php:
'log_types' => [
'login' => 'ورود به سایت',
'registration' => 'ثبت نام در سایت',
],
First Log Entry Use the facade to generate a log entry:
use Sinarajabpour1998\LogManager\Facades\LogFacade;
LogFacade::generateLog("login");
LogFacade::generateLog("type") to log predefined events (e.g., user actions).// In a controller or service
LogFacade::generateLog("registration");
App\Exceptions\Handler to log errors via LogFacade::generateErrorLog($e).public function register()
{
$this->reportable(function (Throwable $e) {
if ($this->shouldReport($e)) {
LogFacade::generateErrorLog($e);
}
});
}
LogFacade::generateErrorLog($e, ['user_id' => auth()->id()]);
LogPermissionSeeder) to restrict log viewing/editing.// In a seeder
use Sinarajabpour1998\LogManager\Models\LogPermission;
LogPermission::create(['name' => 'view_logs', 'description' => 'View all logs']);
public function showLogs()
{
$this->authorize('view_logs');
// Show logs logic
}
// Dynamically fetch log types from a database table
$logTypes = DB::table('log_types')->pluck('name', 'key')->toArray();
config(['log-manager.log_types' => $logTypes]);
boot method to merge dynamic config:
public function boot()
{
$this->mergeConfigFrom(__DIR__.'/log-types.php', 'log-manager.log_types');
}
generateSmsLog method for SMS-related events (added in v1.1).LogFacade::generateSmsLog("verification", ["phone" => "+123456789"]);
Facade Aliasing
Add an alias in config/app.php for cleaner usage:
'aliases' => [
'Log' => Sinarajabpour1998\LogManager\Facades\LogFacade::class,
],
Now use Log::generateLog("type") instead of the full facade path.
Middleware for Automatic Logging Create middleware to log requests automatically:
public function handle($request, Closure $next)
{
$response = $next($request);
LogFacade::generateLog("api_request", [
'method' => $request->method(),
'path' => $request->path(),
]);
return $response;
}
Queue Log Entries for Performance Dispatch log entries to a queue to avoid blocking user requests:
use Illuminate\Support\Facades\Bus;
Bus::dispatch(new GenerateLogJob("login"));
Implement GenerateLogJob to extend ShouldQueue and call LogFacade::generateLog().
Customize Log Menu
Override the log-menu Blade component to add filters or actions:
@extends('log-manager::log-menu')
@section('extra-actions')
<button class="btn btn-sm btn-primary">Custom Action</button>
@endsection
Facade Resolution Issues
BaseFacade errors persist after upgrading, ensure the service provider is registered in config/app.php:
'providers' => [
Sinarajabpour1998\LogManager\LogManagerServiceProvider::class,
],
php artisan cache:clear) and restart your server.Missing Log Types
config/log-manager.php will fail silently or throw an error.$validTypes = config('log-manager.log_types');
if (!array_key_exists($type, $validTypes)) {
throw new \InvalidArgumentException("Invalid log type: {$type}");
}
LogFacade::generateLog($type);
Permission Mismatches
php artisan db:seed --class=LogPermissionSeeder
Error Log Overwriting
error_trace field may truncate long stack traces if the database column is too small.error_logs migration and adjust the error_trace column size if needed (e.g., text instead of mediumText).Blade Component Not Found
<x-log-menu /> component may fail if the view is not published or the namespace is incorrect.resources/views/vendor/log-manager/log-menu.blade.php matches your setup.Git Ignoring Custom Logs
storage/logs/customLogs/ might be ignored by Git..gitignore or ensure it’s tracked:
!storage/logs/customLogs/
Log Facade Instantiation Debug facade resolution by checking if the service provider is bound:
dd(app(Sinarajabpour1998\LogManager\LogManagerServiceProvider::class));
Query Logs for Errors Enable Laravel’s query log to debug database issues:
DB::enableQueryLog();
LogFacade::generateLog("test");
dd(DB::getQueryLog());
Check Published Views Verify the Blade component exists after publishing:
ls resources/views/vendor/log-manager/
Validate Migrations
Ensure the logs and error_logs tables exist:
php artisan db:show logs
php artisan db:show error_logs
Test Error Logging
Force an error to test generateErrorLog:
throw new \Exception("Test error for logging");
Check the error_logs table for the entry.
Custom Log Models
Extend the Log or ErrorLog models to add fields:
php artisan make:model LogExtension -m
Add a log_id foreign key and customize the migration.
Hooks for Pre/Post Logging Use Laravel’s events to add logic before/after logging:
// In EventServiceProvider
protected $listen = [
'log.generating' => [LogHookListener::class, 'beforeGenerate'],
'log.generated' => [LogHookListener::class, 'afterGenerate'],
];
API for Log Export Create a controller to export logs as CSV/JSON:
public function exportLogs()
{
return Excel::download(new LogsExport, 'logs.xlsx');
}
Webhook Notifications
How can I help you explore Laravel packages today?