Skip to content

Commit e49ad41

Browse files
logger
1 parent 52fe8fc commit e49ad41

File tree

8 files changed

+134
-38
lines changed

8 files changed

+134
-38
lines changed

packages/cli/src/commands/bundle/bundler.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,11 @@ export function generatePlatformWrapper(
823823

824824
return `export default async function(context = {}) {
825825
const config = ${configObject};${codeSection}
826+
// Apply context overrides (e.g., logger config from CLI)
827+
if (context.logger) {
828+
config.logger = { ...config.logger, ...context.logger };
829+
}
830+
826831
return await startFlow(config);
827832
}`;
828833
}

packages/cli/src/commands/push/index.ts

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import path from 'path';
22
import { JSDOM, VirtualConsole } from 'jsdom';
33
import fs from 'fs-extra';
4-
import { getPlatform, type Elb } from '@walkeros/core';
4+
import {
5+
getPlatform,
6+
type Elb,
7+
type Logger as CoreLogger,
8+
} from '@walkeros/core';
59
import { schemas } from '@walkeros/core/dev';
610
import {
711
createCommandLogger,
12+
createCollectorLoggerConfig,
813
getErrorMessage,
914
detectInput,
1015
type Logger,
@@ -77,6 +82,10 @@ export async function pushCommand(options: PushCommandOptions): Promise<void> {
7782
);
7883
} else {
7984
// Bundle flow: execute directly
85+
const collectorLoggerConfig = createCollectorLoggerConfig(
86+
logger,
87+
options.verbose,
88+
);
8089
result = await executeBundlePush(
8190
detected.content,
8291
detected.platform!,
@@ -85,6 +94,7 @@ export async function pushCommand(options: PushCommandOptions): Promise<void> {
8594
(dir) => {
8695
tempDir = dir;
8796
},
97+
{ logger: collectorLoggerConfig },
8898
);
8999
}
90100

@@ -201,7 +211,13 @@ async function executeConfigPush(
201211
return executeWebPush(tempPath, validatedEvent, logger);
202212
} else if (platform === 'server') {
203213
logger.debug('Executing in server environment (Node.js)');
204-
return executeServerPush(tempPath, validatedEvent, logger);
214+
const collectorLoggerConfig = createCollectorLoggerConfig(
215+
logger,
216+
options.verbose,
217+
);
218+
return executeServerPush(tempPath, validatedEvent, logger, 60000, {
219+
logger: collectorLoggerConfig,
220+
});
205221
} else {
206222
throw new Error(`Unsupported platform: ${platform}`);
207223
}
@@ -216,6 +232,7 @@ async function executeBundlePush(
216232
validatedEvent: { name: string; data: Record<string, unknown> },
217233
logger: Logger,
218234
setTempDir: (dir: string) => void,
235+
context: { logger?: CoreLogger.Config } = {},
219236
): Promise<PushResult> {
220237
// Write bundle to temp file
221238
const tempDir = path.join(
@@ -239,7 +256,7 @@ async function executeBundlePush(
239256
return executeWebPush(tempPath, validatedEvent, logger);
240257
} else {
241258
logger.debug('Executing in server environment (Node.js)');
242-
return executeServerPush(tempPath, validatedEvent, logger);
259+
return executeServerPush(tempPath, validatedEvent, logger, 60000, context);
243260
}
244261
}
245262

@@ -280,23 +297,28 @@ async function executeWebPush(
280297
const bundleCode = await fs.readFile(bundlePath, 'utf8');
281298
window.eval(bundleCode);
282299

283-
// Wait for window.elb assignment
284-
logger.debug('Waiting for elb...');
300+
// Wait for window.collector assignment
301+
logger.debug('Waiting for collector...');
285302
await waitForWindowProperty(
286303
window as unknown as Record<string, unknown>,
287-
'elb',
304+
'collector',
288305
5000,
289306
);
290307

291308
const windowObj = window as unknown as Record<string, unknown>;
292-
const elb = windowObj.elb as unknown as (
293-
name: string,
294-
data: Record<string, unknown>,
295-
) => Promise<Elb.PushResult>;
309+
const collector = windowObj.collector as unknown as {
310+
push: (event: {
311+
name: string;
312+
data: Record<string, unknown>;
313+
}) => Promise<Elb.PushResult>;
314+
};
296315

297-
// Push event
316+
// Push event directly to collector (bypasses source handlers)
298317
logger.log(`Pushing event: ${event.name}`);
299-
const elbResult = await elb(event.name, event.data);
318+
const elbResult = await collector.push({
319+
name: event.name,
320+
data: event.data,
321+
});
300322

301323
return {
302324
success: true,
@@ -320,6 +342,7 @@ async function executeServerPush(
320342
event: PushEventInput,
321343
logger: Logger,
322344
timeout: number = 60000, // 60 second default timeout
345+
context: { logger?: CoreLogger.Config } = {},
323346
): Promise<PushResult> {
324347
const startTime = Date.now();
325348

@@ -342,26 +365,28 @@ async function executeServerPush(
342365
throw new Error('Bundle does not export default factory function');
343366
}
344367

345-
// Call factory function to start flow
368+
// Call factory function to start flow (pass context for verbose logging)
346369
logger.debug('Calling factory function...');
347-
const result = await flowModule.default();
370+
const result = await flowModule.default(context);
348371

349-
if (!result || !result.elb || typeof result.elb !== 'function') {
372+
if (
373+
!result ||
374+
!result.collector ||
375+
typeof result.collector.push !== 'function'
376+
) {
350377
throw new Error(
351-
'Factory function did not return valid result with elb',
378+
'Factory function did not return valid result with collector',
352379
);
353380
}
354381

355-
const { elb } = result;
382+
const { collector } = result;
356383

357-
// Push event
384+
// Push event directly to collector (bypasses source handlers)
358385
logger.log(`Pushing event: ${event.name}`);
359-
const elbResult = await (
360-
elb as (
361-
name: string,
362-
data: Record<string, unknown>,
363-
) => Promise<Elb.PushResult>
364-
)(event.name, event.data);
386+
const elbResult = await collector.push({
387+
name: event.name,
388+
data: event.data,
389+
});
365390

366391
return {
367392
success: true,

packages/cli/src/commands/run/execution.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { createLogger, Level } from '@walkeros/core';
1+
import { createLogger, Level, type Logger } from '@walkeros/core';
22
import type { RuntimeConfig, ServeConfig } from '../../runtime/index.js';
33
import { runFlow, runServeMode } from '../../runtime/index.js';
44

55
// Create logger for local execution - DEBUG level when VERBOSE, otherwise INFO
66
const logLevel = process.env.VERBOSE === 'true' ? Level.DEBUG : Level.INFO;
7-
const logger = createLogger({ level: logLevel });
7+
const loggerConfig: Logger.Config = { level: logLevel };
8+
const logger = createLogger(loggerConfig);
89

910
/**
1011
* Execute run command locally
@@ -32,7 +33,7 @@ export async function executeRunLocal(
3233
port: options.port,
3334
host: options.host,
3435
};
35-
await runFlow(flowPath, config, logger.scope('runner'));
36+
await runFlow(flowPath, config, logger.scope('runner'), loggerConfig);
3637
break;
3738
}
3839

packages/cli/src/commands/simulate/node-executor.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
import { pathToFileURL } from 'url';
8-
import type { Elb } from '@walkeros/core';
8+
import type { Elb, Logger as CoreLogger } from '@walkeros/core';
99
import { getErrorMessage } from '../../core/index.js';
1010
import type { CallTracker, ApiCall } from './tracker.js';
1111

@@ -83,6 +83,7 @@ export async function executeInNode(
8383
tracker: CallTracker,
8484
envs: Record<string, DestinationEnv>,
8585
timeout: number = 30000,
86+
context: { logger?: CoreLogger.Config } = {},
8687
): Promise<ExecutionResult> {
8788
const start = Date.now();
8889

@@ -103,21 +104,27 @@ export async function executeInNode(
103104
throw new Error('Bundle does not export default factory function');
104105
}
105106

106-
const result = await module.default();
107+
const result = await module.default(context);
107108

108-
if (!result || !result.elb || typeof result.elb !== 'function') {
109+
if (
110+
!result ||
111+
!result.collector ||
112+
typeof result.collector.push !== 'function'
113+
) {
109114
throw new Error(
110-
'Factory function did not return valid result with elb',
115+
'Factory function did not return valid result with collector',
111116
);
112117
}
113118

114119
const { collector, elb } = result;
115120

116121
let elbResult: Elb.PushResult | undefined;
117122
try {
118-
elbResult = (await elb(event.name, event.data)) as
119-
| Elb.PushResult
120-
| undefined;
123+
// Use collector.push directly (bypasses source handlers, same as push command)
124+
elbResult = await collector.push({
125+
name: event.name,
126+
data: event.data,
127+
});
121128
} catch (error) {
122129
throw new Error(`Event execution failed: ${getErrorMessage(error)}`);
123130
}

packages/cli/src/commands/simulate/simulator.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import path from 'path';
22
import fs from 'fs-extra';
3-
import type { Flow } from '@walkeros/core';
3+
import type { Flow, Logger as CoreLogger } from '@walkeros/core';
44
import { getPlatform } from '@walkeros/core';
55
import {
66
createLogger,
7+
createCollectorLoggerConfig,
78
getErrorMessage,
89
detectInput,
10+
type Logger,
911
type Platform,
1012
} from '../../core/index.js';
1113
import {
@@ -48,7 +50,10 @@ export async function simulateCore(
4850
try {
4951
// Execute simulation
5052
logger.debug(`Simulating event: ${JSON.stringify(event)}`);
51-
const result = await executeSimulation(event, inputPath, options.platform);
53+
const result = await executeSimulation(event, inputPath, options.platform, {
54+
logger,
55+
verbose: options.verbose,
56+
});
5257

5358
return result;
5459
} catch (error) {
@@ -92,10 +97,16 @@ export async function executeSimulation(
9297
event: unknown,
9398
inputPath: string,
9499
platformOverride?: Platform,
100+
options: { logger?: Logger; verbose?: boolean } = {},
95101
): Promise<SimulationResult> {
96102
const startTime = Date.now();
97103
const tempDir = getTmpPath();
98104

105+
// Create collector logger config for forwarding logs
106+
const collectorLoggerConfig = options.logger
107+
? createCollectorLoggerConfig(options.logger, options.verbose)
108+
: undefined;
109+
99110
try {
100111
// Ensure temp directory exists
101112
await fs.ensureDir(tempDir);
@@ -124,6 +135,7 @@ export async function executeSimulation(
124135
typedEvent,
125136
tempDir,
126137
startTime,
138+
collectorLoggerConfig,
127139
);
128140
} else {
129141
// Bundle flow: execute directly without mocking
@@ -133,6 +145,7 @@ export async function executeSimulation(
133145
typedEvent,
134146
tempDir,
135147
startTime,
148+
collectorLoggerConfig,
136149
);
137150
}
138151
} catch (error) {
@@ -161,6 +174,7 @@ async function executeConfigSimulation(
161174
typedEvent: { name: string; data?: unknown },
162175
tempDir: string,
163176
startTime: number,
177+
loggerConfig?: CoreLogger.Config,
164178
): Promise<SimulationResult> {
165179
// Load config
166180
const { flowConfig, buildOptions } = await loadFlowConfig(configPath);
@@ -230,6 +244,7 @@ async function executeConfigSimulation(
230244
tracker,
231245
envs,
232246
30000,
247+
loggerConfig ? { logger: loggerConfig } : {},
233248
);
234249
}
235250

@@ -253,6 +268,7 @@ async function executeBundleSimulation(
253268
typedEvent: { name: string; data?: unknown },
254269
tempDir: string,
255270
startTime: number,
271+
loggerConfig?: CoreLogger.Config,
256272
): Promise<SimulationResult> {
257273
// Write bundle to temp file
258274
const tempOutput = path.join(
@@ -283,6 +299,7 @@ async function executeBundleSimulation(
283299
tracker,
284300
{},
285301
30000,
302+
loggerConfig ? { logger: loggerConfig } : {},
286303
);
287304
}
288305

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import type { Logger as CoreLogger } from '@walkeros/core';
2+
import type { Logger } from './logger.js';
3+
4+
/**
5+
* Create collector logger config that forwards to CLI logger
6+
*
7+
* This bridges the CLI logger with the collector's internal logger,
8+
* allowing collector logs (from destinations, sources, etc.) to be
9+
* displayed through the CLI's output system.
10+
*
11+
* @param cliLogger - The CLI logger instance
12+
* @param verbose - Whether verbose mode is enabled
13+
* @returns Logger config for the collector
14+
*/
15+
export function createCollectorLoggerConfig(
16+
cliLogger: Logger,
17+
verbose?: boolean,
18+
): CoreLogger.Config {
19+
return {
20+
level: verbose ? 'DEBUG' : 'ERROR',
21+
handler: (level, message, context, scope) => {
22+
const scopePath = scope.length > 0 ? `[${scope.join(':')}] ` : '';
23+
const hasContext = Object.keys(context).length > 0;
24+
const contextStr = hasContext ? ` ${JSON.stringify(context)}` : '';
25+
26+
if (level === 0) {
27+
// ERROR - always show
28+
cliLogger.error(`${scopePath}${message}${contextStr}`);
29+
} else if (verbose) {
30+
// INFO or DEBUG - only show if verbose
31+
cliLogger.debug(`${scopePath}${message}${contextStr}`);
32+
}
33+
},
34+
};
35+
}

packages/cli/src/core/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from './logger.js';
2+
export * from './collector-logger.js';
23
export * from './timer.js';
34
export * from './output.js';
45
export * from './tmp.js';

0 commit comments

Comments
 (0)