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

Allow overwriting Bun.stdout and Bun.stderr #14874

Open
TomasHubelbauer opened this issue Oct 28, 2024 · 9 comments
Open

Allow overwriting Bun.stdout and Bun.stderr #14874

TomasHubelbauer opened this issue Oct 28, 2024 · 9 comments
Labels
bun.js Something to do with a Bun-specific API enhancement New feature or request

Comments

@TomasHubelbauer
Copy link

What is the problem this feature would solve?

I have a piece of code where a method writes to the standard output using console.log and when a condition is met, it should instead write to a file.

I have no control over the implementation of the method, so I am looking for ways to redirect the console.log and console.error calls.

I tried doing this:

const file = Bun.file('file-name.ext');
Bun.stdout = file;
Bun.stderr = file;

method(); // Calls `console.log` inside

However the standard output and standard error streams remained the terminal-attached ones, the file was created, but remained empty.

What is the feature you are proposing to solve the problem?

Make Bun.stdout and Bun.stderr read-write and respect the overriden values when using these internally as sinks for console methods. (But this should probably also affect process.stdout and process.stderr maybe?

What alternatives have you considered?

No response

@TomasHubelbauer TomasHubelbauer added the enhancement New feature or request label Oct 28, 2024
@heimskr
Copy link
Member

heimskr commented Oct 28, 2024

Would it suffice to replace the console.log global?

console.log = (...args) => {
    writeToFile("filename", ...args);
};

@TomasHubelbauer
Copy link
Author

That's what I'm doing right now but it doesn't feel like a correct solution. A way to tell Bun to write to a different BunFile whenever stdout or stderr is written into would be preferable.

@nektro nektro added the bun.js Something to do with a Bun-specific API label Oct 28, 2024
@guest271314

This comment was marked as off-topic.

@guest271314

This comment was marked as off-topic.

@TomasHubelbauer
Copy link
Author

Note that this feature request is about Bun.stdout and Bun.stderr. Console method are just one example of where these sinks are used.

@guest271314

This comment was marked as off-topic.

@TomasHubelbauer
Copy link
Author

Agreed. That's why I am using it, as I've stated. The feature request still stands.

@guest271314

This comment was marked as off-topic.

@guest271314

This comment was marked as off-topic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bun.js Something to do with a Bun-specific API enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants