Installation:
composer require babzich/logentries-bundle
Add to AppKernel.php:
new Bab\LogentriesBundle\BabLogentriesBundle(),
Configuration:
Add to config/packages/bab_logentries.yaml (create if missing):
bab_logentries:
token: "%env(LOGENTRIES_TOKEN)%"
host: "%env(LOGENTRIES_HOST)%" # Optional (defaults to logentries.com)
First Use Case: Inject the logger in a service/controller:
use Psr\Log\LoggerInterface;
class MyController
{
public function __construct(private LoggerInterface $logger)
{
}
public function index()
{
$this->logger->info('Test log message');
}
}
Logging in Controllers/Commands:
$this->logger->debug('Debug info', ['context' => 'key']);
$this->logger->error('Error occurred', ['exception' => $e]);
Dynamic Log Levels:
Use env() or config to toggle log levels:
bab_logentries:
level: "%env(LOG_LEVEL)%" # e.g., "debug", "info"
Tagging Logs: Add tags via context:
$this->logger->info('User action', ['tags' => ['user', 'auth']]);
Integration with Monolog: Extend the handler for custom logic:
$handler = new \LogEntries\Handler(
$this->get('bab_logentries.token'),
$this->get('bab_logentries.host')
);
$handler->setFormatter(new \Monolog\Formatter\LineFormatter());
%env() for tokens/hosts to avoid hardcoding.RotatingFileHandler for local fallback.['user_id' => 123]).Token/Host Validation:
LOGENTRIES_TOKEN/HOST. Validate in config/validator.yaml:
services:
Bab\LogentriesBundle\Validator\LogentriesValidator:
arguments: ["%env(LOGENTRIES_TOKEN)%"]
Dependency Conflicts:
logentries/logentries-monolog-handler to dev-master. Pin to a stable version in composer.json:
"require": {
"logentries/logentries-monolog-handler": "^1.0"
}
Network Dependencies:
BufferHandler for resilience:
$handler = new \Monolog\Handler\BufferHandler(new \LogEntries\Handler($token));
$handler = $this->logger->getHandlers()[0];
if (!$handler->isHandling()) {
$this->logger->error('Logentries handler inactive!');
}
$handler->setBatchMode(100); // Batch 100 logs per request
Custom Formatter:
Override the default formatter in services.yaml:
services:
Bab\LogentriesBundle\Handler\LogEntriesHandler:
arguments:
- "%env(LOGENTRIES_TOKEN)%"
- "%env(LOGENTRIES_HOST)%"
calls:
- [setFormatter, ["@custom_logentries_formatter"]]
Pre-Process Logs: Use a Monolog processor:
$processor = new \Monolog\Processor\UidProcessor();
$this->logger->pushProcessor($processor);
Fallback Handlers:
Chain with other handlers (e.g., StreamHandler):
$this->logger->pushHandler(
new \Monolog\Handler\StreamHandler('/var/log/app.log', Monolog\Logger::DEBUG)
);
How can I help you explore Laravel packages today?