feat(log): add opt-in session ID logging

Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
This commit is contained in:
Christoph Wurst 2025-06-16 11:15:03 +02:00
parent 9b29df2dd5
commit c9b316b028
No known key found for this signature in database
GPG key ID: CC42AC2A7F0E56D8
2 changed files with 19 additions and 0 deletions

View file

@ -1141,6 +1141,16 @@ $CONFIG = [
*/
'log.backtrace' => false,
/**
* Enables logging the PHP session ID with each log line
*
* This can be used for session-related debugging, e.g. to see when a session
* is ended or restarted.
*
* Defaults to ``false``.
*/
'log.sessionId' => false,
/**
* This uses PHP.date formatting; see https://www.php.net/manual/en/function.date.php
*

View file

@ -15,11 +15,13 @@ use OC\Log\ExceptionSerializer;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\ILogger;
use OCP\IRequest;
use OCP\ISession;
use OCP\IUserSession;
use OCP\Log\BeforeMessageLoggedEvent;
use OCP\Log\IDataLogger;
use OCP\Log\IFileBased;
use OCP\Log\IWriter;
use OCP\Session\Exceptions\SessionNotAvailableException;
use OCP\Support\CrashReport\IRegistry;
use Throwable;
use function array_merge;
@ -170,6 +172,13 @@ class Log implements ILogger, IDataLogger {
if (!$hasBacktrace && $logBacktrace) {
$entry['backtrace'] = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
}
if ($this->config->getValue('log.sessionId', false)) {
try {
$entry['sessionId'] = \OCP\Server::get(ISession::class)->getId();
} catch (SessionNotAvailableException) {
$entry['sessionId'] = false;
}
}
try {
if ($level >= $minLevel) {