Skip to content

Commit

Permalink
Merge pull request #3 from OpenTermsArchive/warn-logger-mailer-not-en…
Browse files Browse the repository at this point in the history
…abled

Add warning log for disabled Logger mailer due to missing config
  • Loading branch information
Ndpnt authored Feb 15, 2024
2 parents e7065ce + f6749fb commit ef83cc8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 17 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

- Log a warning in case log emails cannot be sent because of a missing config

## 0.1.0 - 2023-12-05

Expand Down
41 changes: 25 additions & 16 deletions src/utils/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -37,4 +21,29 @@ const logger = winston.createLogger({
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 {
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,
});
}
}

export default logger;

0 comments on commit ef83cc8

Please sign in to comment.