logging.onLog cannot be used without also writing every log to stderr
Summary
logging.onLog and the SDK's own stderr output are controlled by a single setting, so an application that supplies its own logger still gets every log line printed to the console. There is no configuration that delivers logs to onLog without also printing them.
Detail
In handleStagehandNotification:
if (LOG_LEVEL_PRIORITY[log.level] < LOG_LEVEL_PRIORITY[logging.level]) return;
process.stderr.write(renderStagehandLog(log, logging.format) + "\n");
if (!logging.onLog) return;
try {
const result = logging.onLog(log);
...The process.stderr.write is unconditional for any line that passes the level filter, and it runs before onLog. The only value that suppresses it is level: 'off', which returns before onLog is called — so silencing the console also silences the callback.
The practical effect is that onLog can only ever duplicate console output, never replace it. An application that forwards logs into its own structured logger ends up emitting each line twice, once in its own format and once as [stagehand] ... on stderr.
Why it matters
We run Stagehand inside a long-lived server with its own structured logging, where stdout/stderr is an operational channel rather than a debugging one. Routine SDK output at ERROR — frame-detachment messages during snapshots of pages with third-party iframes, for example — is indistinguishable from application errors in aggregated logs, and we have no way to route it.
Suggested fix
Decouple delivery from printing. Any of these would work:
- Print only when no
onLogis supplied — treating the callback as taking ownership of output. - Add
logging.console: boolean(defaulttrue) independent oflevel. - Accept
'off'forlogging.format, meaning deliver but do not render.
Option 2 is the most explicit and the least likely to surprise existing users.
Versions checked
4.0.2, 4.1.0, and 4.2.0-alpha-fbcdf61 — handleStagehandNotification is identical in all three.
Source: browserbase/stagehand