cashier/microservice-log-bundle
Installation Add the bundle via Composer:
composer require cashier/microservice-log-bundle
Enable the bundle in config/bundles.php (Symfony):
return [
// ...
Cashier\MicroserviceLogBundle\CashierMicroserviceLogBundle::class => ['all' => true],
];
Configuration Publish the default config:
php bin/console cashier:log:config:init
Update config/packages/cashier_microservice_log.yaml with your logging targets (e.g., ELK, Loki, or custom HTTP endpoints):
cashier_microservice_log:
targets:
- { type: 'http', url: '%env(LOG_TARGET_URL)%' }
include_context: true
First Use Case Inject the logger into a service:
use Cashier\MicroserviceLogBundle\Logger\MicroserviceLoggerInterface;
class OrderService {
public function __construct(private MicroserviceLoggerInterface $logger) {}
public function processOrder() {
$this->logger->info('Order processed', ['order_id' => 123]);
}
}
The log will automatically include:
service_name, instance_id).include_context: true).Structured Logging Use structured data for observability:
$this->logger->error('Payment failed', [
'amount' => 99.99,
'currency' => 'USD',
'gateway' => 'stripe',
'metadata' => ['customer_id' => 'cust_123']
]);
Context Propagation Attach request-scoped context (e.g., user ID, request ID):
$this->logger->withContext(['user_id' => $user->id])->info('User action');
Batching Logs For high-throughput services, batch logs before sending:
cashier_microservice_log:
batch:
enabled: true
max_items: 50
flush_interval: 300 # seconds
Integration with Symfony Monolog Extend existing Monolog handlers:
// config/packages/monolog.yaml
monolog:
handlers:
cashier_log:
type: service
id: Cashier\MicroserviceLogBundle\Logger\MicroserviceHandler
level: debug
targets:
- { type: 'http', url: '%env(DEBUG_LOG_URL)%', levels: [debug] }
- { type: 'http', url: '%env(PROD_LOG_URL)%', levels: [info, warning, error] }
cashier_microservice_log:
correlation_id_header: 'X-Request-ID'
cashier_microservice_log:
async: true
queue_name: 'log_queue'
Missing Metadata
Ensure service_name and instance_id are set in config. Defaults to unknown if omitted:
cashier_microservice_log:
service_name: 'order-service'
instance_id: '%kernel.environment%.%hostname%'
Synchronous Blocking
Avoid flush() in async mode—logs may be delayed or dropped:
// ❌ Bad: Blocks execution
$this->logger->flush();
// ✅ Good: Let the bundle handle it
$this->logger->info('Non-blocking log');
Target Failures HTTP targets throw exceptions on failure. Use retries or dead-letter queues:
targets:
- { type: 'http', url: '%env(LOG_URL)%', retries: 3, timeout: 5 }
Context Leakage
Sensitive data (e.g., passwords) in withContext() may leak to logs. Sanitize explicitly:
$this->logger->withContext(['user_id' => $user->id])->info('Login');
// Avoid: $this->logger->info('Login', ['token' => $user->apiToken]);
debug to inspect bundle behavior:
cashier_microservice_log:
debug: true
targets:
- { type: 'http', url: 'http://localhost:3000/logs' }
curl -H "X-Request-ID: test123" http://your-service/api
Custom Handlers
Implement Cashier\MicroserviceLogBundle\Logger\TargetInterface for new targets (e.g., Kafka, S3):
class S3Target implements TargetInterface {
public function send(array $logs): void {
// Upload to S3
}
}
Register in config:
targets:
- { type: 's3', bucket: 'logs-bucket' }
Preprocessors
Modify logs before sending via Cashier\MicroserviceLogBundle\Event\LogEvent:
// config/services.yaml
Cashier\MicroserviceLogBundle\EventListener\LogPreprocessorListener:
tags:
- { name: kernel.event_listener, event: cashier.log.preprocess, method: onPreprocess }
Metrics Track log volume/latency via Prometheus:
cashier_microservice_log:
metrics:
enabled: true
namespace: 'cashier_logging'
How can I help you explore Laravel packages today?