hono icon indicating copy to clipboard operation
hono copied to clipboard

Hono Logger ("hono/logger") Disable Coloring

Open kingmesal opened this issue 1 year ago • 6 comments

What is the feature you are proposing?

Currently the color status coding is set with this: const isNoColor = typeof Deno?.noColor === "boolean" ? Deno.noColor : process !== void 0 ? "NO_COLOR" in process?.env : false;

Can something be added to check it this is running in a cloudflare worker to disable coloring?

My logs spit out [32m200[0m 923ms, which is terrible.

The c.env could be passed in and if NO_COLOR === true then we are set.

kingmesal avatar Dec 15 '24 21:12 kingmesal

I was going to raise a feature request related to disabling status code coloring too!

The Debug Console in VS Code does not support the ANSI escape codes that are used to add the colors, so the output looks like --> GET / \x1b[32m200\x1b[0m 7ms.

Looking through the source code, I found the same thing that @kingmesal did: to disable status code coloring, you can set a "NO_COLOR" environment variable, which is described at https://no-color.org/.

The way to get coloring working in the VS Code Debug Console (rather than disabling it) was to add "outputCapture": "std" to my configuration in launch.json.

For @kingmesal's original problem...

I haven't written code to run on CloudFlare Workers before (so this might be a bad work-around), but I understand that they have an empty process.env object. Could you enable "nodejs_compat" in your wrangler.toml and add process.env['NO_COLOR'] = 'asdf' to your index.ts file (instead of changing the logger middleware to check for c.env.NO_COLOR)?

StuartMoncrieff avatar Dec 18 '24 02:12 StuartMoncrieff

I haven't written code to run on CloudFlare Workers before (so this might be a bad work-around), but I understand that they have an empty process.env object. Could you enable "nodejs_compat" in your wrangler.toml and add process.env['NO_COLOR'] = 'asdf' to your index.ts file (instead of changing the logger middleware to check for c.env.NO_COLOR)?

Wow, interesting idea! This is okay, and we are working on another solution.

yusukebe avatar Dec 18 '24 06:12 yusukebe

Thanks for implementing this. This is a huge saver, especially when logging to files and not having to postprocess the logs with regex.

albert-92 avatar Dec 18 '24 11:12 albert-92

Am I reading the getColorEnabled() method correctly that is process === undefined then coloring is disabled?

If so, that in and of itself solves turning coloring off in Cloudflare, right.

Otherwise, if not, having to turn on nodejs_compat to set an env is kind of a terribly obscure approach.

kingmesal avatar Dec 18 '24 14:12 kingmesal

I guess that neither of us checked what the default logger output looked like on CloudFlare Workers before commenting.

Your reading of getColorEnabled() matches mine:

  • Node.js and Bun have a process global with a process.env object that might contain a NO_COLOR property (if that environment variable has been set).
  • Deno has a Deno.noColor boolean that reflects the NO_COLOR environment variable at program start.
  • CloudFlare Workers do not have a process global (unless someone has added the nodejs_compat_v2 compatibility flag to their wrangler.toml file) so they will default to logger output with no coloring (whereas Node/Deno/Bun will default to colored log output).
// from /src/utils/color.ts
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
}

StuartMoncrieff avatar Dec 20 '24 04:12 StuartMoncrieff

Cloudflare Workers now have access to environment variables via process.env. If we add nodejs_compat to compatibility flags, NO_COLOR flag will work. https://developers.cloudflare.com/workers/runtime-apis/nodejs/process/#processenv

Also, since env on Cloudflare can now be imported in global, it may be possible to implement it without compat flags. I'll check it later. https://developers.cloudflare.com/workers/runtime-apis/bindings/#importing-env-as-a-global

ryuapp avatar Apr 20 '25 03:04 ryuapp

Hi!

Thanks to @ryuapp, the logger will disable coloring if you set NO_COLOR in Cloudflare Workers environment variables since version `4.8.0.

@kingmesal Is this an expected feature? If so, can you try it?

yusukebe avatar Jun 26 '25 08:06 yusukebe