Installation:
composer require rstriquer/eloquent-logger --dev
Enable Logging:
Add to your .env:
ELOQUENT_LOGGER_ENABLED=true
storage/logs/eloquent.log (created automatically).First Use Case:
User::all()) and check storage/logs/eloquent.log for the raw SQL.Conditional Activation:
.env or dynamically in code:
if (app()->environment('local')) {
\EloquentLogger::enable();
}
Query Filtering:
\EloquentLogger::setFilter(function ($query) {
return $query->getModel() instanceof \App\Models\User;
});
Integration with Debugging Tools:
\EloquentLogger::enable();
\DB::enableQueryLog();
// Run queries, then inspect both logs.
Testing:
phpunit.xml:
<env key="ELOQUENT_LOGGER_ENABLED" value="true"/>
Performance Overhead:
\EloquentLogger::disable();
storage/logs/eloquent.log) to avoid disk issues.File Size Limits:
php artisan eloquent-logger:clear
storage/logs has sufficient inode limits.Environment Leakage:
eloquent.log to version control. Add to .gitignore:
storage/logs/eloquent.log
Raw SQL Formatting:
?. For readability, use:
\EloquentLogger::setFormatter(function ($sql, $bindings) {
return \DB::connection()->prepare($sql)->toSql();
});
Log Location:
storage/logs/eloquent.log (check permissions).Query Not Logging?:
ELOQUENT_LOGGER_ENABLED=true and the query uses Eloquent (not raw PDO).Custom Log Path:
config(['eloquent-logger.path' => storage_path('logs/custom_eloquent.log')]);
Extending Functionality:
use Rstriquer\EloquentLogger\EloquentLogger;
class CustomLogger extends EloquentLogger {
protected function logQuery($query) {
$this->writeToLog("[$this->timestamp] " . $query);
}
}
Then bind your custom class in config/eloquent-logger.php.logrotate or a cron job:
find /path/to/storage/logs -name "eloquent.log" -type f -exec truncate -s 0 {} \;
sync queues for debugging.How can I help you explore Laravel packages today?