Hono Logger ("hono/logger") Disable Coloring
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.
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)?
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.envobject. Could you enable"nodejs_compat"in yourwrangler.tomland addprocess.env['NO_COLOR'] = 'asdf'to yourindex.tsfile (instead of changing the logger middleware to check forc.env.NO_COLOR)?
Wow, interesting idea! This is okay, and we are working on another solution.
Thanks for implementing this. This is a huge saver, especially when logging to files and not having to postprocess the logs with regex.
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.
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
processglobal with aprocess.envobject that might contain aNO_COLORproperty (if that environment variable has been set). - Deno has a
Deno.noColorboolean that reflects the NO_COLOR environment variable at program start. - CloudFlare Workers do not have a
processglobal (unless someone has added thenodejs_compat_v2compatibility flag to theirwrangler.tomlfile) 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
}
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
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?