Skip to content

Commit

Permalink
Make timestamp prefix in logs configurable (#1118)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ndpnt authored Nov 20, 2024
2 parents 4481873 + 3c3b697 commit 7e6e7b0
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 18 deletions.
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](https://docs.opentermsarchive.org/#configuring) to control this feature.

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

0 comments on commit 7e6e7b0

Please sign in to comment.