fix(logger): use app.isPackaged for log dir detection and add logging docs

Previously getLogDir() only checked app.isReady(), which caused
development builds to write logs to the user data directory instead
of the local project logs/ folder. Now uses app.isPackaged to
correctly distinguish production from development environments.

Also adds comprehensive logging system documentation and a debug
utility for verifying Electron environment detection.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Misaka
2026-04-04 09:22:20 +08:00
parent ba436cf374
commit 24d9bfebaf
3 changed files with 788 additions and 4 deletions

View File

@@ -10,14 +10,19 @@ import { app } from 'electron'
/**
* Get log directory path
* Uses app.getPath('logs') in production, local logs dir in development
* Production = app.isPackaged === true
*/
export function getLogDir(): string {
if (app && app.isReady()) {
// Check if running in production (packed app)
// This must be checked BEFORE app.getPath('logs') because Electron
// always returns the user data logs path regardless of environment
if (app && app.isReady() && app.isPackaged) {
return app.getPath('logs')
}
// Fallback for development or before app is ready
// Note: synchronous FS calls are acceptable here because this branch only
// executes in dev environments when app is not yet ready (rare, at startup).
// Development environment: use logs directory in project root
// Note: synchronous FS calls are acceptable here because this branch
// executes in dev environments or before app is ready.
const devLogDir = path.join(process.cwd(), 'logs')
if (!fs.existsSync(devLogDir)) {
fs.mkdirSync(devLogDir, { recursive: true })

View File

@@ -0,0 +1,27 @@
/**
* Debug script to verify Electron environment detection
*/
import { app } from 'electron'
console.log('=== Electron Environment Debug ===\n')
console.log('1. app.isPackaged:', app.isPackaged)
console.log('2. app.getPath("userData"):', app.getPath('userData'))
console.log('3. app.getPath("logs"):', app.getPath('logs'))
console.log('4. NODE_ENV:', process.env.NODE_ENV)
console.log('5. process.cwd():', process.cwd())
console.log('6. __dirname:', __dirname)
// Predict log dir
function getLogDir(): string {
if (app && app.isReady()) {
return app.getPath('logs')
}
const devLogDir = `${process.cwd()}\\logs`
return devLogDir
}
console.log('\n7. Predicted log dir:', getLogDir())
console.log('\n=== END DEBUG ===')
app.quit()