Skip to content

Default logger middleware doesn't check that stdout is a TTY #3809

@gpanders

Description

@gpanders

What version of Hono are you using?

4.6.16

What runtime/platform is your app running on? (with version if possible)

Cloudflare Workers

What steps can reproduce the bug?

Use the default logger middleware provided by Hono https://hono.dev/docs/middleware/builtin/logger

Observe that emitted logs contain ANSI escape sequences when the output is not a TTY:

image

This screenshot is taken from Cloudflare Workers logs, but the same would be true for any log output which is not a terminal.

What is the expected behavior?

Hono should check that stdout is a TTY when determining whether or not to emit ANSI escape sequences for colors:

hono/src/utils/color.ts

Lines 13 to 26 in 2ead4d8

export function getColorEnabled(): boolean {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const { process, Deno } = globalThis as any
const isNoColor =
typeof Deno?.noColor === 'boolean'
? (Deno.noColor as boolean)
: process !== undefined
? // eslint-disable-next-line no-unsafe-optional-chaining
'NO_COLOR' in process?.env
: false
return !isNoColor
}

In Node this can be done with Boolean(process.stdout.isTTY) === true https://nodejs.org/api/tty.html#tty

Deno has Deno.stdout.isTerminal() https://docs.deno.com/api/deno/~/Deno.stdout.isTerminal

What do you see instead?

No response

Additional information

A workaround in the meantime is to set NO_COLOR manually if stdout is not a TTY:

if (Boolean(process.stdout.isTTY) === false) {
    process.env["NO_COLOR"] = "1"
}

Note however that even this will not work in Cloudflare Workers because of another issue with the process polyfill: unjs/unenv#380

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions