Skip to content

Commit

Permalink
[TECH] Ajout d'un format compact de log pour le dev
Browse files Browse the repository at this point in the history
  • Loading branch information
pix-service-auto-merge authored Nov 26, 2024
2 parents 2104ad0 + 8e8739e commit 099dd32
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
10 changes: 10 additions & 0 deletions api/sample.env
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,16 @@ LOG_ENDING_EVENT_DISPATCH=true
# type: String
LOG_FOR_HUMANS=true


# Logs for humans format
#
# "compact": Always display logs on a single line with key information.
#
# presence: optional
# type: String
# LOG_FOR_HUMANS_FORMAT=compact


# Trace email sending in the mailer
# DEBUG="pix:mailer:email"

Expand Down
1 change: 1 addition & 0 deletions api/src/shared/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ const configuration = (function () {
enabled: toBoolean(process.env.LOG_ENABLED),
logLevel: process.env.LOG_LEVEL || 'info',
logForHumans: _getLogForHumans(),
logForHumansCompactFormat: process.env.LOG_FOR_HUMANS_FORMAT === 'compact',
enableKnexPerformanceMonitoring: toBoolean(process.env.ENABLE_KNEX_PERFORMANCE_MONITORING),
enableLogStartingEventDispatch: toBoolean(process.env.LOG_STARTING_EVENT_DISPATCH),
enableLogEndingEventDispatch: toBoolean(process.env.LOG_ENDING_EVENT_DISPATCH),
Expand Down
47 changes: 47 additions & 0 deletions api/src/shared/infrastructure/utils/logger.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import isEmpty from 'lodash/isEmpty.js';
import omit from 'lodash/omit.js';
import pino from 'pino';
import pretty from 'pino-pretty';

Expand All @@ -13,6 +15,8 @@ if (logging.logForHumans) {
colorize: true,
translateTime: omitDay,
ignore: 'pid,hostname',
messageFormat: logging.logForHumansCompactFormat ? messageFormatCompact : undefined,
hideObject: logging.logForHumansCompactFormat,
});
}

Expand All @@ -25,4 +29,47 @@ const logger = pino(
prettyPrint,
);

function messageFormatCompact(log, messageKey, _logLevel, { colors }) {
const message = log[messageKey];
const { err, req, res, responseTime } = log;

// compact log for errors
if (err) {
const stack = colors.red(err.stack);
return `${message}\n${stack}`;
}

// compact log for HTTP requests
if (req && res) {
const method = req.method?.toUpperCase();

const queries = req.metrics?.knexQueryCount ? `sql:${req.metrics.knexQueryCount}` : '';
const queriesTime = req.metrics?.knexTotalTimeSpent ? `sql-time:${req.metrics.knexTotalTimeSpent}` : '';

const statusCode = res.statusCode >= 400 ? colors.red(res.statusCode) : colors.greenBright(res.statusCode);
const request = colors.magentaBright([method, req.url].filter(Boolean).join(' '));
const details = colors.yellow([queries, queriesTime].filter(Boolean).join(' '));
const time = colors.gray(`(${responseTime}ms)`);

return [statusCode, request, details, time].filter(Boolean).join(' - ');
}

// compact log by default
const compactLog = omit(log, [
messageKey,
'id',
'level',
'time',
'pid',
'hostname',
'uri',
'address',
'event',
'started',
'created',
]);
const details = !isEmpty(compactLog) ? colors.gray(JSON.stringify(compactLog)) : '';
return `${message} ${details}`;
}

export { logger };

0 comments on commit 099dd32

Please sign in to comment.