Skip to content

feat(logger): support for NO_COLOR on cloudflare workers #4094

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

Merged

Conversation

ryuapp
Copy link
Contributor

@ryuapp ryuapp commented Apr 20, 2025

closes #3751

Note

Currently, process.env is available on Cloudflare Workers with nodejs_compat flag, so there may not be a need to support it.
https://developers.cloudflare.com/workers/runtime-apis/nodejs/process/#processenv

Logger now uses getColorEnabledAsync() to support retrieving cloudflare env. Since we couldn't support this without dynamic import now, I created a new getColorEnabled() that is asynchronous.
getColorEnabled() is maintained for backwards compatibility for showRoute().

The cost of dynamic imports has not been investigated, so it may be necessary to optimize it with following PR:

The author should do the following, if applicable

  • Add tests
  • Run tests
  • bun run format:fix && bun run lint:fix to format the code
  • Add TSDoc/JSDoc to document the code

Copy link

codecov bot commented Apr 20, 2025

Codecov Report

Attention: Patch coverage is 92.85714% with 1 line in your changes missing coverage. Please review.

Project coverage is 91.36%. Comparing base (b96de9f) to head (b69cf0e).
Report is 9 commits behind head on next.

Files with missing lines Patch % Lines
src/utils/color.ts 87.50% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             next    #4094      +/-   ##
==========================================
- Coverage   91.47%   91.36%   -0.12%     
==========================================
  Files         168      168              
  Lines       10791    10753      -38     
  Branches     3088     3107      +19     
==========================================
- Hits         9871     9824      -47     
- Misses        919      928       +9     
  Partials        1        1              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ryuapp
Copy link
Contributor Author

ryuapp commented Apr 20, 2025

I also have another plan, #4095.
JSR validation error is related to denoland/deno#28922.

@yusukebe
Copy link
Member

Hi @ryuapp

Thank you for the PR. I think this code is doing a little bit tricky things: dynamic import and calling env. So it's better to write runtime tests. I've created the PR to update wrangler: #4096. Can you review it first?

@ryuapp ryuapp marked this pull request as draft April 21, 2025 15:55
@yusukebe
Copy link
Member

@ryuapp

If it's ready to review, please ping me:)

@ryuapp ryuapp force-pushed the support-for-no_color-on-cloudflare-workers branch from 9d25f4c to d036a85 Compare April 26, 2025 07:43
@ryuapp ryuapp marked this pull request as ready for review April 26, 2025 08:09
Old Cloudflare workers are missing import('cloudflare:workers')
@ryuapp
Copy link
Contributor Author

ryuapp commented Apr 26, 2025

@yusukebe This is now ready for review.

@ryuapp ryuapp force-pushed the support-for-no_color-on-cloudflare-workers branch from ab269b1 to 4a2e1c1 Compare April 27, 2025 12:48
Copy link
Member

@yusukebe yusukebe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@yusukebe
Copy link
Member

Thanks @ryuapp !

I'll merge this later and include the next minor release (maybe shipped soon).

@yusukebe yusukebe added the v4.8 label Apr 28, 2025
@yusukebe yusukebe changed the base branch from main to next June 16, 2025 05:05
@yusukebe yusukebe merged commit 4b762e1 into honojs:next Jun 16, 2025
16 of 17 checks passed
@ryuapp ryuapp deleted the support-for-no_color-on-cloudflare-workers branch June 16, 2025 07:05
navigator !== undefined && navigator.userAgent === 'Cloudflare-Workers'
? // eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
'NO_COLOR' in ((await import('cloudflare:workers')).env ?? {}) // ?? {} is for backward compat
Copy link

@ysm-dev ysm-dev Jun 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got this error from this line:

image

environment: monorepo + nextjs + hono (v4.8.0)

cc @yusukebe

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ysm-dev Can you create an issue?

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

Successfully merging this pull request may close these issues.

Hono Logger ("hono/logger") Disable Coloring
3 participants