1
0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-12-24 21:15:51 +01: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

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

@ -1,6 +1,8 @@
import { printMessage } from './formatter';
import { fillInMsgTemplate, printMessage } from './formatter';
import { PrettyOptionsExtended } from './types';
export { fillInMsgTemplate };
export type PrettyFactory = (param) => string;
/*
@ -8,11 +10,11 @@ export type PrettyFactory = (param) => string;
{ 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
const breakLike = '\n';
return (inputData): string => {
// FIXME: review colors by default is true
return printMessage(inputData, options, true) + breakLike;
};
};
}

@ -3,6 +3,7 @@ import _ from 'lodash';
import pino from 'pino';
import { warningUtils } from '@verdaccio/core';
import prettifier, { fillInMsgTemplate } from '@verdaccio/logger-prettify';
const debug = buildDebug('verdaccio:logger');
@ -53,6 +54,7 @@ export function createLogger(
// pretty logs are not allowed in production for performance reasons
if ((format === DEFAULT_LOG_FORMAT || format !== 'json') && isProd() === false) {
pinoConfig = Object.assign({}, pinoConfig, {
prettifier,
// more info
// https://github.com/pinojs/pino-pretty/issues/37
prettyPrint: {
@ -60,7 +62,23 @@ export function createLogger(
prettyStamp: format === 'pretty-timestamped',
...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);