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 #1053

Merged
merged 6 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 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

Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
65 changes: 37 additions & 28 deletions src/logger/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
Expand Down
Loading