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

Hono Logger ("hono/logger") Disable Coloring #3751

Open
kingmesal opened this issue Dec 15, 2024 · 4 comments · May be fixed by #3752
Open

Hono Logger ("hono/logger") Disable Coloring #3751

kingmesal opened this issue Dec 15, 2024 · 4 comments · May be fixed by #3752
Labels
enhancement New feature or request.

Comments

@kingmesal
Copy link

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 kingmesal added the enhancement New feature or request. label Dec 15, 2024
@kingmesal kingmesal changed the title Hono Logger ("hono/logger") Hono Logger ("hono/logger") Disable Coloring Dec 15, 2024
@EdamAme-x EdamAme-x linked a pull request Dec 16, 2024 that will close this issue
4 tasks
@StuartMoncrieff
Copy link

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)?

@yusukebe
Copy link
Member

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.

@albert-92
Copy link

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

@kingmesal
Copy link
Author

kingmesal commented Dec 18, 2024

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.

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

Successfully merging a pull request may close this issue.

4 participants