skip to content

Logging from Code

What a script prints and where it actually goes: not a stream but a message that leaves the sandbox, so what you read back is a rendering of your value rather than the value itself.

part ofAPI & DB clientsoverview, primer and where to startread it →
on this pageshow

explore

questions

4

When a Postman sandbox script calls console.log, where does that output actually go?

level: juniorimportance: must knowfreq 68%

answer

  1. Not a stream at all
  2. The sandbox owns no file descriptors
  3. A message leaves the sandbox outward
  4. An execution.console event, with the cursor

basics

~20 s

Nowhere on its own. The sandbox's console dispatches an execution.console event carrying the cursor, the level and the serialised arguments out to whatever host runs the script, and the host decides whether to display it.

solid answer

~40 s

Postman's sandbox `console` is built by `PostmanConsole`, and it never writes to a stream. Each call — `log`, `warn`, `debug`, `info`, `error`, `clear` — **dispatches an `execution.console` event** carrying the execution cursor, the level, and the serialised arguments across the sandbox boundary. Whatever embedded the sandbox receives that event and decides what to do with it: render it, forward it, or drop it. A host may not have a terminal at all. So `console.log` is best read as *emitting a message about the run*, not as *printing*: nothing in the script blocks on it, nothing reads it back, and its visibility is the host's choice rather than the script's.

code

javascript · 4 lines
javascript
console.log('starting item');
console.warn('retrying', 2);
console.error('bad payload', { id: 7 });
console.clear();

go deeper

for a junior

Be ready to say plainly that a Postman script has no stdout: the console call sends a message out of the sandbox and something else decides whether to show it.

for a middle

Explain the mechanism: PostmanConsole dispatches an execution.console event carrying the cursor, the level and the serialised arguments, and the level is a field rather than a destination.

for a senior

Show that you design for it — messages that are self-describing because you do not control the surrounding context, and nothing in the run depending on a log line being seen.

for a principal

Own the consequence for a team: logging is an export out of the sandbox and a courtesy to a reader, so it can never be the place where a run's decisions or values live.

## The mechanism: a dispatch, not a write Postman scripts run inside a **sandbox** — an isolated JavaScript execution context created for the run. That context owns no file descriptors: no stdout, no stderr, no log file. The `console` object a script can see is therefore not the host's console. It is built by `PostmanConsole`, and every member it exposes does the same single thing: it **dispatches an `execution.console` event** across the sandbox boundary. So `console.log('hello')` does not print. It assembles a message that says *this execution, at this level, produced these values*, and hands that message outward. Whatever embedded the sandbox is on the other side of the boundary, and what happens next is entirely that host's business. ## What travels in the message Three things ride along with each dispatched `execution.console` event: - the **cursor** — the execution's own position, which tells a receiver *which* run, iteration and item produced the message, so that interleaved output can be attributed rather than guessed at; - the **level** — `log`, `warn`, `debug`, `info` or `error`, recorded as a **field on the event** rather than as a choice of destination; - the **serialised arguments** — your values, converted into a form that can cross the boundary. `clear` fits the same shape. It is a console event whose meaning is *discard what you are showing*, addressed to the receiver; the sandbox has nothing of its own to clear. ## Stream write versus event dispatch | | writing to a stream | dispatching a console event | |---|---|---| | destination | a descriptor the process owns | a listener outside the sandbox | | what is sent | bytes | cursor, level, serialised args | | role of the level | picks stdout or stderr | one field on a single event | | who decides visibility | whoever redirects the stream | whatever embeds the sandbox | | if nobody listens | the bytes still land somewhere | the message is simply not shown | | back channel | the process can read what it wrote | none; nothing returns to the script | ## Why the distinction is practical, not pedantic Four consequences follow directly from "message, not stream", and each of them surprises somebody at least once: 1. **Visibility is not yours to guarantee.** The host may render the message in a console pane, forward it somewhere, or drop it. A host with no display attached is a perfectly legitimate receiver; your call still succeeds and still produces nothing anybody sees. 2. **There is no back channel.** The call returns immediately, tells you nothing about who received the message, and raises nothing if nobody did. A script cannot read what it logged, so control flow can never legitimately depend on log output. 3. **Values are snapshots, not references.** The arguments are serialised at the moment of the call. What a reader eventually sees is a rendering of that serialisation — not a live window onto an object that your script may since have changed. 4. **Everything logged leaves the sandbox.** The serialised arguments cross the boundary to whoever is on the other side. That is fine for a request id and considerably less fine for a credential. ## How to think about it while writing scripts Treat `console.log` as **emitting a diagnostic about the run for a human who may or may not be looking**, in the same spirit as any structured logging call in application code — and specifically *not* as any of the following: a print statement, a value store, a return channel, or a way to make something happen. The level you choose is a label on the message that helps whoever is reading triage it; it is not a routing decision, because there is only ever one event type and one boundary. The useful habit that falls out of this is to make each message self-describing. Because the host decides how much context it shows around your line, a message that reads well on its own — naming the item, the value and why you cared — survives every host. A bare `console.log(x)` does not. ## Common misreadings - Believing the sandbox writes to the host process's stdout, and reasoning about redirection, buffering or pipes on that basis. None of those concepts apply to an event dispatch. - Assuming `console.error` reaches a different, more reliable place than `console.log`. Both dispatch the same event; only the recorded level differs. - Expecting a log line to be a durable record. Whether anything is retained is decided outside the sandbox entirely. - Using log output as a synchronisation or hand-off mechanism between parts of a run. There is no reader inside the sandbox to pick it up.

  • If nothing is listening for that event, what happens to the message?
    It is simply not shown. The dispatch still succeeds — the script does not block, does not learn who received it, and gets no error. A host with no console pane, or no terminal at all, is a legitimate receiver; the message just goes undisplayed. Visibility is the host's decision, never the script's.
  • Can a script tell whether its console message was displayed?
    No. The call returns immediately with nothing useful and there is no back channel into the sandbox. A script cannot read what it logged, cannot count receivers, and cannot branch on whether a message landed. Treat logging as a one-way diagnostic you hope somebody reads, never as a mechanism the run relies on.

It is closer to posting a postcard than to writing in your own notebook: the message leaves your hands, and whether anyone ever reads it is decided by whoever receives it.

saying these in an interview costs you the question

  • Says the sandbox console writes to the process's stdout
  • Assumes the script can read back what it logged
  • Thinks error output travels on a separate stream
  • Believes a logged line is guaranteed to be displayed
  • Treats console output as somewhere to store a value
open as a page

Which logging members does Postman's sandbox console expose, and what actually differs between them?

level: middleimportance: should knowfreq 44%

basics

~10 s

PostmanConsole builds six members: log, warn, debug, info, error and clear. The five logging ones differ only in the level field on the identical execution.console event they dispatch, not in destination, stream or transport.

open as a page

Why is what you read in Postman's console a rendering of your value rather than the value itself?

level: seniorimportance: should knowfreq 34%

basics

~20 s

Console arguments are serialised at the moment of the call and shipped out of the sandbox. The host renders that serialisation, so you read a snapshot of what crossed the boundary, never the live object itself.

open as a page

Why should a team never let a Postman run depend on console output being seen or acted on?

level: principalimportance: should knowfreq 28%

basics

~20 s

Logging is a message addressed outward, not a channel with delivery semantics. A console call dispatches an event and returns; the host may render it, drop it, or have no display, and the script is never told which.

open as a page