diff --git a/CHANGELOG.md b/CHANGELOG.md index 5956269f1..f3ac23b0a 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 CONTRIBUTING file. This codebase adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## Unreleased +## Unreleased [patch] + +_Full changeset and discussions: [#1053](https://github.com/OpenTermsArchive/engine/pull/1053)._ + +> 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 + +- Log a warning in case log emails cannot be sent because of a missing config ## 0.36.0 - 2024-02-15 diff --git a/src/index.js b/src/index.js index dd31458c2..d8b7bda85 100644 --- a/src/index.js +++ b/src/index.js @@ -51,10 +51,10 @@ export default async function track({ services, types, extractOnly, schedule }) await reporter.initialize(); archivist.attach(reporter); } else { - logger.warn('Configuration key "reporter.githubIssues.repositories.declarations" was not found; the Reporter module will be ignored'); + logger.warn('Configuration key "reporter.githubIssues.repositories.declarations" was not found; issues on the declarations repository cannot be created'); } } else { - logger.warn('Environment variable "GITHUB_TOKEN" was not found; the Notifier module will be ignored'); + logger.warn('Environment variable "GITHUB_TOKEN" was not found; the Reporter module will be ignored'); } if (!schedule) { diff --git a/src/logger/index.js b/src/logger/index.js index 1cb73f7c0..f14bca85d 100644 --- a/src/logger/index.js +++ b/src/logger/index.js @@ -34,43 +34,52 @@ const consoleTransport = new winston.transports.Console(); const transports = [consoleTransport]; -if (process.env.SMTP_PASSWORD && config.get('logger.sendMailOnError')) { - const mailerOptions = { - 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 visible in the console. It is referenced in the argument object with a Symbol of which we do not have the reference but we know it is the second one. - exitOnError: true, - }; - - transports.push(new winston.transports.Mail({ - ...mailerOptions, - level: 'error', - subject: `[OTA] Error Report — ${os.hostname()}`, - })); - - if (config.get('logger.sendMailOnError.sendWarnings')) { +const logger = winston.createLogger({ + format: alignedWithColorsAndTime, + transports, + rejectionHandlers: transports, +}); + +if (config.get('logger.sendMailOnError')) { + if (process.env.SMTP_PASSWORD === undefined) { + logger.warn('Environment variable "SMTP_PASSWORD" was not found; log emails cannot be sent'); + } else { + const mailerOptions = { + 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 visible in the console. It is referenced in the argument object with a Symbol of which we do not have the reference but we know it is the second one. + exitOnError: true, + }; + transports.push(new winston.transports.Mail({ ...mailerOptions, - level: 'warn', - subject: `[OTA] Inaccessible content — ${os.hostname()}`, + level: 'error', + subject: `[OTA] Error Report — ${os.hostname()}`, })); + + if (config.get('logger.sendMailOnError.sendWarnings')) { + transports.push(new winston.transports.Mail({ + ...mailerOptions, + level: 'warn', + subject: `[OTA] Inaccessible content — ${os.hostname()}`, + })); + } + + logger.configure({ + transports, + rejectionHandlers: transports, + }); } } let recordedSnapshotsCount; let recordedVersionsCount; -const logger = winston.createLogger({ - format: alignedWithColorsAndTime, - transports, - rejectionHandlers: transports, -}); - logger.onFirstSnapshotRecorded = ({ serviceId, termsType, documentId, id }) => { logger.info({ message: `Recorded first snapshot with id ${id}`, serviceId, termsType, documentId }); recordedSnapshotsCount++;