Closed Innei closed 1 year ago
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.
FancyReporter
process.stdout.columns
undefined
https://github.com/unjs/consola/blob/abb016f568e8942a3c3222258757bfff14304f7c/src/reporters/basic.js#L63
Width will set to 0 if columns is undefined. But if width is 0, space will ignore, and right date will be ignore too.
0
https://github.com/unjs/consola/blob/abb016f568e8942a3c3222258757bfff14304f7c/src/reporters/fancy.js#L77-L81
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'
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) } }
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
Put right date to left, if process.stdout.columns is undefined.
consola v3 without width:
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 foundprocess.stdout.columns
isundefined
in PM2 pty, beause PM2 is not run in a terminal simulator.https://github.com/unjs/consola/blob/abb016f568e8942a3c3222258757bfff14304f7c/src/reporters/basic.js#L63
Width will set to
0
if columns isundefined
. But if width is0
, space will ignore, and right date will be ignore too.https://github.com/unjs/consola/blob/abb016f568e8942a3c3222258757bfff14304f7c/src/reporters/fancy.js#L77-L81
So, there is a way to put the time on the right on the left if columns is
0
, or provide a option likedateAlign: 'right' | 'left'
Current, I hack it if check env is in pty(no terminal simulator) and then replace consola output string.
To reproduce
This is my simple code.
Run in pm2:
Output (in PM2):
Expect
Put right date to left, if process.stdout.columns is
undefined
.