Installation:
composer require aimeos/ai-monolog
Ensure aimeos/aimeos-laravel is also installed (core dependency).
Configuration:
Add to config/logging.php under channels:
'aimeos' => [
'driver' => 'custom',
'via' => \Aimeos\AI\Monolog\Handler::class,
'config' => [
'client' => [
'host' => env('AIMEOS_HOST', 'localhost'),
'port' => env('AIMEOS_PORT', 8080),
],
'logger' => [
'name' => 'aimeos',
'level' => env('AIMEOS_LOG_LEVEL', \Monolog\Logger::DEBUG),
],
],
],
First Use Case: Log a debug message to Aimeos:
\Log::channel('aimeos')->debug('User viewed product', [
'product_id' => 123,
'user_id' => auth()->id(),
]);
Contextual Logging: Attach Aimeos-specific metadata (e.g., session, product, or order data) to logs:
$context = [
'aimeos' => [
'session' => session()->all(),
'product' => $product->getId(),
],
];
\Log::channel('aimeos')->info('Order processed', $context);
Error Handling: Catch exceptions and log them with Aimeos:
try {
// Risky operation
} catch (\Exception $e) {
\Log::channel('aimeos')->error('Payment failed', [
'exception' => $e->getMessage(),
'trace' => $e->getTraceAsString(),
'order_id' => $order->getId(),
]);
}
Performance Tracking: Log request durations with timestamps:
$start = microtime(true);
// ... business logic ...
\Log::channel('aimeos')->debug('Request completed', [
'duration_ms' => (microtime(true) - $start) * 1000,
'route' => request()->route()->getName(),
]);
Middleware: Use the channel in middleware to log API requests:
public function handle($request, Closure $next) {
\Log::channel('aimeos')->info('API request', [
'method' => $request->method(),
'path' => $request->path(),
'ip' => $request->ip(),
]);
return $next($request);
}
Events: Log custom events (e.g., order.created):
event(new OrderCreated($order));
// In EventServiceProvider:
protected $listen = [
OrderCreated::class => [\App\Listeners\LogOrderEvent::class],
];
Queue Jobs: Log job execution in handle():
public function handle() {
\Log::channel('aimeos')->debug('Job started', ['job' => $this->job]);
// ... job logic ...
\Log::channel('aimeos')->debug('Job completed');
}
Connection Issues:
'config' => [
'client' => [...],
'fallback' => true, // Logs to default channel if Aimeos fails
],
Serialization:
->toArray() or custom handlers:
\Log::channel('aimeos')->debug('Product', [
'data' => $product->toArray(),
]);
Performance Overhead:
AIMEOS_LOG_LEVEL matches your needs (e.g., DEBUG for development, INFO for production).config/aimeos.php:
'client' => [
'debug' => env('AIMEOS_DEBUG', false),
],
php artisan config:clear
Custom Handlers:
Extend \Aimeos\AI\Monolog\Handler to add pre-processing:
class CustomHandler extends \Aimeos\AI\Monolog\Handler {
protected function getContext($record) {
$context = parent::getContext($record);
$context['custom'] = 'value';
return $context;
}
}
Dynamic Channels:
Create dynamic channels for different Aimeos services (e.g., aimeos.catalog, aimeos.order):
$channel = \Log::channel('aimeos')->withContext(['service' => 'catalog']);
Log Enrichment: Use Monolog processors to enrich logs before sending:
'config' => [
'processors' => [
new \Monolog\Processor\UidProcessor(),
new \Monolog\Processor\MemoryUsageProcessor(),
],
],
How can I help you explore Laravel packages today?