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

Print logs to stderr #4922

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft

Print logs to stderr #4922

wants to merge 2 commits into from

Conversation

gonzaloriestra
Copy link
Contributor

@gonzaloriestra gonzaloriestra commented Nov 26, 2024

WHY are these changes introduced?

Fixes #3863

WHAT is this pull request doing?

How to test your changes?

Post-release steps

Measuring impact

How do we know this change was effective? Please choose one:

  • n/a - this doesn't need measurement, e.g. a linting rule or a bug-fix
  • Existing analytics will cater for this addition
  • PR includes analytics changes to measure impact

Checklist

  • I've considered possible cross-platform impacts (Mac, Linux, Windows)
  • I've considered possible documentation changes

Copy link
Contributor

Unused dependencies (1)

Filename dependencies
packages/app/package.json micromatch

Unused devDependencies (1)

Filename devDependencies
packages/app/package.json @types/micromatch

Unused types (1)

Filename types
packages/app/src/cli/services/build/extension.ts BuildFunctionExtensionOptions

Copy link
Contributor

Differences in type declarations

We detected differences in the type declarations generated by Typescript for this branch compared to the baseline ('main' branch). Please, review them to ensure they are backward-compatible. Here are some important things to keep in mind:

  • Some seemingly private modules might be re-exported through public modules.
  • If the branch is behind main you might see odd diffs, rebase main into this branch.

New type declarations

We found no new type declarations in this PR

Existing type declarations

packages/cli-kit/dist/public/node/output.d.ts
@@ -141,19 +141,19 @@ export interface OutputProcess {
     action: (stdout: Writable, stderr: Writable, signal: AbortSignal) => Promise<void>;
 }
 /**
- * Prints a log message in the console.
+ * Prints a log message in the console to stdout.
  *
  * @param message - The message to print.
  */
 export declare function consoleLog(message: string): void;
 /**
- * Prints an error message in the console.
+ * Prints an error message in the console to stderr.
  *
  * @param message - The message to print.
  */
 export declare function consoleError(message: string): void;
 /**
- * Prints a warning message in the console.
+ * Prints a warning message in the console to stderr.
  *
  * @param message - The message to print.
  */

@@ -258,7 +256,7 @@ export const clearCollectedLogs = (): void => {
* @param content - The content to be output to the user.
* @param logger - The logging function to use to output to the user.
*/
export function outputInfo(content: OutputMessage, logger: Logger = consoleLog): void {
export function outputInfo(content: OutputMessage, logger: Logger = consoleWarn): void {
Copy link
Contributor

Choose a reason for hiding this comment

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

May I suggest a export function output(content: outputMessage, logger: Logger = consoleLog): void {

?

That way all the output{log,warn,error} would all go to stderr and we'd make it explicit which one goes to stdout?

STDOUT = scriptable output
STDERR = logging

Copy link
Contributor

Choose a reason for hiding this comment

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

console.log goes to stdout...

I think the idea is that we have several output functions based on log level, but by default all log to consoleWarn which uses console.warn which goes to stderr.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah that's what I'm saying. Let's change this so that console.log is only used intentionally.

  • outputInfo - STDERR loglevel info
  • outputDebug - STDERR loglevel debug
  • outputWarn - STDERR loglevel warn
  • outputError - STDERR loglevel error
  • output - STDOUT. Use only for JSON or whatever could be piped into another bash script.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we should ever log anything without specifying a log level. We can default everything to stderr and make stdout an explicit choice by specifying stdout as the logger.

Copy link
Contributor

Choose a reason for hiding this comment

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

idk I think output is output and logging is logging. I personally don't see a problem with these two things:

  • logging that goes to STDERR and requires a loglevel
  • output goes to STDOUT and doesn't require a loglevel.

STDOUT is for piping & scripts. Why would we want a loglevel in there? What am I missing?

Copy link
Contributor

Choose a reason for hiding this comment

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

Because, while we don't currently support it, many programs have a silent log level which doesn't print anything. And I think we should ultimately support that. Keeping everything with log levels gives us more room to fine-tune logging behavior.

Copy link
Contributor

Choose a reason for hiding this comment

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

Here is a good example: https://docs.npmjs.com/cli/v10/using-npm/config#loglevel

Silent is just another log level.

Copy link
Contributor

Choose a reason for hiding this comment

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

Wouldn't that mean that --json --silent do nothing at all? There's always &> /dev/null?

Whenever I'm scripting, I'm mostly annoyed by shit showing up in STDOUT and that's why I'd want --silent, but if it doesn't, then I don't see the point.

One of pnpm or nx is an example offender here. If I want to run the shopify theme language-server with the dev build in Shopify/cli, I can only do that by running the build node package stuff because otherwise pnpm & nx are outputting crap to STDOUT that don't fit the LSP.

Same goes for pnpm shopify theme check --json.

Copy link
Contributor

@charlespwd charlespwd Nov 28, 2024

Choose a reason for hiding this comment

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

For that reason, whenever I want to test a Shopify/theme-tools bump in vim. I have to use a script in my $HOME/bin that is this:

#!/usr/bin/env bash

if [[ -z $DEBUG ]]; then
  node /Users/charles/src/github.com/Shopify/cli/packages/cli/bin/dev.js "$@"
else
  node --inspect-brk /Users/charles/src/github.com/Shopify/cli/packages/cli/bin/dev.js "$@"
fi

Which means I need pnpm run build any time I want to test it too.

Copy link
Contributor

Choose a reason for hiding this comment

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

Have you tried pnpm --silent shopify:run ...

--silent quiets pnpm

shopify:run skips the build step (you have to run pnpm shopify build first, but anyway you need to do that in your script)

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

Successfully merging this pull request may close these issues.

[Bug]: Logging should be done to STDERR by default. STDOUT should be reserved for scriptable output.
3 participants