Skip to content

New lines in messageFormat are not considered #634

@greg-md

Description

@greg-md

I am trying to provide a custom messageFormat option to display the error stack from a new line, but the new lines are replaced with a single space.

Options:

options: {
  ignore: 'pid,hostname,context,err',
  messageFormat: `[{context}] {msg}{if err.stack}\n{err.stack}{end}`,
},

Example:

const myError = new Error('Something went wrong');
logger.error(`An error occurred: ${myError.message}`, myError.stack);

Output:

[18:31:29.781] ERROR: [Test] An error occurred: Something went wrong Error: Something went wrong
    at bootstrap (/dist/apps/test/webpack:/node-trading-robot/apps/test/src/main.ts:26:19)
    at processTicksAndRejections (node:internal/process/task_queues:105:5)

Expected output:

[18:31:29.781] ERROR: [Test] An error occurred: Something went wrong
Error: Something went wrong
    at bootstrap (/dist/apps/test/webpack:/node-trading-robot/apps/test/src/main.ts:26:19)
    at processTicksAndRejections (node:internal/process/task_queues:105:5)

Seems like new lines are replaced on purpose, but is there a way to allow them?

PS: If I put the new line in the log message itself, it works as expected.

Example:

const myError = new Error('Something went wrong');
logger.error(`An error occurred: ${myError.message}\n`, myError.stack);

Output:

[18:31:29.781] ERROR: [Test] An error occurred: Something went wrong
Error: Something went wrong
    at bootstrap (/dist/apps/test/webpack:/node-trading-robot/apps/test/src/main.ts:26:19)
    at processTicksAndRejections (node:internal/process/task_queues:105:5)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions