0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2025-02-17 23:45:29 -05:00

fix: hydrate template placeholders in log messages when format is set to 'json' (#2652)

This commit is contained in:
Ed Clement 2021-11-11 01:22:11 -05:00 committed by GitHub
parent 7632edd0e1
commit e75c0a3b95
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 4 deletions

View file

@ -0,0 +1,6 @@
---
'@verdaccio/logger-prettify': minor
'@verdaccio/logger': patch
---
hydrate template placeholders in log messages when format is set to 'json'

View file

@ -1,6 +1,8 @@
import { printMessage } from './formatter'; import { fillInMsgTemplate, printMessage } from './formatter';
import { PrettyOptionsExtended } from './types'; import { PrettyOptionsExtended } from './types';
export { fillInMsgTemplate };
export type PrettyFactory = (param) => string; export type PrettyFactory = (param) => string;
/* /*
@ -8,11 +10,11 @@ export type PrettyFactory = (param) => string;
{ messageKey: 'msg', levelFirst: true, prettyStamp: false } { messageKey: 'msg', levelFirst: true, prettyStamp: false }
*/ */
module.exports = function prettyFactory(options: PrettyOptionsExtended): PrettyFactory { export default function prettyFactory(options: PrettyOptionsExtended): PrettyFactory {
// the break line must happens in the prettify component // the break line must happens in the prettify component
const breakLike = '\n'; const breakLike = '\n';
return (inputData): string => { return (inputData): string => {
// FIXME: review colors by default is true // FIXME: review colors by default is true
return printMessage(inputData, options, true) + breakLike; return printMessage(inputData, options, true) + breakLike;
}; };
}; }

View file

@ -3,6 +3,7 @@ import _ from 'lodash';
import pino from 'pino'; import pino from 'pino';
import { warningUtils } from '@verdaccio/core'; import { warningUtils } from '@verdaccio/core';
import prettifier, { fillInMsgTemplate } from '@verdaccio/logger-prettify';
const debug = buildDebug('verdaccio:logger'); const debug = buildDebug('verdaccio:logger');
@ -53,6 +54,7 @@ export function createLogger(
// pretty logs are not allowed in production for performance reasons // pretty logs are not allowed in production for performance reasons
if ((format === DEFAULT_LOG_FORMAT || format !== 'json') && isProd() === false) { if ((format === DEFAULT_LOG_FORMAT || format !== 'json') && isProd() === false) {
pinoConfig = Object.assign({}, pinoConfig, { pinoConfig = Object.assign({}, pinoConfig, {
prettifier,
// more info // more info
// https://github.com/pinojs/pino-pretty/issues/37 // https://github.com/pinojs/pino-pretty/issues/37
prettyPrint: { prettyPrint: {
@ -60,7 +62,23 @@ export function createLogger(
prettyStamp: format === 'pretty-timestamped', prettyStamp: format === 'pretty-timestamped',
...prettyPrintOptions, ...prettyPrintOptions,
}, },
prettifier: require('@verdaccio/logger-prettify'), });
} else {
pinoConfig = Object.assign({}, pinoConfig, {
// more info
// https://github.com/pinojs/pino/blob/v7.1.0/docs/api.md#hooks-object
hooks: {
logMethod(inputArgs, method) {
const [templateObject, message, ...otherArgs] = inputArgs;
const templateVars =
!!templateObject && typeof templateObject === 'object'
? Object.getOwnPropertyNames(templateObject)
: [];
if (!message || !templateVars.length) return method.apply(this, inputArgs);
const hydratedMessage = fillInMsgTemplate(message, templateObject, false);
return method.apply(this, [templateObject, hydratedMessage, ...otherArgs]);
},
},
}); });
} }
const logger = pino(pinoConfig, destination); const logger = pino(pinoConfig, destination);