Automatically reset Monolog processors between background jobs #42670
Unanswered
mortenscheel
asked this question in
Ideas
Replies: 1 comment
-
I have a similar problem, but ended up with a new queue worker class ResetLoggerAwareWorker extends Worker
{
public function __construct(
private readonly LogManager $logger,
QueueManager $manager,
Dispatcher $events,
ExceptionHandler $exceptions,
callable $isDownForMaintenance,
callable $resetScope = null
) {
parent::__construct($manager, $events, $exceptions, $isDownForMaintenance, $resetScope);
}
protected function runJob($job, $connectionName, WorkerOptions $options)
{
try {
return parent::runJob($job, $connectionName, $options);
} finally {
$logger = $this->logger->driver();
if ($logger instanceof Logger) {
$logger = $logger->getLogger();
}
if ($logger instanceof \Monolog\Logger) {
$logger->close();
}
if ($logger instanceof ResettableInterface) {
$logger->reset();
}
}
}
} the problem is with |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Monolog provides a
ResettableInterface
that allows Processors and Handlers to reset their state, which can help avoid memory leaks in long running processes.Telling Monolog to reset all resettable handlers and processors at the start of each background job is pretty simple:
But maybe this is something Laravel should do automatically?
I haven't personally experienced memory leaks due to logging, but I like adding Monolog's UidProcessor which appends an identifier to all log messages that changes between each request. This makes it super easy to find all log messages related to a specific request, but since queue workers reuse the same Logger instance, the log uid remains the same for the lifetime of the worker.
Beta Was this translation helpful? Give feedback.
All reactions