Hello. Now we will ask a very trivial question: how does the output to the console work? I now use nodejs as an example.

A simple piece of code:

console.log(1); console.log(2); console.error(3); console.log(4);

And now attention: If to launch through the terminal (in Linux) node test.js will display everything in order: 1 2 3 4

But if you run in any environment that can distinguish an error from info ... such as a storm or ci ...

error output in the storm

That will be anything, 3 1 2 4, 1 3 2 4 or 1 2 4 3. In the mood of the figure 3 will be anywhere in the log.

And initially, why I had this question, this is ci:

error output in ci

Errors are mixed with logs.

How it works? The answer suggests this: they say there is a message queue, and the errors are not in the queue. Provided that the terminal can distinguish the error from the message.

And now I want proofs. Why did they do that? Is that really the case? what is optimized? Why not in order ... Or is nodejs still to blame?

    1 answer 1

    These two output methods are written to different output streams .

    console.log writes to stdout (file descriptor 1).
    console.error writes to stderr (file descriptor 2).

    If some handler of these two threads plans to output them combined (in your cases, exactly like this), then the result will depend on the combination method. This is already in the area of ​​responsibility not of Node.js, but of the process that launches it. Shell, CI runner or something else.

    You can accurately expect that the order within each individual stream, the order of the output lines will be preserved. For logs, streams are usually output to separate files.

    • Thank you for what you need! I can not raise a turnip, so just Thanks for the answer - Andrey Soroka
    • @Andrey Soroka, you can tick the answer if it answers your question. Thank! - D-side