Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
gpanders opened this issue Jan 7, 2025 · 1 comment
Open

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

gpanders opened this issue Jan 7, 2025 · 1 comment
Labels

Comments

@gpanders
Copy link

gpanders commented Jan 7, 2025

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

@gpanders gpanders added the triage label Jan 7, 2025
@yusukebe
Copy link
Member

yusukebe commented Jan 9, 2025

HI @gpanders

Thank you for raising the issue. There is a similar issue #3751. We have the hacky way, but using checking TTY is a good idea.

Hey @ryuapp! What do you think of checking TTY for the logger?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants