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

Feature request: .runWithoutWrappers(fn) method #347

Open
1 task
antfu opened this issue Feb 4, 2025 · 3 comments
Open
1 task

Feature request: .runWithoutWrappers(fn) method #347

antfu opened this issue Feb 4, 2025 · 3 comments
Labels
enhancement New feature or request

Comments

@antfu
Copy link
Member

antfu commented Feb 4, 2025

Describe the feature

I'd like to introduce a consola.runWithoutWrappers(() => {...}) function for temporary escape the wrapper to print raw logs.

Currently we are using .restoreAll() and .wrapAll() to workaround that in Nuxt:

Which as one drawback is that the later .wrapAll() might not always expected, as if the user wants to completely disable the wrapper by calling .restoreAll() at the server start; But the later .wrapAll() might turn it back unexpectly.

Thus I am thinking to have a

await logger.runWithoutWrappers(() => {
  await vite.build(serverConfig)
})

to replace that, which either have a flag to bypass, or only restore if the the console has been wrapped.

The pesudo code I imagine might be:

  async runWithoutWrappers(fn: ()=> void | Promise<void>) {
    const isConsoleWrapped = this.isConsoleWrapped
    const isStdWrapped = this.isStdWrapped

    if (isConsoleWrapped)
      this.restoreConsole()
    if (isStdWrapped)
      this.restoreStd()

    try {
      await fn()
    }
    finally {
      if (isConsoleWrapped)
        this.wrapConsole()
      if (isStdWrapped)
        this.wrapStd()
    }
  }

Additional information

  • Would you be willing to help implement this feature?
@antfu antfu added the enhancement New feature or request label Feb 4, 2025
@pi0
Copy link
Member

pi0 commented Feb 4, 2025

Currently we are using .restoreAll() and .wrapAll() to workaround that in Nuxt:

I was not aware of this. can you share the context of why it was needed? (note the v4 plan: #335, right tags will be removed and partially moved left)

const isConsoleWrapped = this.isConsoleWrapped

This is going to be little more complicated. There is no guarantee of having same consola instance (dependency, runtime module (loc, cjs|esm) or even instance). if we want to do it, it needs global state.

@antfu
Copy link
Member Author

antfu commented Feb 4, 2025

Consola's interop adds timestamp or other format changes to the log, which sometimes might break the layout when it intercepts the log from other tools. For example: nuxt/framework#9761

Meanwhile I found that sometimes the log being eaten by consola with server/api for debugging, I am not yet sure why but unwrap the logger fixes it.

it needs global state.

Yeah, I was more like putting a pesudo code to show the idea. I guess we need to check the existence of console.__log and so to determine if it's wrapped?

@pi0
Copy link
Member

pi0 commented Feb 5, 2025

Wrapping is because of the right tags (timestamp) which won’t happen in v4.

If you could somehow reproduce condition of logs not being shown, please feel free to open new issue with reproduction 🙏 (note: unenv stubs consola in production with console)

In the meantime i rather to first work on v4 to see if the workaround could be avoided to not temporarily disabling wrapper in certain conditions.

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

No branches or pull requests

2 participants