From 40087625709d5dd0af77088466a1af97f29483dc Mon Sep 17 00:00:00 2001 From: Nicolas Dupont Date: Tue, 19 Nov 2024 15:33:56 +0100 Subject: [PATCH 1/6] Delegate dates logging to process manager in prod --- scripts/dataset/logger/index.js | 4 +++- src/collection-api/logger.js | 6 +++++- src/logger/index.js | 18 ++++++------------ 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/scripts/dataset/logger/index.js b/scripts/dataset/logger/index.js index 3bf065e94..fbd0dec67 100644 --- a/scripts/dataset/logger/index.js +++ b/scripts/dataset/logger/index.js @@ -10,7 +10,9 @@ logger.format = combine( printf(({ level, message, counter, hash, timestamp }) => { const prefix = counter && hash ? `${counter.toString().padEnd(6)} ${hash.padEnd(40)}` : ''; - return `${timestamp} ${level.padEnd(15)} ${prefix.padEnd(50)} ${message}`; + const timestampPrefix = process.env.NODE_ENV !== 'production' ? `${timestamp} ` : ''; + + return `${timestampPrefix}${level.padEnd(15)} ${prefix.padEnd(50)} ${message}`; }), ); diff --git a/src/collection-api/logger.js b/src/collection-api/logger.js index c1df1c8fa..81dbc0bf4 100644 --- a/src/collection-api/logger.js +++ b/src/collection-api/logger.js @@ -31,7 +31,11 @@ const logger = winston.createLogger({ format: combine( colorize(), timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }), - printf(({ level, message, timestamp }) => `${timestamp} ${level.padEnd(15)} ${message}`), + printf(({ level, message, timestamp }) => { + const timestampPrefix = process.env.NODE_ENV !== 'production' ? `${timestamp} ` : ''; + + return `${timestampPrefix}${level.padEnd(15)} ${message}`; + }), ), transports, rejectionHandlers: transports, diff --git a/src/logger/index.js b/src/logger/index.js index ca8594759..0d9e3e1c4 100644 --- a/src/logger/index.js +++ b/src/logger/index.js @@ -10,21 +10,15 @@ const alignedWithColorsAndTime = combine( colorize(), timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }), printf(({ level, message, timestamp, serviceId, termsType, documentId }) => { - let prefix = ''; + const servicePrefix = serviceId && termsType + ? `${serviceId} — ${termsType}${documentId ? `:${documentId}` : ''}` + : ''; - if (serviceId && termsType) { - prefix = `${serviceId} — ${termsType}`; - } - - if (documentId) { - prefix = `${prefix}:${documentId}`; - } + const truncatedPrefix = servicePrefix.length > 75 ? `${servicePrefix.slice(0, 74)}…` : servicePrefix; - if (prefix.length > 75) { - prefix = `${prefix.substring(0, 74)}…`; - } + const timestampPrefix = process.env.NODE_ENV !== 'production' ? `${timestamp} ` : ''; - return `${timestamp} ${level.padEnd(15)} ${prefix.padEnd(75)} ${message}`; + return `${timestampPrefix}${level.padEnd(15)} ${truncatedPrefix.padEnd(75)} ${message}`; }), ); From 14a9e9ccb48a91ba195028d2cd64f8b61a659111 Mon Sep 17 00:00:00 2001 From: Nicolas Dupont Date: Tue, 19 Nov 2024 15:36:12 +0100 Subject: [PATCH 2/6] Add changelog entry --- CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7bb6c99d9..95337467e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,14 @@ All changes that impact users of this module are documented in this file, in the [Common Changelog](https://common-changelog.org) format with some additional specifications defined in the CONTRIBUTING file. This codebase adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## Unreleased [minor] + +> Development of this release was supported by the [French Ministry for Foreign Affairs](https://www.diplomatie.gouv.fr/fr/politique-etrangere-de-la-france/diplomatie-numerique/) through its ministerial [State Startups incubator](https://beta.gouv.fr/startups/open-terms-archive.html) under the aegis of the Ambassador for Digital Affairs. + +### Changed + +- Delegate prefixing logs with dates to process manager in production environment; to maintain dates with PM2, use either [the `--time` CLI option](https://pm2.keymetrics.io/docs/usage/log-management/#auto-prefixing-logs-with-date) or [the `log_date_format` configuration file option](https://pm2.keymetrics.io/docs/usage/application-declaration/#log-files) + ## 2.6.0 - 2024-11-19 _Full changeset and discussions: [#1116](https://github.com/OpenTermsArchive/engine/pull/1116)._ From 132aade52c1f990b69f1a808e6d567fe83bdf092 Mon Sep 17 00:00:00 2001 From: Nicolas Dupont Date: Wed, 20 Nov 2024 09:59:45 +0100 Subject: [PATCH 3/6] Make timestamp prefix in logs configurable --- config/default.json | 3 ++- scripts/dataset/logger/index.js | 3 ++- src/collection-api/logger.js | 2 +- src/logger/index.js | 2 +- 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/config/default.json b/config/default.json index 0d3d82eac..2d18aeac3 100644 --- a/config/default.json +++ b/config/default.json @@ -48,7 +48,8 @@ "host": "smtp-relay.sendinblue.com", "username": "admin@opentermsarchive.org" }, - "sendMailOnError": false + "sendMailOnError": false, + "timestampPrefix": true }, "notifier": { "sendInBlue": { diff --git a/scripts/dataset/logger/index.js b/scripts/dataset/logger/index.js index fbd0dec67..e143895d3 100644 --- a/scripts/dataset/logger/index.js +++ b/scripts/dataset/logger/index.js @@ -1,3 +1,4 @@ +import config from 'config'; import winston from 'winston'; import logger from '../../../src/logger/index.js'; @@ -10,7 +11,7 @@ logger.format = combine( printf(({ level, message, counter, hash, timestamp }) => { const prefix = counter && hash ? `${counter.toString().padEnd(6)} ${hash.padEnd(40)}` : ''; - const timestampPrefix = process.env.NODE_ENV !== 'production' ? `${timestamp} ` : ''; + const timestampPrefix = config.get('@opentermsarchive/engine.logger.timestampPrefix') ? `${timestamp} ` : ''; return `${timestampPrefix}${level.padEnd(15)} ${prefix.padEnd(50)} ${message}`; }), diff --git a/src/collection-api/logger.js b/src/collection-api/logger.js index 81dbc0bf4..44042f9d6 100644 --- a/src/collection-api/logger.js +++ b/src/collection-api/logger.js @@ -32,7 +32,7 @@ const logger = winston.createLogger({ colorize(), timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }), printf(({ level, message, timestamp }) => { - const timestampPrefix = process.env.NODE_ENV !== 'production' ? `${timestamp} ` : ''; + const timestampPrefix = config.get('@opentermsarchive/engine.logger.timestampPrefix') ? `${timestamp} ` : ''; return `${timestampPrefix}${level.padEnd(15)} ${message}`; }), diff --git a/src/logger/index.js b/src/logger/index.js index 0d9e3e1c4..d7c1a2627 100644 --- a/src/logger/index.js +++ b/src/logger/index.js @@ -16,7 +16,7 @@ const alignedWithColorsAndTime = combine( const truncatedPrefix = servicePrefix.length > 75 ? `${servicePrefix.slice(0, 74)}…` : servicePrefix; - const timestampPrefix = process.env.NODE_ENV !== 'production' ? `${timestamp} ` : ''; + const timestampPrefix = config.get('@opentermsarchive/engine.logger.timestampPrefix') ? `${timestamp} ` : ''; return `${timestampPrefix}${level.padEnd(15)} ${truncatedPrefix.padEnd(75)} ${message}`; }), From fed84bfd18cba0dcd930e298fd677ac4fbcbed72 Mon Sep 17 00:00:00 2001 From: Nicolas Dupont Date: Wed, 20 Nov 2024 10:00:09 +0100 Subject: [PATCH 4/6] Use ISO 8601 format for timestamps prefix in logs --- scripts/dataset/logger/index.js | 2 +- src/collection-api/logger.js | 2 +- src/logger/index.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/dataset/logger/index.js b/scripts/dataset/logger/index.js index e143895d3..4c2389ecf 100644 --- a/scripts/dataset/logger/index.js +++ b/scripts/dataset/logger/index.js @@ -7,7 +7,7 @@ const { combine, timestamp, printf, colorize } = winston.format; logger.format = combine( colorize(), - timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }), + timestamp({ format: 'YYYY-MM-DDTHH:MM:SSZ' }), printf(({ level, message, counter, hash, timestamp }) => { const prefix = counter && hash ? `${counter.toString().padEnd(6)} ${hash.padEnd(40)}` : ''; diff --git a/src/collection-api/logger.js b/src/collection-api/logger.js index 44042f9d6..c1287dbd4 100644 --- a/src/collection-api/logger.js +++ b/src/collection-api/logger.js @@ -30,7 +30,7 @@ if (config.get('@opentermsarchive/engine.logger.sendMailOnError')) { const logger = winston.createLogger({ format: combine( colorize(), - timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }), + timestamp({ format: 'YYYY-MM-DDTHH:MM:SSZ' }), printf(({ level, message, timestamp }) => { const timestampPrefix = config.get('@opentermsarchive/engine.logger.timestampPrefix') ? `${timestamp} ` : ''; diff --git a/src/logger/index.js b/src/logger/index.js index d7c1a2627..ea94f489c 100644 --- a/src/logger/index.js +++ b/src/logger/index.js @@ -8,7 +8,7 @@ const { combine, timestamp, printf, colorize } = winston.format; const alignedWithColorsAndTime = combine( colorize(), - timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }), + timestamp({ format: 'YYYY-MM-DDTHH:MM:SSZ' }), printf(({ level, message, timestamp, serviceId, termsType, documentId }) => { const servicePrefix = serviceId && termsType ? `${serviceId} — ${termsType}${documentId ? `:${documentId}` : ''}` From 7ff24a6f9cb0e3515f488834d0c74795ff1d7988 Mon Sep 17 00:00:00 2001 From: Nicolas Dupont Date: Wed, 20 Nov 2024 10:06:39 +0100 Subject: [PATCH 5/6] Update changelog entry --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 95337467e..f02bccd64 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,9 +6,9 @@ All changes that impact users of this module are documented in this file, in the > Development of this release was supported by the [French Ministry for Foreign Affairs](https://www.diplomatie.gouv.fr/fr/politique-etrangere-de-la-france/diplomatie-numerique/) through its ministerial [State Startups incubator](https://beta.gouv.fr/startups/open-terms-archive.html) under the aegis of the Ambassador for Digital Affairs. -### Changed +### Added -- Delegate prefixing logs with dates to process manager in production environment; to maintain dates with PM2, use either [the `--time` CLI option](https://pm2.keymetrics.io/docs/usage/log-management/#auto-prefixing-logs-with-date) or [the `log_date_format` configuration file option](https://pm2.keymetrics.io/docs/usage/application-declaration/#log-files) +- Add configuration option to toggle timestamp prefix in logs; set `@opentermsarchive/engine.logger.timestampPrefix` to `true` or `false` in your configuration file to control this feature. ## 2.6.0 - 2024-11-19 From 3c3b6974beab66c015ab9a800f4b987228fcbf06 Mon Sep 17 00:00:00 2001 From: Nicolas Dupont Date: Wed, 20 Nov 2024 10:15:29 +0100 Subject: [PATCH 6/6] Add link to doc --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f02bccd64..66c398c0e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ All changes that impact users of this module are documented in this file, in the ### Added -- Add configuration option to toggle timestamp prefix in logs; set `@opentermsarchive/engine.logger.timestampPrefix` to `true` or `false` in your configuration file to control this feature. +- Add configuration option to toggle timestamp prefix in logs; set [`@opentermsarchive/engine.logger.timestampPrefix` to `true` or `false` in your configuration file](https://docs.opentermsarchive.org/#configuring) to control this feature. ## 2.6.0 - 2024-11-19