refactor: replace console.log with structured logger calls
Replace raw `console.log`/`console.error` calls across CLI, API handlers, and module files with structured logger functions (`step`, `done`, `warn`, `fail`) from the shared logger library. This improves log consistency, readability, and makes it easier to control output formatting and log levels from a single place.
This commit is contained in:
+24
-24
@@ -4,6 +4,7 @@
|
||||
*/
|
||||
|
||||
import { discoverModules, registerExternalModules, startModuleCronJobs, stopModuleCronJobs } from '../../core/modules/index.js';
|
||||
import { step, done, warn, fail } from './logger.js';
|
||||
|
||||
// Use globalThis to persist initialization flag across module reloads
|
||||
const ZEN_INIT_KEY = Symbol.for('__ZEN_INITIALIZED__');
|
||||
@@ -11,10 +12,10 @@ const ZEN_INIT_KEY = Symbol.for('__ZEN_INITIALIZED__');
|
||||
/**
|
||||
* Initialize ZEN system
|
||||
* Discovers modules dynamically and starts cron jobs
|
||||
*
|
||||
*
|
||||
* Recommended: Use instrumentation.js for automatic initialization
|
||||
* Alternative: Call this function manually in your root layout
|
||||
*
|
||||
*
|
||||
* @example
|
||||
* // instrumentation.js (Recommended) — internal modules only
|
||||
* export async function register() {
|
||||
@@ -42,26 +43,26 @@ const ZEN_INIT_KEY = Symbol.for('__ZEN_INITIALIZED__');
|
||||
*/
|
||||
export async function initializeZen(config = {}) {
|
||||
const { modules: externalModules = [], skipCron = false, skipDb = true } = config;
|
||||
|
||||
|
||||
// Only run on server-side
|
||||
if (typeof window !== 'undefined') {
|
||||
return { skipped: true, reason: 'client-side' };
|
||||
}
|
||||
|
||||
|
||||
// Prevent multiple initializations using globalThis
|
||||
if (globalThis[ZEN_INIT_KEY]) {
|
||||
console.log('⚠ ZEN: Already initialized, skipping...');
|
||||
warn('ZEN: already initialized, skipping');
|
||||
return { skipped: true, reason: 'already-initialized' };
|
||||
}
|
||||
|
||||
|
||||
globalThis[ZEN_INIT_KEY] = true;
|
||||
console.log('🚀 ZEN: Starting initialization...');
|
||||
|
||||
step('ZEN starting...');
|
||||
|
||||
const result = {
|
||||
discovery: null,
|
||||
cron: { started: [], errors: [] }
|
||||
};
|
||||
|
||||
|
||||
try {
|
||||
// Step 1: Discover and register internal modules (from modules.registry.js)
|
||||
result.discovery = await discoverModules();
|
||||
@@ -70,10 +71,10 @@ export async function initializeZen(config = {}) {
|
||||
const skippedCount = result.discovery.skipped?.length || 0;
|
||||
|
||||
if (enabledCount > 0) {
|
||||
console.log(`✓ ZEN: Discovered ${enabledCount} internal module(s): ${result.discovery.enabled.join(', ')}`);
|
||||
done(`ZEN: ${enabledCount} module(s): ${result.discovery.enabled.join(', ')}`);
|
||||
}
|
||||
if (skippedCount > 0) {
|
||||
console.log(`⚠ ZEN: Skipped ${skippedCount} disabled module(s): ${result.discovery.skipped.join(', ')}`);
|
||||
warn(`ZEN: skipped ${skippedCount} module(s): ${result.discovery.skipped.join(', ')}`);
|
||||
}
|
||||
|
||||
// Step 2: Register external modules from zen.config.js (if any)
|
||||
@@ -81,26 +82,26 @@ export async function initializeZen(config = {}) {
|
||||
result.external = await registerExternalModules(externalModules);
|
||||
|
||||
if (result.external.registered.length > 0) {
|
||||
console.log(`✓ ZEN: Registered ${result.external.registered.length} external module(s): ${result.external.registered.join(', ')}`);
|
||||
done(`ZEN: ${result.external.registered.length} external module(s): ${result.external.registered.join(', ')}`);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Step 3: Start cron jobs for all enabled modules (internal + external)
|
||||
if (!skipCron) {
|
||||
result.cron = await startModuleCronJobs();
|
||||
|
||||
|
||||
if (result.cron.started.length > 0) {
|
||||
console.log(`✓ ZEN: Started ${result.cron.started.length} cron job(s): ${result.cron.started.join(', ')}`);
|
||||
done(`ZEN: ${result.cron.started.length} cron job(s): ${result.cron.started.join(', ')}`);
|
||||
}
|
||||
}
|
||||
|
||||
console.log('✓ ZEN: Initialization complete');
|
||||
|
||||
|
||||
done('ZEN: ready');
|
||||
|
||||
} catch (error) {
|
||||
console.error('✗ ZEN: Initialization failed:', error);
|
||||
fail(`ZEN: init failed: ${error.message}`);
|
||||
result.error = error.message;
|
||||
}
|
||||
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -110,14 +111,13 @@ export async function initializeZen(config = {}) {
|
||||
*/
|
||||
export function resetZenInitialization() {
|
||||
globalThis[ZEN_INIT_KEY] = false;
|
||||
|
||||
|
||||
// Stop all cron jobs using the module system
|
||||
try {
|
||||
stopModuleCronJobs();
|
||||
} catch (e) {
|
||||
// Cron system not available
|
||||
}
|
||||
|
||||
console.log('⚠ ZEN: Initialization flag reset');
|
||||
}
|
||||
|
||||
warn('ZEN: initialization reset');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user