From 74781621d709796e71419e689d5465fefb620d1d Mon Sep 17 00:00:00 2001 From: Nicolas Dupont Date: Thu, 15 Feb 2024 11:08:55 +0100 Subject: [PATCH 1/5] Warn on Logger mailer disabled due to missing config --- src/utils/logger.js | 43 +++++++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/src/utils/logger.js b/src/utils/logger.js index 96f45bd..82d365b 100644 --- a/src/utils/logger.js +++ b/src/utils/logger.js @@ -11,22 +11,6 @@ const { combine, timestamp, printf, colorize } = winston.format; const transports = [new winston.transports.Console()]; -if (config.get('logger.sendMailOnError')) { - transports.push(new winston.transports.Mail({ - to: config.get('logger.sendMailOnError.to'), - from: config.get('logger.sendMailOnError.from'), - host: config.get('logger.smtp.host'), - username: config.get('logger.smtp.username'), - password: process.env.SMTP_PASSWORD, - ssl: true, - timeout: 30 * 1000, - formatter: args => args[Object.getOwnPropertySymbols(args)[1]], // Returns the full error message, the same that is visible in the console. It is referenced in the argument object with a Symbol of which we do not have the reference to but we know it is the second one. - exitOnError: true, - level: 'error', - subject: `[OTA] [Federated API] Error — ${os.hostname()}`, - })); -} - const logger = winston.createLogger({ format: combine( colorize(), @@ -37,4 +21,31 @@ const logger = winston.createLogger({ rejectionHandlers: transports, }); +if (process.env.SMTP_PASSWORD) { + if (config.get('logger.sendMailOnError')) { + transports.push(new winston.transports.Mail({ + to: config.get('logger.sendMailOnError.to'), + from: config.get('logger.sendMailOnError.from'), + host: config.get('logger.smtp.host'), + username: config.get('logger.smtp.username'), + password: process.env.SMTP_PASSWORD, + ssl: true, + timeout: 30 * 1000, + formatter: args => args[Object.getOwnPropertySymbols(args)[1]], // Returns the full error message, the same that is visible in the console. It is referenced in the argument object with a Symbol of which we do not have the reference to but we know it is the second one. + exitOnError: true, + level: 'error', + subject: `[OTA] [Federated API] Error — ${os.hostname()}`, + })); + + logger.configure({ + transports, + rejectionHandlers: transports, + }); + } else { + logger.warn('Configuration key "logger.sendMailOnError" was not found; the mailer feature of the Logger module won\'t be enabled'); + } +} else { + logger.warn('Environment variable "SMTP_PASSWORD" was not found; the mailer feature of the Logger module won\'t be enabled'); +} + export default logger; From 5702dc9b6a3630bbbd4b2a63531cdfecc154e007 Mon Sep 17 00:00:00 2001 From: Nicolas Dupont Date: Thu, 15 Feb 2024 11:09:05 +0100 Subject: [PATCH 2/5] Add changelog entry --- CHANGELOG.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a1c0b2b..adf996f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,15 @@ 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 [engine CONTRIBUTING file](https://github.com/OpenTermsArchive/engine/blob/main/CONTRIBUTING.md#changelog). This codebase adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## Unreleased +## Unreleased [patch] + +_Full changeset and discussions: [#3](https://github.com/OpenTermsArchive/engine/pull/3)._ + +> 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. + +### Added + +- Add warning log for disabled Logger mailer due to missing config ## 0.1.0 - 2023-12-05 From 7abd0b0a8e242e0cfd2b6dd70d0e581beb0882f6 Mon Sep 17 00:00:00 2001 From: Nicolas Dupont Date: Thu, 15 Feb 2024 14:27:55 +0100 Subject: [PATCH 3/5] Only warn for missing config if mailer enabled by the config --- src/utils/logger.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/utils/logger.js b/src/utils/logger.js index 82d365b..c4603b3 100644 --- a/src/utils/logger.js +++ b/src/utils/logger.js @@ -21,8 +21,10 @@ const logger = winston.createLogger({ rejectionHandlers: transports, }); -if (process.env.SMTP_PASSWORD) { - if (config.get('logger.sendMailOnError')) { +if (config.get('logger.sendMailOnError')) { + if (process.env.SMTP_PASSWORD) { + logger.warn('Environment variable "SMTP_PASSWORD" was not found; log emails cannot be sent'); + } else { transports.push(new winston.transports.Mail({ to: config.get('logger.sendMailOnError.to'), from: config.get('logger.sendMailOnError.from'), @@ -41,11 +43,7 @@ if (process.env.SMTP_PASSWORD) { transports, rejectionHandlers: transports, }); - } else { - logger.warn('Configuration key "logger.sendMailOnError" was not found; the mailer feature of the Logger module won\'t be enabled'); } -} else { - logger.warn('Environment variable "SMTP_PASSWORD" was not found; the mailer feature of the Logger module won\'t be enabled'); } export default logger; From 4328f45af06daa8c18e9ea3c244ac7aa14db881b Mon Sep 17 00:00:00 2001 From: Nicolas Dupont Date: Thu, 15 Feb 2024 14:28:21 +0100 Subject: [PATCH 4/5] Allow SMTP password to be empty See https://github.com/OpenTermsArchive/federated-api/pull/3#discussion_r1490985170 --- src/utils/logger.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/logger.js b/src/utils/logger.js index c4603b3..b2d212d 100644 --- a/src/utils/logger.js +++ b/src/utils/logger.js @@ -22,7 +22,7 @@ const logger = winston.createLogger({ }); if (config.get('logger.sendMailOnError')) { - if (process.env.SMTP_PASSWORD) { + if (process.env.SMTP_PASSWORD === undefined) { logger.warn('Environment variable "SMTP_PASSWORD" was not found; log emails cannot be sent'); } else { transports.push(new winston.transports.Mail({ From f6749fb491f4cb030955ede8ffe89ce6a49295ff Mon Sep 17 00:00:00 2001 From: Nicolas Dupont Date: Thu, 15 Feb 2024 14:28:28 +0100 Subject: [PATCH 5/5] Improve changelog entry --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index adf996f..f601caf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,7 @@ _Full changeset and discussions: [#3](https://github.com/OpenTermsArchive/engine ### Added -- Add warning log for disabled Logger mailer due to missing config +- Log a warning in case log emails cannot be sent because of a missing config ## 0.1.0 - 2023-12-05