app-insights-php/doctrine-dependency-logger
Doctrine dependency logger for PHP that tracks and records Doctrine-related dependencies/events to help debug and understand runtime wiring. Useful for inspecting Doctrine configuration, service usage, and dependency resolution in apps using Doctrine ORM/DBAL.
Installation Add the package via Composer:
composer require app-insights-php/doctrine-dependency-logger
Register the Logger
In your Laravel service provider (e.g., AppServiceProvider.php), bind the dependency logger to Doctrine’s event system:
use AppInsights\DoctrineDependencyLogger\DoctrineDependencyLogger;
use Doctrine\DBAL\Connection;
public function register()
{
$this->app->bind(DoctrineDependencyLogger::class, function ($app) {
$connection = $app->make(Connection::class);
$logger = new DoctrineDependencyLogger($connection, $app['appinsights']);
return $logger;
});
}
First Use Case: Logging Queries
Attach the logger to Doctrine’s event manager in your bootstrap/app.php or a service provider:
$connection->getEventManager()->addEventListener(
'connection.query',
$this->app->make(DoctrineDependencyLogger::class)
);
Instrument Existing Queries Use the logger to automatically capture SQL queries, execution time, and parameters:
$query = $connection->createQueryBuilder()
->select('*')
->from('users')
->where('id = :id')
->setParameter('id', 1)
->execute();
The logger will emit telemetry to App Insights for this query.
Custom Telemetry Attributes Extend the logger to add custom dimensions (e.g., user context):
$logger->setCustomProperties(['user_id' => auth()->id()]);
Conditional Logging Disable logging for specific queries or environments:
if (app()->environment('production')) {
$connection->getEventManager()->addEventListener(
'connection.query',
$this->app->make(DoctrineDependencyLogger::class)
);
}
DB::connection()->getEventManager() to attach the logger to Eloquent queries.public function handle($request, Closure $next)
{
$logger = app(DoctrineDependencyLogger::class);
$logger->setCustomProperties(['endpoint' => $request->path()]);
return $next($request);
}
connection.query events to log aggregated metrics.Performance Overhead
staging/production) or sample queries:
if (rand(0, 100) < 10) { // 10% sampling
$logger->logQuery($event);
}
Missing Telemetry
$connection->getEventManager()->addEventListener('connection.query', $logger);
Parameter Masking
DoctrineDependencyLogger::getMaskedParams() to redact sensitive fields:
protected function getMaskedParams(array $params): array
{
$params['password'] = '[REDACTED]';
return $params;
}
connection.query:
$listeners = $connection->getEventManager()->getListeners('connection.query');
Custom Telemetry Extend the logger to emit additional metrics (e.g., query complexity):
public function logQuery($event)
{
$this->telemetryClient->trackDependency(
'DoctrineQuery',
$event->getSql(),
'SQL',
DateInterval::fromMillis($event->getExecutionTime())
);
// Add custom logic here
}
Async Logging Offload logging to a queue for high-traffic apps:
public function logQuery($event)
{
QueryLogJob::dispatch($event->getSql(), $event->getExecutionTime());
}
Environment-Specific Config Use Laravel’s config to toggle logging:
'app_insights' => [
'doctrine_logging' => env('APP_INSIGHTS_DOCTRINE_LOGGING', false),
],
Then conditionally attach the logger:
if (config('app_insights.doctrine_logging')) {
$connection->getEventManager()->addEventListener('connection.query', $logger);
}
How can I help you explore Laravel packages today?