Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add warning log for disabled Logger mailer due to missing config #3

Merged
merged 5 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Loading