Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# Logs
logs/**/*
!logs/an_empty_log.txt
*.log
npm-debug.log*
yarn-debug.log*
Expand Down
1 change: 0 additions & 1 deletion packages/join-block/logs/an_empty_log.txt

This file was deleted.

91 changes: 78 additions & 13 deletions packages/join-block/src/Logging.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,90 @@

class Logging
{
public static function getLogDirectory()
{
// Prefer the uploads dir (survives plugin updates), but fall back to
// WP_CONTENT_DIR if uploads is misconfigured or unwritable.
$uploads = wp_upload_dir(null, false);
$basedir = (is_array($uploads) && empty($uploads['error']) && !empty($uploads['basedir']))
? $uploads['basedir']
: null;

$candidates = [];
if ($basedir) {
$candidates[] = $basedir . '/join-block-logs';
}
if (defined('WP_CONTENT_DIR')) {
$candidates[] = WP_CONTENT_DIR . '/join-block-logs';
}
Comment thread
joaquimds marked this conversation as resolved.
Comment thread
joaquimds marked this conversation as resolved.
Comment on lines +18 to +31

$logLocation = null;
$created = false;
foreach ($candidates as $candidate) {
$existed = is_dir($candidate);
if (!$existed && !wp_mkdir_p($candidate)) {
continue;
}
if (!is_writable($candidate)) {
continue;
}
$logLocation = $candidate;
$created = !$existed;
break;
}

Comment thread
joaquimds marked this conversation as resolved.
if ($logLocation === null) {
error_log(
'join-block: unable to create a writable log directory (tried: '
. implode(', ', $candidates ?: ['<none>']) . '); '
. 'file-based logging disabled for this request'
);
Comment on lines +49 to +53
return null;
}

// On first creation, migrate any pre-existing logs from the old
// in-plugin location, which WordPress wipes on plugin update.
if ($created) {
$legacyLocation = __DIR__ . '/../logs';
if (is_dir($legacyLocation)) {
$legacyFiles = scandir($legacyLocation) ?: [];
foreach ($legacyFiles as $file) {
if ($file === '.' || $file === '..') {
continue;
}
$src = $legacyLocation . '/' . $file;
$dst = $logLocation . '/' . $file;
if (is_file($src) && !file_exists($dst)) {
@copy($src, $dst);
Comment thread
joaquimds marked this conversation as resolved.
}
}
Comment on lines +59 to +72
}
}

return $logLocation;
}

public static function init()
{
global $joinBlockLog;
$joinBlockLog = new Logger('join-block');
$logFilenameHash = null;
$logLocation = __DIR__ . "/../logs";
$logFiles = scandir($logLocation);
foreach ($logFiles as $logFile) {
if (str_starts_with($logFile, "debug-")) {
$parts = explode("-", $logFile);
$logFilenameHash = $parts[1];
break;
$logLocation = self::getLogDirectory();
if ($logLocation !== null) {
$logFilenameHash = null;
$logFiles = scandir($logLocation) ?: [];
foreach ($logFiles as $logFile) {
if (str_starts_with($logFile, "debug-")) {
$parts = explode("-", $logFile);
$logFilenameHash = $parts[1];
break;
}
}
if (!$logFilenameHash) {
$logFilenameHash = bin2hex(random_bytes(18));
}
$logFilename = "debug-$logFilenameHash.log";
$joinBlockLog->pushHandler(new RotatingFileHandler("$logLocation/$logFilename", 10, Level::Info));
Comment thread
joaquimds marked this conversation as resolved.
Comment thread
joaquimds marked this conversation as resolved.
}
if (!$logFilenameHash) {
$logFilenameHash = bin2hex(random_bytes(18));
}
$logFilename = "debug-$logFilenameHash.log";
$joinBlockLog->pushHandler(new RotatingFileHandler("$logLocation/$logFilename", 10, Level::Info));
$joinBlockLog->pushProcessor(new WebProcessor());
}

Expand Down
4 changes: 2 additions & 2 deletions packages/join-block/src/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,8 @@ public static function init()
/** @var Html_Field $logField */
$logField = Field::make('html', 'ck_join_flow_log_contents');
$logField->set_html(function () {
$joinBlockLogLocation = __DIR__ . "/../logs";
$logfiles = scandir($joinBlockLogLocation, SCANDIR_SORT_DESCENDING) ?: [];
$joinBlockLogLocation = Logging::getLogDirectory();
$logfiles = $joinBlockLogLocation ? scandir($joinBlockLogLocation, SCANDIR_SORT_DESCENDING) : [];
$logfiles = array_values(array_filter($logfiles, function ($file) use ($joinBlockLogLocation) {
return str_starts_with($file, 'debug-') && is_file($joinBlockLogLocation . '/' . $file);
}));
Comment on lines +242 to 246
Expand Down
2 changes: 1 addition & 1 deletion packages/join-block/tests/SessionLockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function testLockSerializesByKey(): void
$scriptPath = __DIR__ . "/SessionLockTestProcess.php";
// Use an email-shaped key to cover the typical webhook/join lock-key form.
$lockKey = "test+" . microtime(true) . "@example.com";
$logFile = __DIR__ . "/../logs/tests.log";
$logFile = sys_get_temp_dir() . "/join-block-tests.log";
// Ensure clean log file output
// phpcs:ignore WordPress.WP.AlternativeFunctions.unlink_unlink
@unlink($logFile);
Expand Down
2 changes: 1 addition & 1 deletion packages/join-block/tests/SessionLockTestProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
// Set up log file so SessionLockTest can monitor progress of this script
global $joinBlockLog;
$joinBlockLog = new Logger('join-block-test');
$joinBlockLogLocation = __DIR__ . '/../logs/tests.log';
$joinBlockLogLocation = sys_get_temp_dir() . '/join-block-tests.log';
Comment thread
joaquimds marked this conversation as resolved.
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen
$joinBlockLogFile = fopen($joinBlockLogLocation, 'a');
$joinBlockLog->pushHandler(new StreamHandler($joinBlockLogFile, Level::Info));
Expand Down
Loading