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

Can't show right date in PM2 log #128

Closed
Innei opened this issue Feb 14, 2022 · 1 comment
Closed

Can't show right date in PM2 log #128

Innei opened this issue Feb 14, 2022 · 1 comment

Comments

@Innei
Copy link

Innei commented Feb 14, 2022

Hi, I've been using Consola for a long time, It's very nice.

I found that FancyReporter log right date is not work in PM2, so I check source code, I found process.stdout.columns is undefined in PM2 pty, beause PM2 is not run in a terminal simulator.

width: stdout.columns || 0

Width will set to 0 if columns is undefined. But if width is 0, space will ignore, and right date will be ignore too.

if (space > 0 && width >= 80) {
line = left + ' '.repeat(space) + right
} else {
line = left
}

So, there is a way to put the time on the right on the left if columns is 0, or provide a option like dateAlign: 'right' | 'left'

Current, I hack it if check env is in pty(no terminal simulator) and then replace consola output string.

// this is my hack
class Reporter extends FancyReporter {
  isInVirtualTerminal = typeof process.stdout.columns === 'undefined' // HACK: if got `undefined` that means in PM2 pty
  protected formatDate(date: Date): string {
    return this.isInVirtualTerminal ? '' : super.formatDate(date)
  }

  protected formatLogObj(): string {
    return this.isInVirtualTerminal
      ? (
          chalk.gray(getShortTime(new Date())) +
          ' ' +
          super.formatLogObj.apply(this, arguments).replace(/^\n/, '')
        ).trimEnd()
      : super.formatLogObj.apply(this, arguments)
  }
}

To reproduce

This is my simple code.

// index.js
// @ts-check
const { FancyReporter } = require('consola')
const consola = require('consola')

console.log(process.stdout.columns) // got `undefined` in PM2 pty

consola.create({
  reporters: [new FancyReporter()],
})

consola.wrapAll()

consola.log('Hello world')

Run in pm2:

pm2 start index.js
pm2 logs --raw

Output (in PM2):

2|index  | undefined
2|index  | Hello world

Expect

Put right date to left, if process.stdout.columns is undefined.

@Innei Innei changed the title Can't show right date in PM2 pty Can't show right date in PM2 log Feb 16, 2022
@pi0 pi0 closed this as completed in 6704798 Apr 10, 2023
@pi0
Copy link
Member

pi0 commented Apr 10, 2023

consola v3 without width:

image

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

No branches or pull requests

2 participants