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

Make timestamp prefix in logs configurable #1118

Merged
merged 6 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

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 [minor]

> 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 configuration option to toggle timestamp prefix in logs; set `@opentermsarchive/engine.logger.timestampPrefix` to `true` or `false` in your configuration file to control this feature.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add link to doc 🙂


## 2.6.0 - 2024-11-19

_Full changeset and discussions: [#1116](https://github.com/OpenTermsArchive/engine/pull/1116)._
Expand Down
3 changes: 2 additions & 1 deletion config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
"host": "smtp-relay.sendinblue.com",
"username": "[email protected]"
},
"sendMailOnError": false
"sendMailOnError": false,
"timestampPrefix": true
},
"notifier": {
"sendInBlue": {
Expand Down
7 changes: 5 additions & 2 deletions scripts/dataset/logger/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import config from 'config';
import winston from 'winston';

import logger from '../../../src/logger/index.js';
Expand All @@ -6,11 +7,13 @@ const { combine, timestamp, printf, colorize } = winston.format;

logger.format = combine(
colorize(),
timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
timestamp({ format: 'YYYY-MM-DDTHH:MM:SSZ' }),
printf(({ level, message, counter, hash, timestamp }) => {
const prefix = counter && hash ? `${counter.toString().padEnd(6)} ${hash.padEnd(40)}` : '';

return `${timestamp} ${level.padEnd(15)} ${prefix.padEnd(50)} ${message}`;
const timestampPrefix = config.get('@opentermsarchive/engine.logger.timestampPrefix') ? `${timestamp} ` : '';

return `${timestampPrefix}${level.padEnd(15)} ${prefix.padEnd(50)} ${message}`;
}),
);

Expand Down
8 changes: 6 additions & 2 deletions src/collection-api/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@ if (config.get('@opentermsarchive/engine.logger.sendMailOnError')) {
const logger = winston.createLogger({
format: combine(
colorize(),
timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
printf(({ level, message, timestamp }) => `${timestamp} ${level.padEnd(15)} ${message}`),
timestamp({ format: 'YYYY-MM-DDTHH:MM:SSZ' }),
printf(({ level, message, timestamp }) => {
const timestampPrefix = config.get('@opentermsarchive/engine.logger.timestampPrefix') ? `${timestamp} ` : '';

return `${timestampPrefix}${level.padEnd(15)} ${message}`;
}),
),
transports,
rejectionHandlers: transports,
Expand Down
20 changes: 7 additions & 13 deletions src/logger/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,17 @@ const { combine, timestamp, printf, colorize } = winston.format;

const alignedWithColorsAndTime = combine(
colorize(),
timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
timestamp({ format: 'YYYY-MM-DDTHH:MM:SSZ' }),
printf(({ level, message, timestamp, serviceId, termsType, documentId }) => {
let prefix = '';
const servicePrefix = serviceId && termsType
? `${serviceId} — ${termsType}${documentId ? `:${documentId}` : ''}`
: '';

if (serviceId && termsType) {
prefix = `${serviceId} — ${termsType}`;
}

if (documentId) {
prefix = `${prefix}:${documentId}`;
}
const truncatedPrefix = servicePrefix.length > 75 ? `${servicePrefix.slice(0, 74)}…` : servicePrefix;

if (prefix.length > 75) {
prefix = `${prefix.substring(0, 74)}…`;
}
const timestampPrefix = config.get('@opentermsarchive/engine.logger.timestampPrefix') ? `${timestamp} ` : '';

return `${timestamp} ${level.padEnd(15)} ${prefix.padEnd(75)} ${message}`;
return `${timestampPrefix}${level.padEnd(15)} ${truncatedPrefix.padEnd(75)} ${message}`;
}),
);

Expand Down
Loading