From ac45c0b5b2ca1d35a69f9ce54ca87384ae576b9d Mon Sep 17 00:00:00 2001 From: osher Date: Thu, 3 Mar 2022 00:41:30 +0200 Subject: [PATCH] feature: config control for colors in logs (#3011) * pass `logs.colors` as `prettyOptions.colorize` * `prettyPrintOptions` defaults is no concern of `createLogger` * call it `colors` not to confuse with `pinoPretty.colorize` * fix hardcoded `true` for `options.colors` * Support `VERDACCIO_LOGGER_COLORS` overriding env-var * Update docs for `VERDACCIO_LOGGER_COLORS` * docs for `VERDACCIO_LOGGER_COLORS` * docs for `VERDACCIO_LOGGER_COLORS` * `.isTTY` from `stdout` not `stdin` both work, but I want to ask if I emit to TTY, not if I consume from TTY. * .md format * format * more format guesses * declare `PrettyOptionsExtended.colors` * lint * docs: `EXPERIMENTAL__` prefix * logger.ts - prefix `EXPERIMENTAL__` * Update env.variables.md * env.variables.md - remove double `_` * Update logger.ts * logger.ts - remove double `_`, fix boolean parsing * env.variables.md - explain boolean parsing * chore: format * chore: add format, improve logic Co-authored-by: Juan Picado --- docs/env.variables.md | 8 +++++++ src/lib/logger/formatter/index.ts | 3 +-- src/lib/logger/formatter/prettifier.ts | 1 + src/lib/logger/logger.ts | 31 +++++++++++++------------- 4 files changed, 26 insertions(+), 17 deletions(-) diff --git a/docs/env.variables.md b/docs/env.variables.md index 28d8951c0..9e8c74a90 100644 --- a/docs/env.variables.md +++ b/docs/env.variables.md @@ -45,3 +45,11 @@ $ VERDACCIO_FORWARDED_PROTO=CloudFront-Forwarded-Proto verdaccio --listen 5000 #### VERDACCIO_STORAGE_PATH By default, the storage is taken from config file, but using this variable allows to set it from environment variable. + +#### EXPERIMENTAL_VERDACCIO_LOGGER_COLORS + +Overrides `logs.colors` from the `config.yaml`. + +Note that any value that other than `false` will result in `true`. + +When both are not provided - the colors are on by default for TTY processes, and off for processes that are not. diff --git a/src/lib/logger/formatter/index.ts b/src/lib/logger/formatter/index.ts index 266db296b..baa93c369 100644 --- a/src/lib/logger/formatter/index.ts +++ b/src/lib/logger/formatter/index.ts @@ -11,7 +11,6 @@ module.exports = function prettyFactory(options: PrettyOptionsExtended): PrettyF // the break line must happens in the prettify component const breakLike = '\n'; return (inputData): string => { - // FIXME: review colors by default is true - return printMessage(inputData, options, true) + breakLike; + return printMessage(inputData, options, options.colors) + breakLike; }; }; diff --git a/src/lib/logger/formatter/prettifier.ts b/src/lib/logger/formatter/prettifier.ts index 469ff0af4..7ec7b37da 100644 --- a/src/lib/logger/formatter/prettifier.ts +++ b/src/lib/logger/formatter/prettifier.ts @@ -22,6 +22,7 @@ export function formatLoggingDate(time: number, message): string { export interface PrettyOptionsExtended extends PrettyOptions { prettyStamp: boolean; + colors: boolean; } let LEVEL_VALUE_MAX = 0; // eslint-disable-next-line guard-for-in diff --git a/src/lib/logger/logger.ts b/src/lib/logger/logger.ts index 21ef20438..7cd87924c 100644 --- a/src/lib/logger/logger.ts +++ b/src/lib/logger/logger.ts @@ -3,8 +3,6 @@ import { yellow } from 'kleur'; import _ from 'lodash'; import pino from 'pino'; -import { padLeft } from './utils'; - function isProd() { return process.env.NODE_ENV === 'production'; } @@ -21,16 +19,7 @@ export type LogPlugin = { export type LogType = 'file' | 'stdout'; export type LogFormat = 'json' | 'pretty-timestamped' | 'pretty'; -export function createLogger( - options = { level: 'http' }, - destination = pino.destination(1), - format: LogFormat = DEFAULT_LOG_FORMAT, - prettyPrintOptions = { - // we hide warning since the prettifier should not be used in production - // https://getpino.io/#/docs/pretty?id=prettifier-api - suppressFlushSyncWarning: true, - } -) { +export function createLogger(options = { level: 'http' }, destination = pino.destination(1), format: LogFormat = DEFAULT_LOG_FORMAT, prettyPrintOptions) { if (_.isNil(format)) { format = DEFAULT_LOG_FORMAT; } @@ -86,6 +75,7 @@ const DEFAULT_LOGGER_CONF: LoggerConfigItem = { type: 'stdout', format: 'pretty', level: 'http', + colors: true, }; export type LoggerConfigItem = { @@ -94,6 +84,7 @@ export type LoggerConfigItem = { format?: LogFormat; path?: string; level?: string; + colors?: boolean; }; export type LoggerConfig = LoggerConfigItem[]; @@ -120,18 +111,28 @@ export function setup(options: LoggerConfig | LoggerConfigItem = [DEFAULT_LOGGER ); } const pinoConfig = { level: loggerConfig.level }; + let colors = loggerConfig?.colors === true ? process.stdout.isTTY : false; + if ('EXPERIMENTAL_VERDACCIO_LOGGER_COLORS' in process.env) { + colors = process.env.EXPERIMENTAL_VERDACCIO_LOGGER_COLORS != 'false'; + } + const prettyPrintOptions = { + // we hide warning since the prettifier should not be used in production + // https://getpino.io/#/docs/pretty?id=prettifier-api + suppressFlushSyncWarning: true, + colors, + }; if (loggerConfig.type === 'file') { debug('logging file enabled'); const destination = pino.destination(loggerConfig.path); process.on('SIGUSR2', () => destination.reopen()); - logger = createLogger(pinoConfig, destination, loggerConfig.format); + logger = createLogger(pinoConfig, destination, loggerConfig.format, prettyPrintOptions); } else if (loggerConfig.type === 'rotating-file') { process.emitWarning('rotating-file type is not longer supported, consider use [logrotate] instead'); debug('logging stdout enabled'); - logger = createLogger(pinoConfig, pino.destination(1), loggerConfig.format); + logger = createLogger(pinoConfig, pino.destination(1), loggerConfig.format, prettyPrintOptions); } else { debug('logging stdout enabled'); - logger = createLogger(pinoConfig, pino.destination(1), loggerConfig.format); + logger = createLogger(pinoConfig, pino.destination(1), loggerConfig.format, prettyPrintOptions); } if (isProd()) {