@@ -14,6 +14,7 @@ import {
1414import fs from 'fs' ;
1515import fsPromises from 'fs/promises' ;
1616import https from 'node:https' ;
17+ import os from 'os' ;
1718import nodePath from 'path' ;
1819import * as Sentry from '@sentry/electron/main' ;
1920import { __ , sprintf , LocaleData , defaultI18n } from '@wordpress/i18n' ;
@@ -122,6 +123,38 @@ export {
122123 showUserSettings ,
123124} from 'src/modules/user-settings/lib/ipc-handlers' ;
124125
126+ const DEBUG_LOG_MAX_LINES = 50 ;
127+ const PM2_HOME = nodePath . join ( os . homedir ( ) , '.studio' , 'pm2' ) ;
128+
129+ function readLastLines ( filePath : string , maxLines : number ) : string [ ] | undefined {
130+ try {
131+ if ( ! fs . existsSync ( filePath ) ) {
132+ return undefined ;
133+ }
134+ const content = fs . readFileSync ( filePath , 'utf-8' ) ;
135+ const lines = content . split ( '\n' ) . filter ( ( line ) => line . trim ( ) ) ;
136+ return lines . slice ( - maxLines ) ;
137+ } catch {
138+ return undefined ;
139+ }
140+ }
141+
142+ function readWordPressDebugLog ( sitePath : string ) : string [ ] | undefined {
143+ const debugLogPath = nodePath . join ( sitePath , 'wp-content' , 'debug.log' ) ;
144+ return readLastLines ( debugLogPath , DEBUG_LOG_MAX_LINES ) ;
145+ }
146+
147+ function readPm2Logs ( siteId : string ) : { stdout ?: string [ ] ; stderr ?: string [ ] } {
148+ const logsDir = nodePath . join ( PM2_HOME , 'logs' ) ;
149+ const stdoutPath = nodePath . join ( logsDir , `studio-site-${ siteId } -out.log` ) ;
150+ const stderrPath = nodePath . join ( logsDir , `studio-site-${ siteId } -error.log` ) ;
151+
152+ return {
153+ stdout : readLastLines ( stdoutPath , DEBUG_LOG_MAX_LINES ) ,
154+ stderr : readLastLines ( stderrPath , DEBUG_LOG_MAX_LINES ) ,
155+ } ;
156+ }
157+
125158function mergeSiteDetailsWithRunningDetails ( sites : SiteDetails [ ] ) : SiteDetails [ ] {
126159 return sites . map ( ( site ) => {
127160 const server = SiteServer . get ( site . id ) ;
@@ -221,12 +254,14 @@ export async function createSite(
221254 wpVersion,
222255 customDomain,
223256 enableHttps,
224- siteId,
257+ siteId : providedSiteId ,
225258 blueprint,
226259 phpVersion,
227260 noStart = false ,
228261 } = config ;
229262
263+ const siteId = providedSiteId || crypto . randomUUID ( ) ;
264+
230265 const metric = getBlueprintMetric ( blueprint ?. slug ) ;
231266 bumpStat ( StatsGroup . STUDIO_SITE_CREATE , metric ) ;
232267
@@ -273,6 +308,19 @@ export async function createSite(
273308 contexts . startup = cliError . cliArgs ;
274309 }
275310
311+ const debugLog = readWordPressDebugLog ( path ) ;
312+ if ( debugLog && debugLog . length > 0 ) {
313+ contexts . debugLog = { entries : debugLog } ;
314+ }
315+
316+ const pm2Logs = readPm2Logs ( siteId ) ;
317+ if ( pm2Logs . stdout && pm2Logs . stdout . length > 0 ) {
318+ contexts . playgroundLogs = { entries : pm2Logs . stdout } ;
319+ }
320+ if ( pm2Logs . stderr && pm2Logs . stderr . length > 0 ) {
321+ contexts . playgroundErrors = { entries : pm2Logs . stderr } ;
322+ }
323+
276324 Sentry . captureException ( error , {
277325 tags : {
278326 provider : 'cli' ,
@@ -388,6 +436,19 @@ export async function startServer( event: IpcMainInvokeEvent, id: string ): Prom
388436 contexts . startup = cliError . cliArgs ;
389437 }
390438
439+ const debugLog = readWordPressDebugLog ( server . details . path ) ;
440+ if ( debugLog && debugLog . length > 0 ) {
441+ contexts . debugLog = { entries : debugLog } ;
442+ }
443+
444+ const pm2Logs = readPm2Logs ( id ) ;
445+ if ( pm2Logs . stdout && pm2Logs . stdout . length > 0 ) {
446+ contexts . playgroundLogs = { entries : pm2Logs . stdout } ;
447+ }
448+ if ( pm2Logs . stderr && pm2Logs . stderr . length > 0 ) {
449+ contexts . playgroundErrors = { entries : pm2Logs . stderr } ;
450+ }
451+
391452 Sentry . captureException ( error , {
392453 tags : {
393454 provider : 'cli' ,
0 commit comments