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
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { describe, it, expect, vi } from 'vitest';
import { collectPhpLogs } from './collect-php-logs';
import type { UniversalPHP } from '../types';
import type { Logger } from '../logger';

describe('PHP log collection after a site import', () => {
it('reads appended records once and restarts after replacement or removal', async () => {
const logs = createCollector();
await logs.request('old log');
await logs.request('old log\nnew error');
await logs.request('old log\nnew error');
await logs.request('fatal');
await logs.request('other'); // Same length, but an imported file.
await logs.request('');
await logs.request('fatal');
expect(logs.messages()).toEqual([
'old log',
'\nnew error',
'fatal',
'other',
'fatal',
]);
});
it('does not share offsets between Playgrounds', async () => {
const first = createCollector();
const second = createCollector();
await first.request('a much longer old log');
await second.request('error');
expect(second.messages()).toEqual(['error']);
});
});

/** Captures request-end records from one independently tracked Playground log. */
function createCollector() {
const callbacks: Record<string, () => Promise<void>> = {};
let log = '';
const logger = { logMessage: vi.fn() };
collectPhpLogs(
logger as unknown as Logger,
{
addEventListener: (name: string, callback: () => Promise<void>) => {
callbacks[name] = callback;
},
fileExists: async () => !!log,
readFileAsText: async () => log,
} as unknown as UniversalPHP
);
return {
request: async (next: string) => {
log = next;
await callbacks['request.end']();
},
messages: () =>
logger.logMessage.mock.calls.map(([record]) => record.message),
};
}
12 changes: 8 additions & 4 deletions packages/php-wasm/logger/src/lib/collectors/collect-php-logs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { UniversalPHP, PHPRequestErrorEvent } from '../types';
import { type Logger, LogPrefix, LogSeverity } from '../logger';

let lastPHPLogLength = 0;
export const errorLogPath = '/wordpress/wp-content/debug.log';

/**
Expand All @@ -26,16 +25,21 @@ export const collectPhpLogs = (
loggerInstance: Logger,
playground: UniversalPHP
) => {
let lastPHPLog = '';
playground.addEventListener('request.end', async () => {
const log = await getRequestPhpErrorLog(playground);
if (log.length > lastPHPLogLength) {
const currentLog = log.substring(lastPHPLogLength);
// Imports can replace debug.log with a shorter or unrelated file. Track
// each Playground separately and only skip a prefix we actually read.
const currentLog = log.startsWith(lastPHPLog)
? log.substring(lastPHPLog.length)
: log;
lastPHPLog = log;
if (currentLog) {
loggerInstance.logMessage({
message: currentLog,
severity: LogSeverity.Log,
raw: true,
});
lastPHPLogLength = log.length;
}
});
playground.addEventListener('request.error', (event) => {
Expand Down