logentries/logentries-monolog-handler
Monolog handler for sending Laravel/PHP application logs to Logentries (Rapid7). Adds a custom handler to forward log records over the network, enabling centralized log aggregation, search, and monitoring with minimal setup.
Installation Add the package via Composer (ensure compatibility with PHP/Monolog versions):
composer require logentries/logentries-monolog-handler
Basic Configuration
Update config/logging.php to use the new ingestion endpoint (data.logentries.com):
'logentries' => [
'driver' => 'monolog',
'handler' => Logentries\Handler::class,
'url' => env('LOGENTRIES_URL', 'https://data.logentries.com/logs/your-token/'), // Updated endpoint
'level' => env('LOG_LEVEL', \Monolog\Logger::DEBUG),
'tags' => ['laravel', 'app'],
],
First Use Case Log a test entry in a controller or service:
use Illuminate\Support\Facades\Log;
Log::debug('Testing Logentries integration', ['context' => 'test']);
Logentries/Handler (verify updated endpoint logic)..env for LOGENTRIES_URL (now pointing to data.logentries.com).Conditional Logging
Use the level config to filter logs (e.g., INFO for production):
'level' => env('APP_ENV') === 'production' ? \Monolog\Logger::INFO : \Monolog\Logger::DEBUG,
Contextual Tags
Dynamically add tags via tags config or runtime:
Log::withContext(['user_id' => auth()->id()])->info('User action');
Error Handling Catch exceptions and log them:
try {
// Risky operation
} catch (\Exception $e) {
Log::error('Operation failed', ['exception' => $e]);
}
Laravel’s app/Exceptions/Handler.php
Extend the report() method to log unhandled exceptions:
Log::emergency('Unhandled exception', ['exception' => $exception]);
Queue Failed Jobs
Log job failures in app/Jobs/Job.php:
protected function failed(\Throwable $exception) {
Log::error('Job failed', ['job' => $this, 'exception' => $exception]);
}
Custom Formatter Override the default JSON formatter for structured logs:
'formatter' => new \Monolog\Formatter\LineFormatter(
"[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n"
),
Archived Package
URL Format
LOGENTRIES_URL to use data.logentries.com (not api.logentries.com).Connection Timeouts
ConnectionException or TimeoutException.Context Size
Log::debug('Large data', ['truncated' => array_slice($data, 0, 100)]);
Check Handler Output
Temporarily add a stream handler to debug locally:
'handlers' => [
'stream' => [
'driver' => 'stream',
'path' => storage_path('logs/laravel.log'),
],
'logentries' => [...],
],
Validate Webhook Use Logentries’ test URL to verify logs arrive (updated endpoint).
Custom Transport
Extend Logentries\Handler to support HTTPS proxies or retries (now with timeout fixes):
class CustomLogentriesHandler extends \Logentries\Handler {
protected function send($record) {
// Add retry logic for timeouts
parent::send($record);
}
}
Dynamic Token Rotation
Rotate LOGENTRIES_URL via Laravel’s env() or a config listener:
Config::listen('logging.logentries.url', function ($value) {
// Update token periodically
});
Log Sampling
Implement a wrapper to sample logs (e.g., 1% of DEBUG logs):
if (rand(0, 99) < 1) {
Log::debug('Sampled log');
}
How can I help you explore Laravel packages today?