mirror of
https://github.com/verdaccio/verdaccio.git
synced 2024-12-16 21:56:25 -05:00
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 <juanpicado19@gmail.com>
This commit is contained in:
parent
7f04697175
commit
ac45c0b5b2
4 changed files with 26 additions and 17 deletions
|
@ -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.
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
};
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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()) {
|
||||
|
|
Loading…
Reference in a new issue