Skip to content

logger: LOG_PRETTY=1 crashes on first log line when pino-pretty isn't installed #137

@CryptoJones

Description

@CryptoJones

Problem

app/config/logger.js tries to opt into pretty-printed logs when LOG_PRETTY=1, with a guard against pino-pretty not being installed:

let transport;
if (process.env.LOG_PRETTY === '1') {
    try {
        transport = {
            target: 'pino-pretty',
            options: { colorize: true, translateTime: 'SYS:standard' },
        };
    } catch (_) {
        // pino-pretty not installed — fall through to JSON output.
    }
}

The try/catch is a no-op. Constructing an object literal doesn't throw. pino-pretty is lazy-resolved by pino itself the first time the logger emits — usually during DB init seconds into startup. A deployment that sets LOG_PRETTY=1 without pino-pretty on disk (it's not in dependencies) crashes there with a confusing internal pino error. The "fall through to JSON output" the comment promised never executes.

Proposed fix

Add an explicit require.resolve('pino-pretty') inside the try block. require.resolve synchronously throws MODULE_NOT_FOUND when the module is missing, so the catch fires cleanly and transport stays undefined — pino then falls through to default JSON output, which is what an operator who asked for pretty logs but hasn't installed the optional dep should see.

Proudly Made in Nebraska. Go Big Red! 🌽 https://xkcd.com/2347/

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions